【问题标题】:Python: How to list bucket location for each IBM COS bucket?Python:如何列出每个 IBM COS 存储桶的存储桶位置?
【发布时间】:2019-02-28 23:18:52
【问题描述】:

我正在使用Python SDK for IBM Cloud Object Storage 并希望遍历所有可见的存储桶并返回它们的位置。我面临的问题是,对于某些存储桶,会返回错误 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'])

【问题讨论】:

  • 如何操作取决于您如何判断存储桶是否可访问。您是否可以检查可能缺少的属性?

标签: python amazon-s3 ibm-cloud object-storage


【解决方案1】:

我现在采用了这种解决方法:

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

【讨论】:

    猜你喜欢
    • 2019-01-28
    • 2018-10-24
    • 2021-01-19
    • 2018-04-03
    • 2017-11-25
    • 2019-06-17
    • 1970-01-01
    • 2021-05-09
    • 2023-03-31
    相关资源
    最近更新 更多