更改为boto get_bucket后,连接已关闭来自Amazon S3的错误

时间:2012-03-07 10:25:16

标签: python amazon-web-services amazon-s3 boto

昨天我开始使用带有boto的Amazon S3,我设置了一个python脚本来创建一个存储桶并在目录中上传图像。我跑了几次,似乎没问题。

我不想经常创建存储桶,所以我将我的脚本改编为get_bucket:

import os
import boto

s3 = boto.connect_s3()
bucket = s3.get_bucket('images')

# Run Selenium test process to create images


for root, dirs, files in os.walk(imagesPath):
for name in files:       
   filename = os.path.join(root, name)
   key = bucket.new_key('{0}/{1}/{2}'.format(revisionNumber, images_process, name)) 
   print "Uploading " + filename + " to Amazon S3"
   key.set_contents_from_filename(filename)
   key.set_acl('public-read')

错误来自第key.set_contents_from_filename(filename)

这段代码基本上是来自S3入门指南的示例代码,我已将其放入循环中。我认为问题在于它第一次做的很好,但是不足以应对覆盖等。我是否必须提供boto而不仅仅是要上传的文件名?

脚本的输出是:

 [exec] Uploading images/Add_Employer_Process/20227M/1.png to Amazon S3
 [exec] Traceback (most recent call last):
 [exec]   File "addEmployerProcess.py", line 121, in <module>
 [exec]     k.set_contents_from_filename(filename)
 [exec]   File "C:\Python26\lib\site-packages\boto\s3\key.py", line 969, inset_contents_from_filename
 [exec]     encrypt_key=encrypt_key)
 [exec]   File "C:\Python26\lib\site-packages\boto\s3\key.py", line 902, inset_contents_from_file
 [exec]     size=size)
 [exec]   File "C:\Python26\lib\site-packages\boto\s3\key.py", line 660, insend_file
 [exec]     query_args=query_args)
 [exec]   File "C:\Python26\lib\site-packages\boto\s3\connection.py", line 449, in make_request
 [exec]     override_num_retries=override_num_retries)
 [exec]   File "C:\Python26\lib\site-packages\boto\connection.py", line 829, in make_request
 [exec]     return self._mexe(http_request, sender, override_num_retries)
 [exec]   File "C:\Python26\lib\site-packages\boto\connection.py", line 794, in _mexe
 [exec]     raise e
 [exec] socket.error: [Errno 10054] An existing connection was forcibly closed by the remote host

我是所有这一切的新手,包括python,所以任何帮助将不胜感激:)

1 个答案:

答案 0 :(得分:1)

您的代码看起来很好,尽管一个小的,无关的建议是在您编写文件时在文件上设置ACL策略,而不是单独的步骤,如下所示:

key.set_contents_from_filename(filename, policy='public-read')

我在本地尝试了你的代码,它运行得很好。我不确定你为什么会得到套接字异常,但真正奇怪的是boto应该捕获该异常并自动重试请求。

获取一些调试日志记录会很有用。您可以启用完整的调试日志记录到控制台,如下所示:

import boto
boto.set_stream_logger('foo')
...
s3 = boto.connect_s3(debug=2)

这可能会提供有关正在发生的事情的更多信息。