Managing static files like CSS and JavaScript, as well as media uploads such as user photos, can quickly become a headache as your Django app grows. Storing these files locally might seem convenient at first, but it often leads to scalability and performance bottlenecks. This is where AWS S3 (Simple Storage Service) comes to the rescue.
AWS S3 is a reliable, highly scalable, and cost-effective object storage service from Amazon Web Services. It’s designed to store and retrieve any amount of data, anytime, from anywhere on the web. By offloading your Django app’s static and media files to S3, you not only free up server resources but also benefit from S3's global infrastructure, ensuring fast and secure file delivery to users worldwide.
In this guide, we’ll walk through integrating AWS S3 with Django step by step, helping you transform file management into a seamless, scalable experience.
In this guide, we’ll:
Before you start, ensure you have:
Navigate to S3 and click Create bucket.
Bucket Name: Use a globally unique name, e.g., mydjangotestbucket
.
us-east-1
.Uncheck the Block Public Access settings.
Click Create bucket.
Click on your bucket name and go to the Permissions tab.
Add a bucket policy to allow the S3 bucket to serve files:
*
(allow all).s3:GetObject
.Resource: Your bucket's ARN.
Generate and copy the policy, then paste it into the Bucket Policy section.
Click your account name on the top-right of the AWS Management Console.
Go to Security Credentials > Access Keys.
Use the sample project to focus on the integration with AWS S3.
Clone the repository:
git clone https://github.com/charlesu49/django_s3_demo.git
cd django_s3_demo
Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
Install dependencies:
pip install -r requirements.txt
Configure your .env
file with:
AWS_ACCESS_KEY_ID=your_access_key_id
AWS_SECRET_ACCESS_KEY=your_secret_access_key
AWS_STORAGE_BUCKET_NAME=your_bucket_name
Apply migrations and start the server:
python manage.py migrate
django-storages
django-storages
and boto3
are included in the requirements file.
Update your settings.py
:
Add 'storages'
to your installed apps then add the following AWS configurations.
import os
from dotenv import load_dotenv
load_dotenv()
AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.getenv('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = os.getenv('AWS_STORAGE_BUCKET_NAME')
AWS_S3_REGION_NAME = 'us-east-1'
AWS_S3_CUSTOM_DOMAIN = f"{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com"
AWS_DEFAULT_ACL = None
AWS_S3_FILE_OVERWRITE = False
AWS_S3_VERIFY = True
AWS_S3_OBJECT_PARAMETERS = {"CacheControl": "max-age=86400"}
STORAGES = {
"default": {"BACKEND": "storages.backends.s3boto3.S3StaticStorage"},
"staticfiles": {"BACKEND": "storages.backends.s3boto3.S3StaticStorage"},
}
STATIC_URL = f"https://{AWS_S3_CUSTOM_DOMAIN}/static/"
MEDIA_URL = f"https://{AWS_S3_CUSTOM_DOMAIN}/media/"
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
Upload static files:
python manage.py collectstatic
Create a superuser:
python manage.py createsuperuser
Start the development server:
python manage.py runserver
Upload a file and verify it is served from S3.
Check the S3 console to confirm the file exists.
You’ve successfully integrated AWS S3 with Django for managing static and media files. This setup ensures your app is scalable and files are served reliably.
Sign in to add to the conversation