Skip to content

Store Django static assets with Amazon S3

DJANGO NOV 20 ARTICULO NO TEXT

For some projects we serve a lot of images, videos, and documents. This can overload your site and make it slower because it is requesting too many resources and processes at the same time. Due to this situation, it is a good practice to store them in a secure place like Amazon S3.

What is Amazon S3?

“Amazon S3 has a simple web services interface that you can use to store and retrieve any amount of data, at any time, from anywhere on the web. It gives any developer access to the same highly scalable, reliable, fast, inexpensive data storage infrastructure that Amazon uses to run its global network of websites.”

What Dependencies We Will Need?

Boto3

According to their documentation “Boto is the Amazon Web Services (AWS) SDK for Python. It enables Python developers to create, configure, and manage AWS services, such as EC2 and S3. Boto provides an easy to use, object-oriented API, as well as low-level access to AWS services.”

django-storages

Is a project that provides a variety of storage backends in a single library. With this library, we just need to set the configuration variables and then the library will do the hard work for us, making easy to use external storage.

Configure Amazon s3

With a valid AWS account, create a user that has the permissions to access to our assets:

Find IAM link:

IAM: Identity and Access Management (IAM) enables you to manage access to AWS services and resources securely

Create the user with programmatic access:

Create the group with the correct permissions:

Write group name and select Amazons3FullAccess, then create the group:

Save the data for later configuration:

Now we are going to create the bucket.

Bucket: is a public cloud storage resource


After finish the creation, you will see the bucket below.

Now let’s configure the Django project:

Enable your environment and install:

pip install boto3
pip install django-storages

Then add storage in your settings:

INSTALLED_APPS = (
    ...
    'storages',
    ...
)

Setup Static Assets, for more configuration attributes, check  in the official site:

AWS_ACCESS_KEY_ID = 'AKXXXXXXXXXOOAHL'
AWS_SECRET_ACCESS_KEY = 'qdnknt66ZsdrR/123WTB//+DeiHfEt54'
AWS_STORAGE_BUCKET_NAME = 'test-bucket'
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
AWS_S3_OBJECT_PARAMETERS = {
    'CacheControl': 'max-age=86400',
}
AWS_LOCATION = 'static'
AWS_QUERYSTRING_AUTH = False
STATIC_URL = "https://s3.amazonaws.com/%s/static/" % AWS_STORAGE_BUCKET_NAME
AWS_DEFAULT_ACL = None 
STATICFILES_DIRS = [str(APPS_DIR.path("static"))]
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

Remember to don’t leave the keys in this file, you should use environment variables.

Then we need to run collectstatic in order to storage our current assets remotely:

python manage.py collectstatic

Now we can see the static files rendering from amazon s3:

There are huge benefits to use amazon s3 instead of using our own servers to allocate assets and media. Amazon provides secure storage, it is simple to use and we can use as much storage as we want and as long as we are willing to pay. The availability of your images is something you may not worry anymore, and the most important is low cost, you only pay for what you use.