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

孤街浪徒 提交于 2019-12-08 04:43:08

问题


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

回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/41427837/django-wont-serve-static-files-from-amazon-s3-with-custom-domain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!