【发布时间】:2017-02-22 02:54:47
【问题描述】:
我正在使用以下代码来检查存储桶的存在:
def check_bucket_existed(bucket_name, public_auth_endpoint):
endpoint = 'https://' + public_auth_endpoint
s3 = boto3.resource('s3', endpoint_url=endpoint)
try:
s3.meta.client.head_bucket(Bucket=bucket_name)
exists = True
except botocore.exceptions.ClientError as e:
error_code = int(e.response['Error']['Code'])
if error_code == 404:
exists = False
else:
exists = True
return exists
如果bucket不存在,则使用以下代码开始创建bucket:
def create_bucket(bucket_name, public_auth_endpoint):
endpoint = 'https://' + public_auth_endpoint
s3 = boto3.resource('s3', endpoint_url=endpoint)
if check_bucket_existed(bucket_name, public_auth_endpoint):
print("Bucket {} existed , skip bucket creation process".format(bucket_name))
return True
else:
print("Bucket {} doesn't exist, start bucket creation process.".format(bucket_name))
try:
s3.create_bucket(Bucket=bucket_name)
if check_bucket_existed(bucket_name, public_auth_endpoint):
print("Bucket {} created successsfully.".format(bucket_name))
return True
except botocore.exceptions.ClientError as e:
print("Error: Unable to create the bucket : %s" % e)
return False
我对运行 create_bucket 代码时得到的以下日志感到困惑:
Bucket td.cos.s1 doesn't exist, start bucket creation process.
Error: Unable to create the bucket : An error occurred (BucketAlreadyExists) when calling the CreateBucket operation: The requested bucket name is not available. The bucket namespace is shared by all users of the system. Please select a different name and try again.
在日志中,首先显示要使用的bucket name 不存在,但是当开始使用该名称创建bucket 时,提示ERROR info 表明该bucket name 不能使用。
我多次使用这个bucket名字,这个bucket名字可以用。
我的代码有什么问题?谢谢!
【问题讨论】:
-
这个bucket名字以前用过。但在运行此 create_bucket 代码之前已将其删除。
标签: amazon-s3 ibm-cloud-infrastructure bucket