django - 对静态文件使用s3,boto,django-storage时,对等方重置连接

标签 django amazon-s3 static-files django-storage

我试图切换为使用亚马逊S3来为django项目托管我们的静态文件。我正在使用django,boto,django-storage和django-compressor。当我在开发服务器上运行collect static时,出现错误

socket.error: [Errno 104] Connection reset by peer 

我所有静态文件的大小为74MB,这似乎并不太大。有没有人看过这个,或者有任何调试提示?

这是完整的痕迹。
Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 196, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 232, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 371, in handle
    return self.handle_noargs(**options)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 163, in handle_noargs
    collected = self.collect()
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 113, in collect
    handler(path, prefixed_path, storage)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 303, in copy_file
    self.storage.save(prefixed_path, source_file)
  File "/usr/local/lib/python2.7/dist-packages/django/core/files/storage.py", line 45, in save
    name = self._save(name, content)
  File "/usr/local/lib/python2.7/dist-packages/storages/backends/s3boto.py", line 392, in _save
    self._save_content(key, content, headers=headers)
  File "/usr/local/lib/python2.7/dist-packages/storages/backends/s3boto.py", line 403, in _save_content
    rewind=True, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/boto/s3/key.py", line 1222, in set_contents_from_file
    chunked_transfer=chunked_transfer, size=size)
  File "/usr/local/lib/python2.7/dist-packages/boto/s3/key.py", line 714, in send_file
    chunked_transfer=chunked_transfer, size=size)
  File "/usr/local/lib/python2.7/dist-packages/boto/s3/key.py", line 890, in _send_file_internal
    query_args=query_args
  File "/usr/local/lib/python2.7/dist-packages/boto/s3/connection.py", line 547, in make_request
    retry_handler=retry_handler
  File "/usr/local/lib/python2.7/dist-packages/boto/connection.py", line 966, in make_request
    retry_handler=retry_handler)
  File "/usr/local/lib/python2.7/dist-packages/boto/connection.py", line 927, in _mexe
    raise e
socket.error: [Errno 104] Connection reset by peer

更新:我没有如何调试此错误的答案,但此后才停止发生,这使我认为它可能与S3上的某些内容有关。

最佳答案

tl; dr

如果您的存储桶不在默认区域中,则需要告诉Boto要连接到哪个区域,例如如果您的存储桶位于us-west-2中,则需要在settings.py中添加以下行:

 AWS_S3_HOST = 's3-us-west-2.amazonaws.com'

详细说明:

这不是权限问题,您不应将存储桶权限设置为“已认证的用户”。

如果您在默认区域以外的区域中创建存储桶,则会发生此问题(在我的情况下,我使用的是us-west-2)。

如果您不使用默认区域并且不告诉boto存储桶位于哪个区域,boto将连接到默认区域,并且S3将以307重定向回复到存储桶所属的区域。

不幸的是,由于boto中的这个错误:

https://github.com/boto/boto/issues/2207

如果307回复在boto完成上传文件之前到达,则boto将看不到重定向,并将继续上传到默认区域。
最终,S3关闭套接字,导致“对等方重置连接”。

这是一种竞争条件,它取决于要上传的对象的大小和Internet连接的速度,这说明了为什么它随机发生。

OP一段时间后停止看到错误的可能原因有两个:
- he later created a new bucket in the default region and the problem went away by itself. 
- he started uploading only small files, which are fast enough to be fully uploaded by the time S3 replies with 307

关于django - 对静态文件使用s3,boto,django-storage时,对等方重置连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20436705/

相关文章:

json - Django/PostgresQL jsonb (JSONField) - 将选择和更新转换为一个查询

python - Heroku : "Please supply the ENGINE value" 中的 Django 错误

python - 如何在 django-tables2 中使用基于类的 View 控制分页?

amazon-web-services - JENKINS 上的 AWS CLI 命令

docker - 从 jwilder/nginx-proxy 提供静态文件

c# - asp.net core 上的静态文件

mysql - Django ORM : How to filter in an annotation with Count() (subquery) without rawsql

java - Amazon S3 GET 对象

php - Amazon S3 删除标记

ruby-on-rails - Rails 3.1、 unicorn 和 Apache : static files