How do I change configuration to upload only media files to S3 and keep static and assets file on the server ?
So far either server all or S3 all, both works but I need to separate them.
All examples I found are all files on S3.
Not claiming that I have any understanding on how static and file storage mechanism works, so help or guidance greatly appreciated.
BASE_DIR = Path(__file__).resolve().parent.parent
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
STATIC_ROOT = os.path.join(BASE_DIR, "assets")
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
FILE_UPLOAD_STORAGE = get_secret('FILE_UPLOAD_STORAGE')
if FILE_UPLOAD_STORAGE == "s3":
# Using django-storages
# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
AWS_S3_ACCESS_KEY_ID = get_secret("AWS_S3_ACCESS_KEY_ID")
AWS_S3_SECRET_ACCESS_KEY = get_secret("AWS_S3_SECRET_ACCESS_KEY")
AWS_STORAGE_BUCKET_NAME = get_secret("AWS_STORAGE_BUCKET_NAME")
AWS_S3_REGION_NAME = get_secret("AWS_S3_REGION_NAME")
AWS_S3_SIGNATURE_VERSION = get_secret("AWS_S3_SIGNATURE_VERSION")
czue
July 22, 2022, 11:31am
2
Take a look at this article for guidance. They also do static files, but if you skip those steps you can just do the media part.
Thanks for the tip czue, Its was a stupid mistake from my part, Ctrl + R fixed it , i guess Edge or Chrome cached the .js file and never followed changes Im making.
The above configuration actually working fine.
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
if FILE_UPLOAD_STORAGE == "s3":
print('Media storage: S3')
# Using django-storages
# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
AWS_S3_ACCESS_KEY_ID = get_secret("AWS_S3_ACCESS_KEY_ID")
AWS_S3_SECRET_ACCESS_KEY = get_secret("AWS_S3_SECRET_ACCESS_KEY")
AWS_STORAGE_BUCKET_NAME = get_secret("AWS_STORAGE_BUCKET_NAME")
AWS_S3_REGION_NAME = get_secret("AWS_S3_REGION_NAME")
AWS_S3_SIGNATURE_VERSION = get_secret("AWS_S3_SIGNATURE_VERSION")
# MEDIA_URL = '/media/'
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
STATIC_ROOT = os.path.join(BASE_DIR, "assets")
STATICFILES_STORAGE = "django.contrib.staticfiles.storage.StaticFilesStorage"
# MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
satyam
September 10, 2025, 3:44pm
4
Django has deprecated the DEFAULT_FILE_STORAGE, use: STORAGES = { “default”: { “BACKEND”: “storages.backends.s3boto3.S3Boto3Storage”, }, “staticfiles”: { “BACKEND”: “django.contrib.staticfiles.storage.StaticFilesStorage”, }, }