我正在使用Python SDK for IBM Cloud Object Storage,并且想遍历所有可见的存储桶并返回其位置。我面临的问题是,对于某些存储桶,将返回错误The specified bucket does not exist.
。根据{{3}}。
我该如何处理它,至少要找到可访问的存储桶的位置?这是粗糙的Python代码:
cos = ibm_boto3.client('s3',
ibm_api_key_id=api_key,
ibm_service_instance_id=service_instance_id,
ibm_auth_endpoint=auth_endpoint,
config=Config(signature_version='oauth'),
endpoint_url=service_endpoint)
# Call COS to list current buckets
response = cos.list_buckets()
# Get a list of all bucket names from the response
buckets = [bucket['Name'] for bucket in response['Buckets']]
print(response)
for bucketname in buckets:
print(bucketname, cos.get_bucket_location(Bucket=bucketname)['LocationConstraint'])
答案 0 :(得分:0)
我暂时采用此解决方法:
def locations(buckets):
locs={}
for b in buckets:
try:
locs[b]=cos.get_bucket_location(Bucket=b)['LocationConstraint']
except:
locs[b]=None
pass
return locs
它将尝试获取位置。如果失败,则分配 None (无),当转换为JSON时,它会很好地转换为 null 。