我试图在亚马逊S3上托管我的Django静态和媒体文件,并且我一直遵循每个指南,但我仍然在部署时遇到S3ResponseError: 301 Moved Permanently
错误我的Elastic Beanstalk应用程序试图运行collectstatic
时。S3上的Django静态文件:S3ResponseError:301永久移动
我的S3正在工作,我可以访问其上的其他文件。我也将其设置为自定义域,因此您可以通过以下方式访问同一个文件:
- http://s3.condopilot.com.s3-eu-west-1.amazonaws.com/thumbs/big/3fca62e2150e8abec3f693a6eae8d2f79bb227fb.jpg
- https://s3-eu-west-1.amazonaws.com/s3.condopilot.com/thumbs/big/3fca62e2150e8abec3f693a6eae8d2f79bb227fb.jpg
- 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桶被设置在欧盟 - 西1。
我遇到了完全相同的问题。我还没有找到解决方案。 – Sam
我所有的桶都在北加州地区。但是,如果您创建了一个区域为美国标准的新存储区,则带点的存储区名称似乎可行。另一方面,如果存储桶名称中没有点,它将在没有设置调用格式的情况下工作。 – Sam