Heroku only saves the static files and deletes the media files on refresh of dyno which occurs every 30 minutes. So, we need to store media files to some other location, here we are going to use Amazon Web Services. We are going to make use of Amazon S3 bucket as default media storage.
To use aws resources you first need to create and IAM user.
Create IAM User & Get AWS Access Keys
To get your access key ID and secret access key follow the steps below.
- Open the IAM console. If you have not already logged in then sign in to the aws account. You can also create a new account if you don’t have one.
- From the navigation menu at left side, click on Users. You will get a previously created list of existing users and none if No user is created earlier.
- Now from top click on Add User and you will get a new screen. Now enter your Username and select the Programmatic access and AWS Management Console access checkbox in Access type section. Enter the custom password in Console Password section. Now click on Next: Permissions on bottom right side.
- In next window, Attach existing policies directly and select AdministratorAccess and click on Next: Review
- Now on next window just click on Create user
- Now you will get a success screen and new IAM User is created just make a note of Access key ID and Secret access key, it will be used later in this tutorial. Alternatively you can also download the csv file of credentials using Download .csv button or email it using Send email link.
Now we need to create a S3 bucket to store our files in it.
Create S3 Bucket
To create S3 bucket follow the steps below:
- Open the S3 Bucket AWS console. It will show the existing buckets if it is previously created. You can create a new bucket from here. Just click on Create bucket.
On the opened window enter the Bucket Name and Choose the Region and click on Next.
Do Next and Next and finally click on Create Bucket. And once this is done, new bucket will be created and it will show up on the page.
Note: Here I have already created my bucket and am going to make use of my S3 bucket
Now, as IAM user is created and we have access keys from AWS and we have also created S3 bucket.
Let’s store these keys in Heroku config vars so that we can access it using
Saving Environment variables to Heroku
First, you need to go to Heroku dashboard settings, click on your app and then click on settings tab and finally click on config vars.
Then just enter all your environment variables like
AWS_SECRET_ACCESS_KEY and etc.... by entering Key and Value and clicking on
In config vars, add
AWS_SECRET_ACCESS_KEY from what you received after creating IAM user.
AWS_STORAGE_BUCKET_NAME will be
kodefork-assets, in your case the name of your bucket.
AWS_S3_CUSTOM_DOMAIN will be
kodefork-assets.s3.amazonaws.com, see carefully it’s bucket name followed by
MEDIA_URL will be
https://kodefork-assets.s3.amazonaws.com/, again see it is my bucket_name and then url is formed.
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
AWS_ACCESS_KEY_ID = os.environ.get("AWS_ACCESS_KEY_ID", "")
AWS_SECRET_ACCESS_KEY = os.environ.get("AWS_SECRET_ACCESS_KEY", "")
AWS_STORAGE_BUCKET_NAME = os.environ.get("AWS_STORAGE_BUCKET_NAME", "")
AWS_QUERYSTRING_AUTH = False
AWS_S3_CUSTOM_DOMAIN = os.environ.get("AWS_S3_CUSTOM_DOMAIN", "")
MEDIA_ROOT = os.environ.get("MEDIA_URL", "")
MEDIA_URL = '/media/'
Now just update your code to heroku and its should work and file should be stored in your S3 bucket.
Like this post
Share this post on
About the author
- Career over Relationships. Family above everything else.
- Passionate Geek with love for Computer Science, Artificial Intelligence.
- Avid interest in Economics, Sports and Adventures.
- Curious to know outside the boundary limits, coz what I know is very little and what I don’t know is huge.
Join the discussion
can you help me to store media file in s3
Yeah Tell me what’s the problem ?
I have followed your tutorial on Django and Amazon S3 to upload the media and static files to Amazon S3 bucket, however I am unable to upload those files, Maybe I am missing some steps can you help me resolve the issue.
bro your problem solve or not