Mar*_*ind 3 django amazon-s3 amazon-web-services
我正在运行一个 Django 项目,该项目将所有静态文件和媒体文件添加到 Amazon S3。我正在使用我过去多次使用过的 settings.py 配置,该配置适用于多个其他项目(下面发布)。
当我在自己的本地主机上使用相同的 settings.py 文件时。我可以python manage.py collectstatic --noinput正常运行,它会将所有静态文件上传到 Amazon S3。但是,当我将项目放在 Elastic Beanstalk 应用程序服务器上并使用相同的设置运行相同的命令时,我得到了S3ResponseError: 301 Moved Permanently.
我的设置是:
AWS_STORAGE_BUCKET_NAME = 's3.cupcard.com'
AWS_S3_CUSTOM_DOMAIN = AWS_STORAGE_BUCKET_NAME
AWS_S3_CALLING_FORMAT = 'boto.s3.connection.OrdinaryCallingFormat'
AWS_S3_SECURE_URLS = False
AWS_S3_URL_PROTOCOL = 'http:' # Replace with 'https:' if you're not using http.
STATICFILES_LOCATION = 'static'
STATIC_URL = "http://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION)
STATICFILES_STORAGE = 'lindshop.utils.custom_storages.CachedS3BotoStorage'
MEDIAFILES_LOCATION = 'media'
MEDIA_URL = "http://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, MEDIAFILES_LOCATION)
DEFAULT_FILE_STORAGE = 'lindshop.utils.custom_storages.MediaStorage'
Run Code Online (Sandbox Code Playgroud)
看起来lindshop.utils.custom_storages.CachedS3BotoStorage像这样:
class EuropeConnection(S3Connection):
DefaultHost = "s3-eu-west-1.amazonaws.com"
class CachedS3BotoStorage(S3BotoStorage):
connection_class = EuropeConnection
location = settings.STATICFILES_LOCATION
def __init__(self, *args, **kwargs):
super(CachedS3BotoStorage, self).__init__(*args, **kwargs)
self.local_storage = get_storage_class(
"compressor.storage.CompressorFileStorage")()
def save(self, name, content):
non_gzipped_file_content = content.file
name = super(CachedS3BotoStorage, self).save(name, content)
content.file = non_gzipped_file_content
self.local_storage._save(name, content)
return name
Run Code Online (Sandbox Code Playgroud)
总结并重复一遍。
python manage.py collectstatic从本地主机运行。我还验证了我可以访问本地主机使用命令放置在其中的 S3 实例上的文件collectstatic。当我使用相同的设置从 Elastic Beanstalk 应用程序执行相同操作时,出现错误。collectstatic在 EB 服务器上运行时出现的错误是S3ResponseError: 301 Moved Permanently。完整回溯:
caused by: Traceback (most recent call last):
File "/opt/python/run/venv/bin/django-admin.py", line 5, in <module>
management.execute_from_command_line()
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 176, in handle
collected = self.collect()
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 107, in collect
handler(path, prefixed_path, storage)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 325, in copy_file
if not self.delete_file(path, prefixed_path, source_storage):
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 238, in delete_file
if self.storage.exists(prefixed_path):
File "/opt/python/run/venv/local/lib/python2.7/site-packages/storages/backends/s3boto.py", line 446, in exists
return k.exists()
File "/opt/python/run/venv/local/lib/python2.7/site-packages/boto/s3/key.py", line 539, in exists
return bool(self.bucket.lookup(self.name, headers=headers))
File "/opt/python/run/venv/local/lib/python2.7/site-packages/boto/s3/bucket.py", line 143, in lookup
return self.get_key(key_name, headers=headers)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/boto/s3/bucket.py", line 193, in get_key
key, resp = self._get_key_internal(key_name, headers, query_args_l)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/boto/s3/bucket.py", line 231, in _get_key_internal
response.status, response.reason, '')
boto.exception.S3ResponseError: S3ResponseError: 301 Moved Permanently
(ElasticBeanstalk::ExternalInvocationError)
Run Code Online (Sandbox Code Playgroud)
我发现问题是因为django-storages我使用的 Django 模块更新了。在更新中,他们S3BotoStorage.host通过以下方式分配添加了默认值:
host = setting('AWS_S3_HOST', S3Connection.DefaultHost)
Run Code Online (Sandbox Code Playgroud)
发生的情况是,主机被自动设置为,并且因为它在与(AWS 官方 python-package)s3.amazonaws.com创建连接时已经设置了一个主机,所以它永远不会使用我在我的类中添加的。botoDefaultHost = "s3-eu-west-1.amazonaws.com"EuropeConnection
解决这个问题的方法是AWS_S3_HOST在你的settings.py.
AWS_S3_HOST = "s3-eu-west-1.amazonaws.com"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
656 次 |
| 最近记录: |