"The specified bucket does not exist" 尝试使用 Python 列出 IBM Cloud Object Storage 中的对象时

"The specified bucket does not exist" when trying to list objects in IBM Cloud Object Storage using Python

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: An error occurred (NoSuchBucket) when calling the ListObjects operation: The specified bucket does not exist.

我很确定这个桶确实存在,因为我可以在

的输出中清楚地看到它
>>> for b in cos.buckets.all():
...  print(b.name)
... 
sql-efbfb11b-fa01-4c49-8fe1-c70793be3f5f

我做错了什么?

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

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)

我找到的另一个片段:

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