Search code examples
amazon-web-servicesamazon-s3boto3

Files are empty after copying it to the S3 bucket


I have an S3 bucket called source-bucket-6 and I write a lambda function to copy files from the lambda location to the S3 bucket.

enter image description here

As you see in the screenshot, here we have two files Index.html and Test.txt. I just want to copy those files to the S3 bucket. But whenever I try to copy them to the bucket, I see both files are empty and there's no content in the files. Here's my lambda code:

import boto3
import os

s3 = boto3.client('s3')
cwd = os.getcwd()


def lambda_handler(event, context):
    bucket = 'source-bucket-6'


    files = [f for f in os.listdir('.') if os.path.isfile(f)]
    for f in files:
        if f != "lambda_function.py":
            s3.put_object(Bucket=bucket, Key=f)
            print(f"------------------{f} ")
        
    print("Put Completed")


Solution

  • Your put_object command is not specifying any content for the object.

    If you want to upload a file, use:

    s3.upload_file(Filename=f, Bucket=bucket, Key=f)
    

    where f is the filename of the file to upload.