Django在S3ResponseError中的collectstatic结果:301永久移动

时间:2016-07-12 06:15:07

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

我正在运行一个将所有静态和媒体文件添加到Amazon S3的Django项目。我使用的是过去曾多次使用过的settings.py配置,可以用于其他多个项目(在下面发布)。

当我在自己的localhost上使用相同的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'

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

总结并重复自己。

  1. 我可以从localhost运行python manage.py collectstatic。我还验证了我可以使用collectstatic命令访问localhost在那里放置的S3实例上的文件。当我从具有相同设置的Elastic Beanstalk应用程序执行相同操作时,我收到错误。
  2. 我之前在其他项目中使用了相同的设置而没有任何问题。唯一改变的是存储桶名称。
  3. 我在EB服务器上运行collectstatic时遇到的错误是S3ResponseError: 301 Moved Permanently
  4. 完整追溯:

    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)
    

1 个答案:

答案 0 :(得分:4)

我发现问题是因为我使用的Django模块django-storages的更新。在更新中,他们通过以下方式分配默认值S3BotoStorage.host

host = setting('AWS_S3_HOST', S3Connection.DefaultHost)

主机会自动设置为s3.amazonaws.com,并且因为它在创建与boto(AWS官方python-package)的连接时已经拥有主机集,所以它永远不会使用{我在DefaultHost = "s3-eu-west-1.amazonaws.com" - 课程中添加了{1}}。

解决此问题的方法是在EuropeConnection中设置django-storages设置值AWS_S3_HOST(新建)。

settings.py