Python: 如何列出每个IBM COS 存储桶的存储桶位置?

Python: How to list bucket location for each IBM COS bucket?

我正在使用 Python SDK for IBM Cloud Object Storage and want to loop over all visible buckets and return their location. The issue I am facing is that for some buckets an error The specified bucket does not exist. is returned. According to

尽管如此,我如何处理它并至少获得可访问存储桶的位置?这是粗略的 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'])

我现在使用了这个解决方法:

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