为什么这个脚本不能在s3中创建bucket?

2024-04-20 04:41:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经编写了一个python脚本来连接到amazons3服务器,但是在尝试创建bucket时似乎失败了(超时错误)。出于明显的原因,我省略了密钥和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)

这是我收到的错误消息:

文件“/系统/库/框架”/Python.framework/Versions/2.7/lib/Python2.7/httplib.py“,第941行,请求中 self.\u send_请求(方法、url、body、headers) 文件“/系统/库/框架”/Python.framework/Versions/2.7/lib/Python2.7/httplib.py“,第975行,在“发送请求”中 self.endheaders(正文) 文件“/系统/库/框架”/Python.framework/Versions/2.7/lib/Python2.7/httplib.py“,第937行,在结束标题中 self.\u send_输出(消息体) 文件“/系统/库/框架”/Python.framework/Versions/2.7/lib/Python2.7/httplib.py“,第797行,输入发送输出 自我发送(消息) 文件“/系统/库/框架”/Python.framework/Versions/2.7/lib/Python2.7/httplib.py“,第759行,在send中 自连接() 文件“/系统/库/框架”/Python.framework/Versions/2.7/lib/Python2.7/httplib.py“,第1140行,连接 自身超时, self.source_地址) 文件“/系统/库/框架”/Python.framework/Versions/2.7/lib/Python2.7/插座.py“,第571行,在create_connection中 引发错误 套接字超时:超时


Tags: 文件keypyimport框架awsidbucket
1条回答
网友
1楼 · 发布于 2024-04-20 04:41:09

您说您正在尝试创建一个bucket,但是get_bucket()方法没有创建bucket,它返回一个已有的bucket。如果要创建新的bucket,请改用create_bucket()。通常的方法是首先使用get_bucket()来查看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()的调用应返回现有的bucket或在正常操作中引发S3ResponseError。在

您从socket模块收到超时错误,这似乎表明您的网络设置有问题。你在代理服务器后面吗?你能对S3服务执行任何操作吗(例如,列出bucket中的键等)?在

相关问题 更多 >