Я использую следующий код, чтобы проверить наличие ведра:S3 API создает ошибку ведра на Softlayer
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
Если ведро не существует, то начать создавать ведро с помощью следующего кода:
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.
В журнале, первый это шо Имя используемого ws-bucket не существует, но когда вы начинаете создавать bucket с этим именем, запрашивается информация ERROR, которая показывает, что это имя ведра не может быть использовано.
Я использую это имя ведра много раз, и это имя ковша можно использовать.
Что случилось с моим кодом? Благодаря!
Это имя ведро используется прежде. Но он удаляется перед запуском этого кода create_bucket. – Hengguo