【问题标题】:"The specified bucket does not exist" when trying to list objects in IBM Cloud Object Storage using Python尝试使用 Python 列出 IBM Cloud Object Storage 中的对象时出现“指定的存储桶不存在”
【发布时间】:2020-07-05 03:35:23
【问题描述】:

IBM documentation 之后,我正在尝试使用 Python 和 ibm-cos-sdk 列出我的 COS 存储桶中的对象。

import ibm_boto3
from ibm_botocore.client import Config, ClientError

cos = ibm_boto3.resource("s3",
      ibm_api_key_id=params['API_KEY'],
      ibm_service_instance_id=params['COS_TGT_INSTANCE_CRN'],
      ibm_auth_endpoint=params['IAM_ENDPOINT'],
      config=Config(signature_version="oauth"),
      endpoint_url=params['COS_ENDPOINT']
)

for obj in cos.Bucket('sql-efbfb11b-fa01-4c49-8fe1-c70793be3f5f').objects.all():
  print(obj.key)

结果:

ibm_botocore.errorfactory.NoSuchBucket:调用ListObjects操作时发生错误(NoSuchBucket):指定的bucket不存在。

我很确定存储桶确实存在,因为我可以在输出中清楚地看到它

>>> for b in cos.buckets.all():
...  print(b.name)
... 
sql-efbfb11b-fa01-4c49-8fe1-c70793be3f5f

我在这里做错了什么?

【问题讨论】:

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


    【解决方案1】:

    错误的原因是概念性的。您可以查看所有存储桶,但只能获取连接区域中存储桶的详细信息。我很久以前遇到过这个问题并像这样解决了它(当时测试过,不是今天):

    def buckets_json():
       # Get a list of all bucket names from the response
       buckets = [bucket['Name'] for bucket in cos.list_buckets()['Buckets']]
       locs2=[{"name":name,"loc":loc} for name,loc in locations(buckets=buckets).iteritems()]
       return jsonify(buckets=locs2)
    

    我发现的另一个 sn-p:

    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
    

    【讨论】:

    • 谢谢 Henrik,说的很对。您的 sn-ps 显然是指 Client 接口。我看不出如何使用Resource 指定存储桶位置,所以我猜这里的方法是简单地为每个存储桶指定正确的端点。
    • 是的,我尝试提取位置信息并连接到正确的端点。
    猜你喜欢
    • 1970-01-01
    • 2020-04-10
    • 2021-02-11
    • 1970-01-01
    • 2021-01-25
    • 2019-09-18
    • 2021-01-06
    • 1970-01-01
    • 2021-07-17
    相关资源
    最近更新 更多