要实现从互联网下载文件并直接上传到AWS S3的功能,可以使用AWS Lambda和Python编程语言完成。下面是一个代码示例:
import boto3
import requests
def lambda_handler(event, context):
# 下载文件
url = "https://example.com/file.txt" # 替换为实际文件的URL
response = requests.get(url)
file_content = response.content
# 将文件上传到S3
s3_client = boto3.client('s3')
bucket_name = "your-bucket-name" # 替换为实际的S3存储桶名称
file_name = "file.txt" # 替换为实际的文件名
s3_client.put_object(Body=file_content, Bucket=bucket_name, Key=file_name)
return {
'statusCode': 200,
'body': '文件上传成功!'
}
在此示例中,我们首先使用requests
库下载文件并将其内容保存在file_content
变量中。然后,我们使用boto3
库的S3客户端来将文件内容上传到指定的S3存储桶中的指定文件名。
确保根据实际情况替换示例代码中的URL、存储桶名称和文件名。