How to upload a file in S3 bucket using boto3 in python

+2 votes

How can I upload a file in S3 bucket using boto in Python?

Ex: I have a bucket name = Text. And in the bucket, I have 2 folders named "input" & "output". Now I want to copy a file from the local directory to S3 "dump" folder using python. Can anyone help me?

Nov 30, 2018 in AWS by Nitesh
• 3,080 points

edited Jul 2, 2020 by Gitika 28,946 views

1 answer to this question.

+1 vote

You can use method of creating object instance to upload the file from your local machine to AWS S3 bucket in Python using boto3 library.

Here is the code I used for doing this:

import boto3
s3 = boto3.resource('s3')
s3.create_bucket(Bucket= 'anikets3bucket')
s3.Object('anikets3bucket','abcd.txt').upload_file(Filename='C:\\Users\\Aniket\\Desktop\\abcd.txt')

Hope it helps!

If you need to know more about Python, It's recommended to join Python course today.

Thanks!

answered Nov 30, 2018 by Aniket
perfect.Thank you

Hello, 
I am trying to upload a file inside the sub-folder. I tried something like 

s3.Object('anikets3bucket/folder1','abcd.txt').upload_file(Filename='C:\\Users\\Aniket\\Desktop\\abcd.txt')

It does not work. Suggestions please ...

What error are you getting?

    raise ParamValidationError(report=error_msg)
botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid bucket name "anikets3bucket/folder1

": Bucket name must match the regex "^[a-zA-Z0-9.\-_]{1,255}$"

Bucket names cannot have '/' in them. When you use a url like this "anikets3bucket/folder1" the tool simply treats the parts behind / as "directory".

Check your bucket name properly.
Perfect! Helped a lot!

Related Questions In AWS

0 votes
1 answer

How to download the latest file in a S3 bucket using AWS CLI?

You can use the below command $ aws ...READ MORE

answered Sep 6, 2018 in AWS by Archana
• 4,170 points
18,938 views
0 votes
0 answers

How to upload a file in to aws s3 by using programmatically??

Sep 13, 2019 in AWS by anonymous

closed Sep 16, 2019 by Kalgi 3,666 views
0 votes
1 answer

AWS S3 uploading hidden files by default

versioning is enabled in your bucket. docs.aws.amazon.com/AmazonS3/latest/user-guide/….... the ...READ MORE

answered Oct 4, 2018 in AWS by Priyaj
• 58,090 points
5,387 views
–1 vote
1 answer

How to decrypt the encrypted S3 file using aws-encryption-cli --decrypt

Use command : aws s3 presign s3://mybucket/abc_count.png you get ...READ MORE

answered Oct 22, 2018 in AWS by Priyaj
• 58,090 points
4,823 views
0 votes
1 answer

Import my AWS credentials using python script

Using AWS Cli  Configure your IAM user then ...READ MORE

answered Nov 16, 2018 in AWS by Jino
• 5,810 points
2,557 views
0 votes
2 answers
0 votes
1 answer

How to upload a file from S3 in Lambda?

Yes there is an option where you ...READ MORE

answered Jan 24, 2019 in AWS by Priyaj
• 58,090 points
4,796 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP