简体   繁体   中英

Django won't serve static files from Amazon S3 with custom domain

I did setup my Django project, DNS and bucket on Amazon S3 but python manage.py collectstatic and therefore also files uploaded manually won't works.

AWS S3 Settings:

Bucket name: files.domain.com

Bucket policy:

{
  "Id": "Policy1483363850641",
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "Stmt1483363848944",
      "Action": "s3:*",
      "Effect": "Allow",
      "Resource": "arn:aws:s3:::files.domain.com/*",
      "Principal": "*"
    }
  ]
}

DNS Settings:

files.domain.com -> CNAME -> files.domain.com.s3.amazonaws.com

Django Settings:

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
AWS_ACCESS_KEY_ID = 'XXXXXXXXXXXXXXXXXXXX'
AWS_SECRET_ACCESS_KEY = 'XxXxXXxXXXXXxxxXxxXXXxXxxXXXXXXXXxxxXxxx'
AWS_STORAGE_BUCKET_NAME = 'files.domain.com'
AWS_AUTO_CREATE_BUCKET = False
AWS_QUERYSTRING_AUTH = False
AWS_S3_SECURE_URLS = False
AWS_EXPIRY = 60 * 60 * 24 * 7
AWS_HEADERS = {
    'Cache-Control': six.b('max-age=%d, s-maxage=%d, must-revalidate' % (
        AWS_EXPIRY, AWS_EXPIRY))
}

MEDIA_URL = 'http://%s/' % AWS_STORAGE_BUCKET_NAME
STATICFILES_STORAGE = DEFAULT_FILE_STORAGE
STATIC_URL = MEDIA_URL

I included these lines on my Django settings and Amazon S3 with custom domain worked fine.

AWS_S3_CALLING_FORMAT = 'boto.s3.connection.OrdinaryCallingFormat'
AWS_S3_HOST = 's3-sa-east-1.amazonaws.com'
AWS_S3_CUSTOM_DOMAIN = AWS_STORAGE_BUCKET_NAME

The issue is related to using dots on the name. Try using:

AWS_S3_CALLING_FORMAT = boto.s3.connection.OrdinaryCallingFormat()

but I don't know if that still works as I personally just moved away from using dots on the STATICS bucket. I use a CDN anyway, so the S3 bucket name is irrelevant.

See https://github.com/boto/boto/issues/2836

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM