How could I use aws lambda to write file to s3 (python)?

后端 未结 2 763
后悔当初
后悔当初 2021-02-02 08:52

I have tried to use lambda function to write a file to S3, then test shows \"succeeded\" ,but nothing appeared in my S3 bucket. What happened? Does anyone can give me some advic

2条回答
  •  醉话见心
    2021-02-02 09:32

    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")
    

提交回复
热议问题