I'm working on a Django project where I want to store and serve static files using AWS S3. I have followed the usual configuration steps and set up my settings.py to use S3 for static files. However, after running collectstatic, my static files are not loading, and I can't access them through the S3 bucket.
Here's a summary of what I've done so far:
Configuration in settings.py:
# AWS Credentials
AWS_ACCESS_KEY_ID = 'my-access-key'
AWS_SECRET_ACCESS_KEY = 'my-secret-key'
AWS_STORAGE_BUCKET_NAME = 'my-bucket'
# S3 Static File Settings
AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com'
AWS_DEFAULT_ACL = 'public-read'
AWS_LOCATION = 'static'
AWS_QUERYSTRING_AUTH = False
AWS_S3_OBJECT_PARAMETERS = {
'CacheControl': 'max-age=86400',
}
# Static Files Settings
STATIC_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/static/'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
# Directories
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'setup/static'),
]
Command: python manage.py collectstatic Output says: 0 static files copied to 'static/', 176 unmodified.
I’ve set the bucket policy to allow public access:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::cats-gallery-amanda/static/*"
}
]
}
After running collectstatic, there are no files in the S3 bucket under static/.
What Am I Missing?
I can't figure out why collectstatic isn’t uploading the static files to the S3 bucket. Do I need to configure something else? How can I get the static files to upload and serve correctly from S3?
Any advice or troubleshooting tips would be appreciated!
I replaced
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
with
STORAGES = {
"default": {
"BACKEND" : "storages.backends.s3boto3.S3StaticStorage",
},
"staticfiles": {
"BACKEND" : "storages.backends.s3boto3.S3StaticStorage",
},
}
and it worked fine.