Django Static Files Not Loading from AWS S3 After Running collectstatic

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 = 'cats-gallery-amanda'

# 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'),
]

What I've Tried:

  1. Running collectstatic:

Command: python manage.py collectstatic Output says: 0 static files copied to 'static/', 176 unmodified.

  1. S3 Bucket Permissions:

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/*"
    }
  ]
}

  1. Checking Files in S3:

After running collectstatic, there are no files in the S3 bucket under static/.

  1. Attempted Solutions:
  • Verified that django-storages and boto3 are installed.
  • Double-checked AWS credentials.
  • Ensured that the S3 bucket is publicly accessible.
  • Tried manually uploading files to S3, and they work fine.

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!

Вернуться на верх