uploading file to specific folder in S3 using boto3

Alex picture Alex · Sep 1, 2016 · Viewed 30.8k times · Source

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!

Answer

hjpotter92 picture hjpotter92 · Sep 1, 2016

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