django-storages在S3中生成对资产的错误URL

时间:2019-03-16 14:17:35

标签: django amazon-s3

我遇到了一个问题,即未正确生成存储在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>

文件的正确网址为 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/'

1 个答案:

答案 0 :(得分:0)

我通过添加

解决了该问题

AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com'

进入我的设置。注意,看来STATIC_URL django-storages 忽略了。