【问题标题】:Django Static Files on S3: S3ResponseError: 301 Moved PermanentlyS3 上的 Django 静态文件:S3ResponseError: 301 永久移动
【发布时间】:2015-07-10 08:47:03
【问题描述】:

我正在尝试在 Amazon S3 上托管我的 Django 静态和媒体文件,并且我一直在遵循那里的所有指南,但是当我的 Elastic Beanstalk 应用程序尝试部署时,我最终仍然收到S3ResponseError: 301 Moved Permanently 错误运行collectstatic

我的 S3 正在工作,我可以访问上面的其他文件。我还将它设置为自定义域,以便您可以通过以下方式访问同一文件:

  1. http://s3.condopilot.com.s3-eu-west-1.amazonaws.com/thumbs/big/3fca62e2150e8abec3f693a6eae8d2f79bb227fb.jpg
  2. https://s3-eu-west-1.amazonaws.com/s3.condopilot.com/thumbs/big/3fca62e2150e8abec3f693a6eae8d2f79bb227fb.jpg
  3. http://s3.condopilot.com/thumbs/big/3fca62e2150e8abec3f693a6eae8d2f79bb227fb.jpg

这是我想使用的第三个选项,但我也尝试了其他选项。在下面的设置中有和没有https://

我的设置文件如下所示

#settings.py file
AWS_ACCESS_KEY_ID = 'XXX'
AWS_SECRET_ACCESS_KEY = 'XXX'
AWS_HEADERS = { 
    'Expires': 'Thu, 31 Dec 2099 20:00:00 GMT',
    'Cache-Control': 'max-age=94608000',
}
AWS_STORAGE_BUCKET_NAME = 's3.condopilot.com'
# I have also tried setting AWS_S3_CUSTOM_DOMAIN to the following:
# - "s3-eu-west-1.amazonaws.com/%s/" % AWS_STORAGE_BUCKET_NAME
# - "s3-eu-west-1.amazonaws.com/%s" % AWS_STORAGE_BUCKET_NAME
# - "s3.condopilot.com"
AWS_S3_CUSTOM_DOMAIN = "%s.s3-eu-west-1.amazonaws.com" % AWS_STORAGE_BUCKET_NAME
AWS_S3_CALLING_FORMAT = 'boto.s3.connection.OrdinaryCallingFormat'
AWS_S3_SECURE_URLS = False # Tried both True and False
AWS_S3_URL_PROTOCOL = 'http' # Tried with and without

STATICFILES_LOCATION = 'static'
STATIC_URL = "http://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION)
STATICFILES_STORAGE = 'custom_storages.StaticStorage'

MEDIAFILES_LOCATION = 'media'
MEDIA_URL = "http://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, MEDIAFILES_LOCATION)
DEFAULT_FILE_STORAGE = 'custom_storages.MediaStorage'

我之所以拥有AWS_S3_CALLING_FORMAT = 'boto.s3.connection.OrdinaryCallingFormat' 是因为没有它我会收到以下错误: ssl.CertificateError: hostname 's3.condopilot.com.s3.amazonaws.com' doesn't match either of '*.s3.amazonaws.com', 's3.amazonaws.com'。我在网上找到的有关该错误的所有建议都表明,当存储桶名称包含点时,应使用 OrdinaryCallingFormat,例如 s3.condopilot.com

我的自定义存储看起来像这样

#custom_storages.py
from django.conf import settings
from storages.backends.s3boto import S3BotoStorage

class StaticStorage(S3BotoStorage):
    location = settings.STATICFILES_LOCATION

class MediaStorage(S3BotoStorage):
    location = settings.MEDIAFILES_LOCATION

是的,我的 S3 存储桶设置在 eu-west-1 中。

【问题讨论】:

  • 我遇到了完全相同的问题。我还没有找到解决办法。
  • 我所有的存储桶都在北加州地区。但是,如果您创建了一个区域为美国标准的新存储桶,则带有点的存储桶名称似乎可以工作。另一方面,如果存储桶名称中没有点,则无需设置调用格式。

标签: django amazon-web-services amazon-s3


【解决方案1】:

我认为您不需要在 URL 中设置区域 S3,如果您使用的是 django-storage,则将此应用替换为 django-storages-redux。您不需要 custom_storages.py 文件。

保持简单。够了。

from django.utils import six

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
AWS_ACCESS_KEY_ID = 'XXXXXXXXXXXXXXXX'
AWS_SECRET_ACCESS_KEY = 'XxXxXxXxXxXxXxXxXxXxXxXxXxXxxXxX'
AWS_STORAGE_BUCKET_NAME = 'bucket-name'
AWS_AUTO_CREATE_BUCKET = False
AWS_QUERYSTRING_AUTH = 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 = 'https://%s.s3.amazonaws.com/' % AWS_STORAGE_BUCKET_NAME
STATICFILES_STORAGE = DEFAULT_FILE_STORAGE
STATIC_URL = MEDIA_URL

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-21
    • 2017-04-28
    • 2019-11-04
    • 2012-07-10
    • 2013-09-26
    • 2019-10-14
    • 2018-06-18
    • 2018-06-23
    相关资源
    最近更新 更多