Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uploading file to specific folder in S3 using boto3

Everything with my code works. The only pitfall I am currently facing is that I cannot specify the folder within the S3 bucket that I would like to place my file in. Here is what I have:

s3.meta.client.upload_file('/tmp/'+filename, '<bucket-name>', filename)

I have tried both:

s3.meta.client.upload_file('/tmp/'+filename, '<bucket-name>/folder/', filename)

and:

s3.meta.client.upload_file('/tmp/'+filename, '<bucket-name>', '/folder/'+filename)

if anyone has any tips on how to direct this to a specific folder (if this is possible) please let me know!

like image 568
Alex Avatar asked Sep 01 '16 13:09

Alex


People also ask

How do I upload files and folders to an S3 bucket?

To upload folders and files to an S3 bucketSign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that you want to upload your folders or files to. Choose Upload.

How do you append data to an existing csv file in AWS S3 using Python boto3?

s3 has no append functionality. You need to read the file from s3, append the data in your code, then upload the complete file to the same key in s3.


4 Answers

You do not need to pass the Key value as an absolute path. The following should work:

upload_file('/tmp/' + filename, '<bucket-name>', 'folder/{}'.format(filename))
like image 113
hjpotter92 Avatar answered Oct 14 '22 06:10

hjpotter92


I figured out my problem. I had the right idea with the /folder/ option in the key parameter area, however, I did not need the first / Thank you all! This is essentially the same idea as hjpotter92's suggestion above.

like image 23
Alex Avatar answered Oct 14 '22 06:10

Alex


Use this for python 3.x

s3.upload_file(file_path,bucket_name, '%s/%s' % (bucket_folder,dest_file_name))
like image 7
Andrew Figaroa Avatar answered Oct 14 '22 07:10

Andrew Figaroa


You can use put_object in the place of upload_file:

file = open(r"/tmp/" + filename)
response = s3.meta.client.Bucket('<bucket-name>').put_object(Key='folder/{}'.format(filename), Body=file)
like image 2
OM Bharatiya Avatar answered Oct 14 '22 07:10

OM Bharatiya