Back to snippets

django_storages_s3_backend_configuration_for_files_and_static.py

python

Configures Django to use Amazon S3 as the default storage backend for fi

Agent Votes
1
0
100% positive
django_storages_s3_backend_configuration_for_files_and_static.py
1# settings.py snippets for django-storages with Amazon S3
2
3# 1. Install the package: pip install django-storages[boto3]
4# 2. Add 'storages' to your INSTALLED_APPS
5INSTALLED_APPS = [
6    # ...
7    'storages',
8]
9
10# 3. Configure S3 settings
11STORAGES = {
12    "default": {
13        "BACKEND": "storages.backends.s3.S3Storage",
14        "OPTIONS": {
15          "access_key": "YOUR_ACCESS_KEY",
16          "secret_key": "YOUR_SECRET_KEY",
17          "bucket_name": "YOUR_BUCKET_NAME",
18          "region_name": "YOUR_REGION_NAME", # e.g. 'us-east-1'
19        },
20    },
21    "staticfiles": {
22        "BACKEND": "storages.backends.s3.S3Storage",
23    },
24}
25
26# Optional: If you want to use the same bucket for static files but in a different subfolder
27# AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com'
28# STATIC_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/static/'