【发布时间】:2019-08-07 10:16:34
【问题描述】:
我遇到了一个问题,即存储在 Europe/London S3 存储桶上的资产的 URL 没有正确生成。例如:
https://s3.amazonaws.com/mybucket/static/wagtailadmin/css/normalize.css
导致浏览器出现此类错误:
跨域读取阻止 (CORB) 阻止了 MIME 类型 application/xml 的跨域响应。详情请参阅。
当您尝试访问该网址时:
<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>PermanentRedirect</Code>
<Message>The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.</Message>
<Endpoint>mybucket.s3.amazonaws.com</Endpoint>
<Bucket>mybucket</Bucket>
<RequestId>4ER2572A129386F7</RequestId>
<HostId>+l38ZBh/hDscROXzeWdNfldQtcQm1ZPVq4sNZAZKQKwGHLv7MDRW4H0sf0I3pijD1T0j4oSE6E=</HostId>
</Error>
文件的正确 URL 是 https://mybucket.s3.amazonaws.com/static/wagtailadmin/css/normalize.css。
我的项目使用django-cookiecutter生成的原版设置:
# STORAGES
# ------------------------------------------------------------------------------
INSTALLED_APPS += ['storages'] # noqa F405
AWS_ACCESS_KEY_ID = env('DJANGO_AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = env('DJANGO_AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = env('DJANGO_AWS_STORAGE_BUCKET_NAME')
AWS_QUERYSTRING_AUTH = False
_AWS_EXPIRY = 60 * 60 * 24 * 7
AWS_S3_OBJECT_PARAMETERS = {
'CacheControl': f'max-age={_AWS_EXPIRY}, s-maxage={_AWS_EXPIRY}, must-revalidate',
}
AWS_DEFAULT_ACL = 'public-read' # TODO - not sure if this is ideal
# STATIC
# ------------------------
STATICFILES_STORAGE = 'config.settings.production.StaticRootS3Boto3Storage'
STATIC_URL = f'https://{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com/static/'
# MEDIA
# ------------------------------------------------------------------------------
from storages.backends.s3boto3 import S3Boto3Storage # noqa E402
class StaticRootS3Boto3Storage(S3Boto3Storage):
location = 'static'
class MediaRootS3Boto3Storage(S3Boto3Storage):
location = 'media'
file_overwrite = False
DEFAULT_FILE_STORAGE = 'config.settings.production.MediaRootS3Boto3Storage'
MEDIA_URL = f'https://{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com/media/'
【问题讨论】: