Simple Tutorial Python, Boto3, and AWS S3

I’ve horrible experience to find “good” tutorial about how to use Boto3 in Amazon Web Service (AWS). Boto3, not like Boto2, has poor quality documentation. So I create this simple tutorial as reminder to myself and I hope it will help someone out there.

Boto3

The AWS SDK for Python. It simply to said, if you have a python apps and you want it to access AWS features, you need this.

Installation & Setup

Installation is very clear in python documentation and for configuration you can check in Boto3 documentation just using pip:

after install boto3 you can use awscli to make it easier setup credentials, install it using pip too:

set your configuration using command below. You can get your access key and secret key in IAM console:

Create A Simple App

Let’s create a simple app using Boto3. We will create a simple app to access stored data in AWS S3. This app will write and read a json file stored in S3.

1. Prepare Your Bucket

First, you need to create a bucket in your S3. You can create bucket by visiting your S3 service and click Create Bucket button.

Capture

or using some code below:

If you confuse what is bucket and how it works, this one have nice explanation.

2. Write JSON File

Next part is how to write a file in S3. The code below will create a json file (if it doesn’t exist, or overwrite it otherwise) named hello.json and put it in your bucket.

If you want to put it on specific path, you can change the line

to

Run it, and if you check your bucket now you will find your file in there.

3. Read JSON File

Last but not least, if you want to read your file, you can use get() function. The code below will read your hello.json file and show it on screen. File body/content will be read as string by default.

I think that’s all, a simple tutorial using boto3 to read-write file in Amazon Web Service S3. Hope it helps 🙂

About the author

Rian Adam

View all posts

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *