Storing Data with S3 using Python and Django
Published on October 09, 2025 by durga
Setting up Django with Amazon S3 involves configuring your Django project to use S3 for storing static and media files. This process typically utilizes the django-storages and boto3 libraries.
1. AWS S3 Setup:
-
Create an S3 Bucket:
Log in to your AWS Management Console, navigate to S3, and create a new S3 bucket. Choose a unique name and desired region. Configure public access settings based on your needs (e.g., allow public access for static files, restrict for sensitive media).
-
Create IAM User and Access Keys:
In AWS IAM, create a new user and grant it permissions to access your S3 bucket. Generate an Access Key ID and Secret Access Key for this user, as these will be used by Django to authenticate with S3.
2. Django Project Setup:
- Install Packages: Install
django-storagesandboto3in your Django project's virtual environment:
| pip install django-storages boto3 |
- Add
storagestoINSTALLED_APPS: In your Django project'ssettings.pyfile, add'storages'to yourINSTALLED_APPS:
| INSTALLED_APPS = [ # ... other apps 'storages', ] |
- Configure S3 Settings: Add the following configurations to
settings.py, replacing placeholders with your actual AWS credentials and bucket details:
| AWS_ACCESS_KEY_ID = 'your_access_key_id' AWS_SECRET_ACCESS_KEY = 'your_secret_access_key' AWS_STORAGE_BUCKET_NAME = 'your_bucket_name' AWS_S3_REGION_NAME = 'your_bucket_region' # e.g., 'us-east-1' AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com' # Optional, for custom domain AWS_S3_FILE_OVERWRITE = False # Prevents overwriting files with the same name |
- Configure Storage Backends: Define how Django should use S3 for static and media files within your
STORAGESdictionary insettings.py:
|
STORAGES = { MEDIA_URL = f"https://{AWS_S3_CUSTOM_DOMAIN}/media/" # Or just "/media/" if not using custom domain |
- Collect Static Files: Run
python manage.py collectstaticto gather your static files and upload them to your S3 bucket.
After these steps, your Django application will be configured to store and retrieve static and media files from your Amazon S3 bucket.