How to Set Up a Django Project with Django-Storages and Amazon S3: Managing Different Folders for Static and Media Files

As a data scientist or software engineer, you may often find yourself needing to segregate static and media files in your Django project. In this tutorial, we’ll be exploring an efficient way to organize your files on Amazon S3 using django-storages.

How to Set Up a Django Project with Django-Storages and Amazon S3: Managing Different Folders for Static and Media Files

As a data scientist or software engineer, you may often find yourself needing to segregate static and media files in your Django project. In this tutorial, we’ll be exploring an efficient way to organize your files on Amazon S3 using django-storages.

What are Django, Django-Storages, and Amazon S3?

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It simplifies the process of building better web apps more quickly and with less code.

Django-Storages is a collection of custom storage backends for Django. It allows you to manage how and where you store your files.

Amazon S3 (Simple Storage Service) is an object storage service that offers industry-leading scalability, data availability, security, and performance.

Step 1: Setting Up Your Django Project

Assuming you have a Django project ready, let’s first install django-storages and boto3, which is the Amazon Web Services (AWS) SDK for Python:

pip install django-storages boto3

Step 2: Configuring Django-Storages

Add ‘storages’ to your INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    # other apps
    'storages',
]

Then, set your AWS credentials and bucket name:

AWS_ACCESS_KEY_ID = 'your-access-key'
AWS_SECRET_ACCESS_KEY = 'your-secret-key'
AWS_STORAGE_BUCKET_NAME = 'your-bucket-name'

Step 3: Separating Media and Static Folders

Now, let’s create separate directories for static and media files. Add the following to your settings.py:

AWS_STATIC_LOCATION = 'static'
STATIC_URL = f'https://{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com/{AWS_STATIC_LOCATION}/'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

AWS_PUBLIC_MEDIA_LOCATION = 'media/public'
DEFAULT_FILE_STORAGE = 'your_project_name.storages_backends.PublicMediaStorage'

AWS_PRIVATE_MEDIA_LOCATION = 'media/private'
PRIVATE_FILE_STORAGE = 'your_project_name.storages_backends.PrivateMediaStorage'

This configuration will create a ‘static’ directory for your static files and separate ‘public’ and ‘private’ directories under ‘media’ for your media files.

Step 4: Creating Custom Storages

Under your project directory, create a new file named storages_backends.py:

from storages.backends.s3boto3 import S3Boto3Storage


class StaticStorage(S3Boto3Storage):
    location = 'static'
    default_acl = 'public-read'


class PublicMediaStorage(S3Boto3Storage):
    location = 'media/public'
    default_acl = 'public-read'
    file_overwrite = False


class PrivateMediaStorage(S3Boto3Storage):
    location = 'media/private'
    default_acl = 'private'
    file_overwrite = False
    custom_domain = False

The default_acl attribute sets the access control list policy for the files. For static and public media files, we want them to be publicly readable, hence ‘public-read’. For private media files, we set it to ‘private’. The file_overwrite attribute is set to False to prevent Django from overwriting files with the same name.

That’s it! You’ve successfully set up your Django project with Django-Storages and Amazon S3, and segregated your static and media files into different folders. This clean organization of files will help you manage your project better and ensure efficient data handling.


About Saturn Cloud

Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.