Django - S3: collectstatic does not create new folder, even with all permissions and write privileges
Cursor ai called it a "strange problem", so we're back to the original home of solutions.
I upgraded django from 2.2 to 5.2, and upgraded my storages, tinymce,.. etc modules along with it.
STORAGES = {
'staticfiles': {
'BACKEND': 'storages.backends.s3boto3.S3Boto3Storage',
'OPTIONS': {'location': 'static'},
},
'default': {
'BACKEND': 'storages.backends.s3boto3.S3Boto3Storage',
'OPTIONS': {'location': 'media'},
},
}
i added a new app after the upgrade, and everything worked well in development. The issue is, when i run collectstatic in production, the static and media files that have already been synced from previous deployments remain available, but some of the static and media subfolders/files from recent upgrades do not get uploaded - even as they are all have the same static and media root directory. How is it that django-storages can "pick and choose" the folders to create within an identified static or media directory?
i'm not sure what other code to include to explain this further, but if anyone needs to see something else, let me know and i'll add code snippets. Thanks.
Make sure your DEBUG = False
, Django stores the static files in the local files instead of uploading to the S3 while in development mode. If this is not the case, please hide your credentials or any private implementations and upload full code of settings.py
to make the problem more clear to understand. Your current implementation looks fine for django 4.2
and later versions.
Standard Implementation should look like this:
DEBUG = False
ALLOWED_HOSTS = ['my-instance.us-east-1.elasticbeanstalk.com', '127.0.0.1']
AWS_STORAGE_BUCKET_NAME = "my-s3-bucket"
AWS_S3_REGION_NAME = "us-east-1"
AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.s3.{AWS_S3_REGION_NAME}.amazonaws.com'
# Optional but recommended:
AWS_DEFAULT_ACL = None # Prevents "Access Denied" errors
AWS_QUERYSTRING_AUTH = False # Removes ?AWSAccessKeyId=... from URLs
MEDIA_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/'
# Configuration of default storages for Django >4.2 versions
STORAGES = {
"default" : {
"BACKEND" : "storages.backends.s3boto3.S3Boto3Storage",
},
"staticfiles" : {
"BACKEND" : "storages.backends.s3boto3.S3Boto3Storage"
}
}
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.1/howto/static-files/
STATIC_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/static/'
Also, make sure you have storages
in your INSTALLED_APPS
to function well. In my case, I was logged in with terminal using aws configure
(boto3
) due to which I didn't need AWS key and credentials here. But if you are not, make sure to include AWS credentials. Additionally, add your IP address
in your inbound rules of security group for EC2 that runs your Django application.