且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用AWS Lambda将文件写入s3(python)?

更新时间:2021-09-28 21:32:01

我已经成功将数据流传输到S3,必须对此进行编码:

I've had success streaming data to S3, it has to be encoded to do this:

import boto3

def lambda_handler(event, context):
    string = "dfghj"
    encoded_string = string.encode("utf-8")

    bucket_name = "s3bucket"
    file_name = "hello.txt"
    lambda_path = "/tmp/" + file_name
    s3_path = "/100001/20180223/" + file_name

    s3 = boto3.resource("s3")
    s3.Bucket(bucket_name).put_object(Key=s3_path, Body=encoded_string)

如果数据在文件中,则可以读取此文件并将其发送:

If the data is in a file, you can read this file and send it up:

with open(filename) as f:
    string = f.read()

encoded_string = string.encode("utf-8")