为啥此脚本无法在 s3 中创建存储桶?

Posted

技术标签:

【中文标题】为啥此脚本无法在 s3 中创建存储桶?【英文标题】:why does this script fail to create a bucket in s3?为什么此脚本无法在 s3 中创建存储桶? 【发布时间】:2013-12-10 05:13:10 【问题描述】:

我已经编写了一个 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 行 自我发送(味精) 文件“/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) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py”,第 571 行,在 create_connection 提出错误 socket.timeout: 超时

【问题讨论】:

【参考方案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 服务执行任何操作(例如列出存储桶中的键等)吗?

【讨论】:

我在创建连接后尝试过这个:bucket = conn.create_bucket(bucket_name);但我收到以下错误消息未指定的位置约束与此请求发送到的区域特定端点不兼容。到现在都无法进行操作。 好的,我已经弄清楚了上述情况,我没有指定位置,但现在我收到了拒绝访问的消息:消息>拒绝访问

以上是关于为啥此脚本无法在 s3 中创建存储桶?的主要内容,如果未能解决你的问题,请参考以下文章

无法在特定区域中创建s3存储桶

使用脚本在 Amazon S3 存储桶内创建子目录

无法在 cloudformation 中创建带有事件的 s3 存储桶,以连接到它

使用 CloudFormation 在 S3 存储桶中创建 Lambda 通知

查找谁在联合 AWS 账户中创建了 s3 存储桶

在 Terraform 中创建 S3 存储桶通知时出错