Django collecstatic requires STATIC_ROOT but setting STATIC_ROOT blocks upload to S3 [duplicate]
So I use S3 static storage in combination with Django to serve static files for a Zappa deploy. This all worked quite well for a long time until I recently upgraded to a new Django version.
Python 3.12.3
Django 5.1.1
It used to be, I could use: python manage.py collectstatic
To push my static files over to my S3 bucket. But, currently, that fails with this error:
django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
However, If I set a STATIC_ROOT, then instead of pushing to S3, it collects the static files locally.
You have requested to collect static files at the destination
location as specified in your settings:
/-local storage-/static
This is my settings.py:
# STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'templates'),
os.path.join(BASE_DIR, "static")
]
YOUR_S3_BUCKET = "static-bucket"
STATICFILES_STORAGE = "django_s3_storage.storage.StaticS3Storage"
AWS_S3_BUCKET_NAME_STATIC = YOUR_S3_BUCKET
# These next two lines will serve the static files directly
# from the s3 bucket
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % YOUR_S3_BUCKET
STATIC_URL = "https://%s/" % AWS_S3_CUSTOM_DOMAIN
The credentials and all are also in settings.py. It seems I a missing something, I need STATIC_ROOT to run collectstatic, but having STATIC_ROOT makes static collection be local.
Edit: Fixed the question, STATIC_ROOT was defined twice. Didn't change behavior unfortunately.
Edit: added information
STORAGES is not set it seems.
STATICFILES_STORAGE = "django_s3_storage.storage.StaticS3Storage" But my Django version is 5.1.1
I am using the django_s3_storage package yes, django_s3_storage and storages are both installed apps.
Solution: STATICFILES_STORAGE has been deprecated since 4.2 and removed in 5.1 Correct config becomes this:
# These next two lines will serve the static files directly
# from the s3 bucket
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % YOUR_S3_BUCKET
STATIC_URL = "https://%s/" % AWS_S3_CUSTOM_DOMAIN
STORAGES = {
'default': {
'BACKEND': 'storages.backends.s3boto3.S3Boto3Storage',
},
'staticfiles': {
'BACKEND': 'storages.backends.s3boto3.S3Boto3Storage',
'OPTIONS': {
'bucket_name': AWS_S3_BUCKET_NAME_STATIC,
},
},
}
Thank you @abdul-aziz-barkat