Python:如何列出每个ibmcos bucket的bucket位置?

2024-09-21 03:20:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Python SDK for IBM Cloud Object Storage并希望循环所有可见的bucket并返回它们的位置。我面临的问题是,对于某些bucket,会返回一个错误The specified bucket does not exist.。根据this SO answer it is caused by different storage types。你知道吗

我如何处理它,但至少得到位置可访问桶?下面是粗略的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'])

Tags: instancekeyauthapiidforbucketresponse
1条回答
网友
1楼 · 发布于 2024-09-21 03:20:05

我现在求助于这个解决方法:

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。你知道吗

相关问题 更多 >

    热门问题