Django with Heroku and S3 .jpg

Deploying django to Heroku with AWS S3 as media storage

In this tutorial we cover how to upload your Django site to Heroku and host your media files on AWS S3.

Introduction

There are many tutorials out there on how to deploy django applications to heroku and how to link django to S3 but some of them are outdated and people usually get errors which they take hours to google and fix. I won't be writing deployment to heroku all over again, we will start from setting our Amazon S3 to store media files.

Heroku:

Open the tutorial below in new tab and follow every step exactly but don't push your code to heroku yet, we will do that after we configure our settings.py site to store media files on S3. For that, first we will need to create a bucket in S3.

https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Deployment

Amazon AWS:

I am assuming you already have an account on AWS, if you don't, you need to make one.

Creating an IAM user:

Sign in to the AWS Management Console and open the IAM console at https://console.aws.amazon.com/iam/.

Step 1 : In the navigation pane, choose Users and then choose Add user.

tutorial-aws.jpg

Step 2 : Type the user name for the new user. This is the sign-in name for AWS. If you want to add more than one user at the same time, choose Add another user for each additional user and type their user names. You can add up to 10 users at one time.

Step 3 : Check the "Programmatic Access" option and then click on "Next: Permissions".

aws-s3.png

Step 4 : Now we will need to create a group with right permissions to access S3 buckets and then add our user to it. Click the 'Create Group' button as seen in the image:

aws-s3.png

Step 5 : Choose a name for your group, type 's3' in your search box and select the option named 'AmazonS3FullAccess' and click 'Create Group' as shown in the picture:

tutorial-aws.PNG


Don't change anything on the next page, just click 'next: tags' button.
You don't need to change anything on this page either, just click 'Next : Review' button.
Click on 'Create User' and your IAM user will be created with full access to your S3 buckets.

Download your credentials and save them in a secure place, you will need them later.

Creating an S3 bucket for storing data:

So what are S3 buckets?
" An Amazon S3 bucket is a public cloud storage resource available in Amazon Web Services' (AWS) Simple Storage Service (S3), an object storage offering. Amazon S3 buckets, which are similar to file folders, store objects, which consist of data and its descriptive metadata. "

Open the Amazon S3 console at https://console.aws.amazon.com/s3/.

Choose Create Bucket:

aws-s3.png

In the Bucket name box, enter a unique DNS-compliant name for your new bucket. I will choose 'tutorial-webwithdjango' as my bucket name.
Select the region of your choice and make sure to deselect the option named ' Block all public access ', AWS will warn you but we NEED this so the public can read the files within our bucket, then click on 'Create Bucket' button:

aws-s3.png


Go to https://console.aws.amazon.com/s3/, now you should see your bucket listed there. Click on your bucket, got to 'Permissions' tab, then click on 'CORS configuration' and paste this code there and click 'Save'.

<?xml version="1.0" encoding="UTF-8"?>

<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">

<CORSRule>

<AllowedOrigin>*</AllowedOrigin>

<AllowedMethod>GET</AllowedMethod>

<MaxAgeSeconds>3000</MaxAgeSeconds>

<AllowedHeader>Authorization</AllowedHeader>

</CORSRule>

</CORSConfiguration>

aws-s3.png

Django Configuration:

First of all, we need to install 2 libraries named 'boto3' and '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.

Install these libraries with pip :
pip install boto3
pip install django-storages

Make sure to add 'storages' to your INSTALLED_APPS inside your settings.py.

Create a file named 'storages.py' in the same directory/folder as your settings.py file and paste this code there:

 
from storages.backends.s3boto3 import S3Boto3Storage

class MediaStorage(S3Boto3Storage):
    location = 'media'
    file_overwrite = False


  

This tells the S3Boto3Storage that our files should be stored in root directory named 'media' and that it shouldn't over ride files with the same name.

Add these settings to your settings.py file now:

 
AWS_ACCESS_KEY_ID = #Paste in the AWS_ACCESS_KEY_ID from the file we saved earlier when we created our IAM user.
AWS_SECRET_ACCESS_KEY = ##Paste in the AWS_SECRET_ACCESS_KEY from the file we saved earlier when we created our IAM user.
AWS_STORAGE_BUCKET_NAME = 'tutorial-webwithdjango' #Change this with your own bucket name.
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME

AWS_S3_OBJECT_PARAMETERS = {
    'CacheControl': 'max-age=86400',
}
DEFAULT_FILE_STORAGE = 'your_site_name.storages.MediaStorage' #Change the 'your_site_name' to the name of your django site.


  

Debugging:

Getting errors after deploying your code? Follow this link to see how to debug:

https://webwithdjango.com/blog/django-heroku-deployment-error-fixing/

Learn Django with us

Visit Youtube Channel
Latest articles