Tech Incent

How to setup django static and media file in aws s3 ?

In this tutorial, we are setting up AWS s3 for your Django static and media files. So we can store static files and upload media files more effective manner. Django settings up is the best way that I found to set up

All over the world developers highly recommend AWS. Using AWS s3 for Django static files is a really good way to serve and be more flexible. When the user comes to a website, static files are better loaded for the user. Also, AWS s3 has a lot of good reasons.

So it’s a two-part setup…

  1. Aws and s3 setup
  2. Django setup

Aws and s3 setup

01. Login aws or Create an account here

02. Create aws user credentials

  • Go to IAM service and create a user with programming access
    s3 adduser
  • Now create a new group
  • Skip next steps of add user in aws and complete create user
  • The note access key and secret key as that are needed for future steps Or download to store key for backup. that will be used as

    downalod keys

Note: this AWS user, access, secret keys will be removed after publishing this article for AWS safety security.

03. Create new S3 Bucket

01. Go to s3 service and create a bucket. it’s important to choose your region name. Aws s3 is fast but Closes region visitors will browse faster. So if you have a large-scale application, it’s a matter to choose a bucket region.

Note: if you want to create the same name bucket that I was created or another developer created, you will fail because you can’t create the same name bucket
create bucket

02. Select default settings for the next steps of the creation process of the bucket

03. You need to make permission of cors configuration for this bucket

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

04. You must be in your bucket and add this in cors configuration

05. Create static and media folder in your bucket

Great now you are done configuration of your bucket.

04. Add custom policy or permissions for your IAM user or group

Custom policy or permissions for Django allow uploading. delete, change files.
I am talking about manage permission or policy for this project. So we can add a policy for this user. yes, I can. but is it good? Ans: it’s not. why because if another developer wants to work on this project, he has no permissions for access or maintains s3.

Now question is, how do permissions to access or modify this project? how to attach policy? don’t worry I will explain. We will attach a policy with the IAM Group. Why? Because if another developer wants to work on this project. he will simply add to the group.


05. We will use this policy, this policy allows us to upload, change and delete things

    "Version": "2012-10-17",
    "Statement": [
            "Effect": "Allow",
            "Action": [
            "Resource": "arn:aws:s3:::*"
            "Effect": "Allow",
            "Action": [
            "Resource": "arn:aws:s3:::<your_bucket_name>"
            "Effect": "Allow",
            "Action": [
            "Resource": "arn:aws:s3:::/<your_bucket_name>"

06. Selected your new policy to attach to group


07. Now attach with group


So that great. we had created IAM User, Group, S3 Bucket, and Policy or permissions for the bucket. that’s it. Now we can set up our Django application for s3.

Django s3 object storage setup

First need two packages for setup Django s3 configuration.

  1. boto3
    boto3 is an object-oriented API client to access and manage such as AWS S3 resources and EC2. boto is an official distribution maintained by Amazon.
  2. Django-storages
    Django-storages is an open-source package to manage custom backend storage such as AWS S3, Digital Ocean, Google Cloud Storage,  Apache Libclod, Azure Storage, DropBox’s make easy to use or maintain backend storage

Install two package

pip install boto3 django-storages



Run Migrations

python migrate

Configuration for AWS

You can simply add this configuration to your file

AWS_ACCESS_KEY_ID = '<your_access_key>'
AWS_SECRET_ACCESS_KEY = '<your_secrect_key>'
AWS_STORAGE_BUCKET_NAME = '<Your_bucket_name>'
    'CacheControl': 'max-age=86400',
AWS_LOCATION = 'static'

    os.path.join(BASE_DIR, 'static'),

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage' 
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
Collectstatic file to AWS
python collectstatic
collected-static files
Collected static file

It will work. But I configure with a separate folder call AWS.

The best way to configure AWS or How I configure AWS settings with a separate folder

01. Add ‘aws’ folder in django_s3(my django_s3, your case may be different with your project name) directory.

SAJAL-ubuntu:~/Programming/test/django-s3$ tree -d
├── core
│   └── migrations
├── django_s3
│   └── aws   ## crated new aws Configuration folder
├── static
│   ├── css
│   ├── images
│   └── js
└── templates

02. In the django_s3 directory make three files calls “”, “”, “”

SAJAL-ubuntu:~/Programming/test/django-s3$ tree django_s3
├── aws
│   ├──  ## new File
│   ├──  ## new File
│   └──  ## new File

03 Add configuration in files

import datetime
import os

AWS_USERNAME = 'django_s3' # not matter
AWS_GROUP_NAME = 'django_s3_group' # not matter
# Aws config
AWS_SECRET_ACCESS_KEY = '2IF3Z/k4gni+YIl4RVIA4eOXp0ncBqFsrncqdHqf'

AWS_STORAGE_BUCKET_NAME = 'djang-s3-bucket'
S3DIRECT_REGION = 'us-east-1'
MEDIA_ROOT = S3_URL + 'media/'
STATIC_URL = S3_URL + 'static/'
two_months = datetime.timedelta(days=61)
date_two_months_later = + two_months
expires = date_two_months_later.strftime("%A, %d %B %Y 20:00:00 GMT")

    'Expires': expires,
    'Cache-Control': 'max-age=%d' % (int(two_months.total_seconds()),),

04. Add Lambda expression for storages in file

from storages.backends.s3boto3 import S3Boto3Storage
StaticRootS3BotoStorage = lambda: S3Boto3Storage(location='static')
MediaRootS3BotoStorage  = lambda: S3Boto3Storage(location='media')

05. Add in

from import *

06. Great, you configured you aws s3. now final step collectstatic

python collectstatic

Get github django s3 code


I found the best way to set up Django AWS. in this article I explain what I set up in my real project. please comment if you have other better ways.

Related posts

How to work with django ajax requests ?

Sajal Mia

Understanding Django and django design principles

Tech Incent

How to create a virtual environment for Django?

Sajal Mia

How to render Django form individual fields manually?

Sajal Mia

Explained Django inline formset factory with example?

Sajal Mia

Django Send Email with AWS Simple Email Service(SES)

Sajal Mia


hublot big bang replica 23/11/2020 at 8:26 PM

very good material.

zortilo nrel 16/08/2021 at 7:09 PM

Greetings! Very helpful advice on this article! It is the little changes that make the biggest changes. Thanks a lot for sharing!

yard service near me 13/10/2021 at 7:16 PM

As soon as I detected this website I went on reddit to share some of the love with them.

togel singapore hari ini 21/10/2021 at 10:47 PM

I simply could not depart your website prior to suggesting that I really enjoyed the usual information a person provide for your visitors? Is going to be again ceaselessly in order to check out new posts

Branden 08/11/2021 at 11:53 PM

Its not my first time to pay a visit this website, i am browsing this site
dailly and obtain nice facts from here everyday.

Malcom 23/12/2021 at 4:25 AM

Everything is very open with a very clear explanation of the issues. It was definitely informative. Your website is very useful. Thank you for sharing!

Jarrod Houses 23/12/2021 at 8:22 PM

Appreciate you sharing, great blog article.Much thanks again. Really Great.

Hildegarde 26/12/2021 at 11:47 PM

Spot on with this write-up, I seriously think this web site needs a lot more attention. I’ll probably be returning to
read more, thanks for the information!

Arlette 28/12/2021 at 8:59 AM

Pretty! This was a really wonderful post. Many thanks for supplying this info.

Comments are closed.