简体   繁体   中英

Using boto3 to upload files to s3 bucket within specific folder

I have the code below that uploads files to my s3 bucket. However, I want the file to go into a specific folder if it exists. If the folder does not exist, it should make the folder and then add the file.

This is the line I use the add my files.

response = s3_client.upload_file(file_name, bucket, object_name)

My desired folder name is:

<Year>/<Month>/<day>/<this is the file>

Obviously each day, we would need to make new folders. How can I do this with boto3?

There is no need to create folders . Simply uploading an object to a particular path will make the folders automatically 'appear'.

Therefore, your code only needs to figure out the correct full path (Key) for where to upload the object. Assuming that you want it to go to a folder with today's date , you would need to create the correct path in object_name :

from datetime import datetime

target_key = datetime.now().strftime('%Y/%m/%d/') + object_name

response = s3_client.upload_file(file_name, bucket, target_key)

However, please note that the AWS Lambda function runs in the UTC timezone, so you might need to adjust the time based on your local timezone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM