How to Setup Amazon S3 in a Django Project
In this tutorial you will learn how to use the Amazon S3 service to handle static assets and the user uploaded files, that is, the media assets.
First, I will cover the basic concepts, installation and configuration. Then you will find three sections covering:
- Working with static assets only
- Working with static and media assets
- Mixing public assets and private assets
Dependencies
You will need to install two Python libraries:
- boto3
- django-storages
The boto3 library is a public API client to access the Amazon Web Services (AWS) resources, such as the Amazon S3. It’s an official distribution maintained by Amazon.
The django-storages is an open-source library to manage storage backends like Dropbox, OneDrive and Amazon S3. It’s very convenient, as it plugs in the built-in Django storage backend API. In other words, it will make you life easier, as it won’t drastically change how you interact with the static/media assets. We will only need to add a few configuration parameters and it will do all the hard work for us.
Amazon S3 Setup
Before we get to the Django part, let’s set up the S3 part. We will need to create a user that have access to manage our S3 resources.
Logged in the AWS web page, find the IAM in the list of services, it’s listed under Security, Identity & Compliance:
Go to the Users tab and click in the Add user button:
Give a user name and select the programmatic access option:
Click next to proceed to permissions. At this point we will need to create a new group with the right S3 permissions, and add our new user to it. Follow the wizard and click in the Create group button:
Define a name for the group and search for the built-in policy AmazonS3FullAccess:
Click in the Create group to finalize the group creation process, in the next screen, the recently created group will show up selected, keep it that way and finally click in the button Next: Review:
Review the information, if everything is correct proceed to create the new user. Next, you should see this information:
Take note of all the information: User, Access key ID and the Secret access key. Save them for later.
Click in the Close button and let’s proceed. Now, it’s time to create our very first bucket.
Bucket is what we call a storage container in S3. We can work with several buckets within the same Django project. But, for the most part you will only need one bucket per website.
Click in the Services menu and search for S3. It’s located under Storage. If you see the screen below, you are in the right place.
Back to Top