Simple Tutorial Python, Boto3, and AWS S3

I’ve horrible experience to find a “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.

What is Boto3?

The AWS SDK for Python. If you have a python app and you want this app able 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:

$ pip install boto3

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

$ pip install awscli

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

$ aws configure

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.


or using some code below:

import boto3
import json

data = {"HelloWorld": []}
s3 = boto3.resource('s3')

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.

import boto3
import json

data = {"HelloWorld": []}
s3 = boto3.resource('s3')
obj = s3.Object('my-bucket','hello.json')

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

obj = s3.Object('my-bucket','hello.json')


obj = s3.Object('my-bucket','my-path/hello.json')

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.

import boto3

data = {"HelloWorld": []}
s3 = boto3.resource('s3')
obj = s3.Object('my-bucket','hello.json')
data = obj.get()['Body'].read()
print data

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

Lecturer at Universitas Islam Indonesia; Machine Learning Enthusiast

View all posts

1 Comment

Leave a Reply