为什么这个脚本无法在s3中创建一个存储桶?

时间:2013-11-25 12:57:03

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

我已经编写了一个python脚本来连接到亚马逊s3服务器,但在尝试创建存储桶时出现故障(超时错误)。出于显而易见的原因,我省略了密钥和id密钥。任何人都可以看到这个脚本有什么问题吗?提前致谢

import boto
import sys, os
from boto.s3.key import Key
from boto.s3.connection import S3Connection
from boto.exception import S3ResponseError

LOCAL_PATH = '/Users/****/test'
aws_access_key_id = '****'
aws_secret_access_key = '****'
bucket_name = aws_access_key_id.lower() + '****'

class TimeoutException(Exception):
    pass

conn = boto.connect_s3(aws_access_key_id, aws_secret_access_key)
try:
    print "bucket name " + bucket_name;
    bucket = conn.get_bucket( bucket_name)
except TimeoutException:
    sys.exit("Connection timed out; this usually means you're offline.")
except S3ResponseError, exception_data:
    sys.exit(exception_data.error_message)

这是我收到的错误消息:

文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py”,第941行,请求中     self._send_request(方法,网址,正文,标题)   在_send_request中输入文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py”,第975行     self.endheaders(体)   文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py”,第937行,在endheaders中     self._send_output(MESSAGE_BODY)   在_send_output中输入文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py”,第797行     self.send(MSG)   文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py”,第759行,发送     self.connect()   文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py”,第1140行,在连接中     self.timeout,self.source_address)   在create_connection中输入文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py”,第571行     提出错误 socket.timeout:超时

1 个答案:

答案 0 :(得分:1)

您说您正在尝试创建存储桶,但get_bucket()方法不会创建存储桶,而是返回现有存储桶。如果要创建新存储桶,请改用create_bucket()。通常的做法是首先使用get_bucket()查看存储桶是否存在,如果存在,则调用create_bucket()

另外,我不明白这段代码的目的是什么:

try:
    print "bucket name " + bucket_name;
    bucket = conn.get_bucket( bucket_name)
except TimeoutException:
    sys.exit("Connection timed out; this usually means you're offline.")
except S3ResponseError, exception_data:
    sys.exit(exception_data.error_message)

TimeoutException类是您在本地创建的类,对get_bucket()的调用永远不会引发该异常,因为它对此一无所知。对get_bucket()的调用应返回现有存储桶或在正常操作中引发S3ResponseError。

您从socket模块收到超时错误这一事实似乎表明您的网络设置存在问题。你是代理服务器吗?您可以对S3服务执行任何操作(例如,桶中的列表键等)吗?