Uploading a file to a specific folder on S3 using boto3

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

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

I 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 possible), please let me know!

+12
source share
5 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)) 
+27
source

I understood my problem. I had the right idea with the /folder/ option in the key options area, however I don't need the first one / Thanks to everyone! This is essentially the same idea as hjpotter92.

+3
source

Here is a method that will take care of the nested directory structure and be able to download the full directory using boto

 def upload_directory(): for root, dirs, files in os.walk(settings.LOCAL_SYNC_LOCATION): nested_dir = root.replace(settings.LOCAL_SYNC_LOCATION, '') if nested_dir: nested_dir = nested_dir.replace('/','',1) + '/' for file in files: complete_file_path = os.path.join(root, file) file = nested_dir + file if nested_dir else file print "[S3_UPLOAD] Going to upload {complete_file_path} to s3 bucket {s3_bucket} as {file}"\ .format(complete_file_path=complete_file_path, s3_bucket=settings.S3_BUCKET, file=file) s3_client.upload_file(complete_file_path, settings.S3_BUCKET, file) 
0
source

Use this for Python 3.x

 s3.upload_file(file_path,bucket_name, '%s/%s' % (bucket_folder,dest_file_name)) 
0
source
 upload_file(f'/tmp/{filename}', bucket_name, 'folder/{filename}') 

much cleaner

0
source

All Articles