How to upload a file to Amazon S3 in Python

Ahmad Bilesanmi
Bilesanmi Ahmad
Published in
2 min readMay 4, 2018

--

image credit: Kwame Sarpong

For those building production applications may decide to use Amazon Web Services to host their applications and also take advantage of the many wonderful services they offer. One of these services is Amazon S3 (Simple Storage Service). This service is responsible for storage of files like images, videos, music, documents and so on. It is also important for storing static files for web applications, like CSS and JavaScript files.

In this tutorial, I will be showing how to upload files to Amazon S3 using Amazon’s SDK — Boto3.

  1. Get Your Access Key and Access Secret

Once you have an account with Amazon Web Services, you would need an access key and secret. This will help you to make secure REST or HTTP Query protocol requests to AWS.

a. Log in to your AWS Management Console.

b. Click on your username at the top-right of the page to open the drop-down menu.

c. Click on ‘My Security Credentials’.

d. Click on ‘Dashboard’ on the left side of the page.

e. Click on ‘Rotate your access keys’ from the ‘Security Status’ section.

f. Click on the ‘Manage user keys’ button.

g. Click on the ‘Security credentials’ tab to view your access keys.

h. To create a new access key and secret, click on the ‘Create access key’ button.

i. Download the .csv file containing your access key and secret. Please keep it safe.

2. Install boto3 to your application

If you are using pip as your package installer, use the code below:

pip install boto3

If you are using pipenv as your package installer and virtual environment:

pipenv install boto3

3. Your Python code

import boto3
from botocore.exceptions import NoCredentialsError

ACCESS_KEY = 'XXXXXXXXXXXXXXXXXXXXXXX'
SECRET_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'


def upload_to_aws(local_file, bucket, s3_file):
s3 = boto3.client('s3', aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY)

try:
s3.upload_file(local_file, bucket, s3_file)
print("Upload Successful")
return True
except FileNotFoundError:
print("The file was not found")
return False
except NoCredentialsError:
print("Credentials not available")
return False


uploaded = upload_to_aws('local_file', 'bucket_name', 's3_file_name')

Note: Do not include your client key and secret in your python files for security purposes. I prefer using environmental variables to keep my key and secret safe. For more on different ways to use your AWS credentials, please check here.

And that’s it. Please let me know if there’s a better way to do this so I can learn too.

--

--

Ahmad Bilesanmi
Bilesanmi Ahmad

Software Engineer || Python || Javascript || DevOps || Data