S3 With Boto3
Setup Boto3 Guide
This is a guide on accessing S3 with the Boto3 package.
Prerequisites
Create a user in Aws IAM with at least S3 permissions. It is highly recommended that you create a group with S3 permissions and assign your user to that group. See “IAM Basics Tutorial” for more details.
Installing and Configuring
- Install boto3.
1
pip install boto3
- Use
aws configure
to add your credentials and select your default region:1
aws configure
- Configuration can look as follows:
1 2 3 4
AWS Access Key ID: ***** AWS Secret Access Key: ***** Default region name: us-east-1 Default output format:
- Configuration can look as follows:
Using Boto3 to access S3
Now open up the Python shell and make a connection to S3 to check that your credentials are working properly. If everything is working properly it should print out all buckets you have on S3.
- List all of your buckets with the following command:
1 2 3 4 5 6
import boto3 s3 = boto3.resource("s3") print(s3) #Expected: s3.ServiceResource() # for bucket in s3.buckets.all(): print(bucket.name) #Expect to see all your buckets in S3
- Also, you can create and upload new files to S3 with the following:
1 2 3 4 5 6
with open("readme.txt", "w") as f: f.write("Hello World") f.write("This is a new text file!") # with open("readme.txt", "rb") as f: s3.Bucket("my-bucket").put_object(Key="readme.txt", Body=f)
Conclusion
Now that you should have a basic understanding of how to use Boto3 to access S3.
This post is licensed under CC BY 4.0 by the author.