AWS S3 bucket file upload with python and Boto3

Asiru Erioluwa
Hacktive Devs
Published in
3 min readFeb 23, 2019

Before we get into the code it is necessary we understand what we are doing, why we are doing it and most especially some terms we'll be dealing with here.

Aws S3 is a simple object storage service(a computer data storage architecture that manages data as objects) that helps to store large amount of data irrespective of its purpose. Here we’ll be dealing with files but you can read more on s3 here

Boto is the Amazon Web Services (AWS) SDK for Python, which allows Python developers to write software that makes use of Amazon services like S3 and EC2. Boto provides an easy to use, object-oriented API as well as low-level direct service access.

It is important you have the basic knowledge of python for this tutorial and make sure you have python installed as well as flask and boto3. you can install flask and boto3 using pip as follows:

Also to get started you must have created your s3 bucket with aws, lets do a brief run through of that.

After you must have created an account with AWS and verified your account has been activated login to your Aws management console, under Storage & Content Delivery, choose S3 to open the Amazon S3 console.

search for S3 in the search box provided. I only have it there because i used it recently. You should have a page like this:

go ahead to create bucket and follow the necessarily procedures which is quite straight forward and ensure to copy out your secrete key and access key.

now we have a web page like above and the content of our app.py as below

go ahead create a config.py file to contain the credentials for your aws account. the config.py file contains the following

import your credentials into your app as below:

using the s3 boto client input the following :

here we defined our bucket_resource as s3, you can give any name of your choice just to understand what going on there.

Accept your file as below (img is a name i gave to it because i am accepting images)

Now make your upload using the s3 client object known as upload_file() as below:

you can look up your bucket overview to check for your just uploaded file.

So this is how to upload a file to aws s3 bucket. I belief this is pretty easy to implement and quite straight forward.

You can look up the entire code on github

Thanks!

--

--