我已按照在我的应用程序中安装和启用 django-storages 和 boto 的所有说明将文件上传到 S3。(由于此应用程序旨在部署在 Heroku 上,因此我使用 virtualenv 来管理依赖项。)
对于本地开发,我已将所有凭据放入 ~/.bash_profile:
export AWS_ACCESS_KEY_ID=xxxx
export AWS_SECRET_ACCESS_KEY=yyyy
export S3_BUCKET_NAME=zzzz
所以,太好了,但接下来是:
> python manage.py shell
>>> import boto
>>> conn = boto.connect_s3()
>>> conn.get_all_buckets()
产生这些调试消息和这个错误:
2012-06-04 23:17:34,432 [DEBUG] boto: path=/
2012-06-04 23:17:34,432 [DEBUG] boto: auth_path=/
2012-06-04 23:17:34,433 [DEBUG] boto: Method: GET
2012-06-04 23:17:34,433 [DEBUG] boto: Path: /
2012-06-04 23:17:34,433 [DEBUG] boto: Data:
2012-06-04 23:17:34,433 [DEBUG] boto: Headers: {}
2012-06-04 23:17:34,433 [DEBUG] boto: Host: s3.amazonaws.com
2012-06-04 23:17:34,433 [DEBUG] boto: establishing HTTPS connection: host=s3.amazonaws.com, kwargs={'timeout': 2}
2012-06-04 23:17:34,433 [DEBUG] boto: Token: None
2012-06-04 23:17:34,436 [DEBUG] boto: StringToSign:
GET
Tue, 05 Jun 2012 03:17:34 GMT
2012-06-04 23:17:36,900 [DEBUG] boto: encountered SSLError exception, reconnecting
2012-06-04 23:17:36,901 [DEBUG] boto: establishing HTTPS connection: host=s3.amazonaws.com, kwargs={'timeout': 2}
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "../venv/lib/python2.7/site-packages/boto/s3/connection.py", line 346, in get_all_buckets
response = self.make_request('GET', headers=headers)
File "../venv/lib/python2.7/site-packages/boto/s3/connection.py", line 454, in make_request
override_num_retries=override_num_retries)
File "../venv/lib/python2.7/site-packages/boto/connection.py", line 838, in make_request
return self._mexe(http_request, sender, override_num_retries)
File "../venv/lib/python2.7/site-packages/boto/connection.py", line 803, in _mexe
raise e
SSLError: _ssl.c:484: The handshake operation timed out
我无法理解的是,使用相同的确切凭据,我可以使用 Mac 上的 GUI 浏览 S3 进行连接,而不会出现任何问题。我可以列出所有的桶。我可以进入桶 zzzz。我可以上传一个文件给它。我可以重新下载。我可以删除它。一切都很好。但无论我通过 Boto 尝试什么,它都会超时,没有进一步的评论。
为什么 boto 在我的本地环境中超时?
更新
我能够将以下范围缩小到:
>>> import httplib
>>> h1 = httplib.HTTPSConnection('s3.amazonaws.com')
>>> h1.request ("GET", "/")
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 958, in request
self._send_request(method, url, body, headers)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 992, in _send_request
self.endheaders(body)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 954, in endheaders
self._send_output(message_body)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 814, in _send_output
self.send(msg)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 776, in send
self.connect()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1161, in connect
self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 382, in wrap_socket
ciphers=ciphers)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 143, in __init__
self.do_handshake()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 306, in do_handshake
self._sslobj.do_handshake()
error: [Errno 60] Operation timed out
然而:
>>> h1 = httplib.HTTPSConnection('google.com')
>>> h1.request ("GET", "/")
>>> r1 = h1.getresponse()
>>> print r1.status, r1.reason
301 Moved Permanently
AWS 是什么让 httplib.HTTPSConnection 搞砸了?
(我知道上面的请求不是正确和完整的 AWS 请求,但我简化了它只是为了尝试从服务器获得响应。)