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

jke*_*esh 22 django amazon-s3 static-files django-storage

我正在尝试切换到使用amazon s3来托管我们的django项目的静态文件.我正在使用django,boto,django-storage和django-compressor.当我在我的开发服务器上运行collect static时,我收到了错误

socket.error: [Errno 104] Connection reset by peer 
Run Code Online (Sandbox Code Playgroud)

我所有静态文件的大小都是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
Run Code Online (Sandbox Code Playgroud)

更新:我没有如何调试此错误的答案,但后来这个停止发生,这让我觉得它可能与S3上的某些事情有关.

Luc*_*lli 16

TL;博士

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

 AWS_S3_HOST = 's3-us-west-2.amazonaws.com'
Run Code Online (Sandbox Code Playgroud)

很长的解释:

这不是权限问题,您不应将存储桶权限设置为"经过身份验证的用户".

如果您在一个非默认区域(在我的情况下,我使用us-west-2)中创建您的存储桶,则会出现此问题.

如果您不使用默认区域,并且未告知boto您的存储区所在的区域,则boto将连接到默认区域,S3将使用307重定向进行回复,以指向存储区所属的区域.

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

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

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

这是一种竞争条件,取决于上传对象的大小和互联网连接的速度,这解释了为什么它会随机发生.

一段时间后,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
Run Code Online (Sandbox Code Playgroud)


pit*_*ide 5

这是第一次创建新存储桶时会出现的问题,您必须等待几小时或几分钟才能开始上传。我不知道为什么 s3 会这样。为了证明尝试创建一个新存储桶,将您的 django 存储指向它,当您尝试从 django 项目上传任何内容时,您会看到它显示连接对等重置,但等待几个小时或几分钟再试一次它会起作用。重复同样的步骤,看看。


t_i*_*_io -1

您必须将存储桶权限设置为Authenticated Users列表+上传/删除或者您可以在亚马逊的 IAM 部分创建特定用户并仅为该特定用户设置访问权限

前段时间这对我有帮助:Setup S3 for Django

  • 实际上,我认为您不应该使用经过身份验证的用户列表 - 这使得拥有 Amazon S3 帐户的任何人都可以使用该列表,而不仅仅是您帐户中的用户。(http://www.bucketexplorer.com/documentation/amazon-s3--access-control-list-acl-overview.html) (2认同)