我曾尝试使用 lambda 函数将文件写入 S3,然后测试显示“成功”,但我的 S3 存储桶中没有任何内容。发生了什么?有谁能给我一些建议或解决方案吗?非常感谢。这是我的代码。
import json
import boto3
def lambda_handler(event, context):
string = "dfghj"
file_name = "hello.txt"
lambda_path = "/tmp/" + file_name
s3_path = "/100001/20180223/" + file_name
with open(lambda_path, 'w+') as file:
file.write(string)
file.close()
s3 = boto3.resource('s3')
s3.meta.client.upload_file(lambda_path, 's3bucket', s3_path)
最佳答案
我已成功将数据流式传输到 S3,必须对其进行编码才能执行此操作:
import boto3
def lambda_handler(event, context):
string = "dfghj"
encoded_string = string.encode("utf-8")
bucket_name = "s3bucket"
file_name = "hello.txt"
s3_path = "100001/20180223/" + file_name
s3 = boto3.resource("s3")
s3.Bucket(bucket_name).put_object(Key=s3_path, Body=encoded_string)
如果数据在一个文件中,你可以读取这个文件并发送:
with open(filename) as f:
string = f.read()
encoded_string = string.encode("utf-8")
关于python - 我如何使用 aws lambda 将文件写入 s3 (python)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48945389/