[英]Cannot retrieve bucket name in IBM Cloud object storage using python
[英]"The specified bucket does not exist" when trying to list objects in IBM Cloud Object Storage using Python
按照IBM 文档,我尝试使用 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
我在这里做错了什么?
错误的原因是概念性的。 可以看到所有的bucket,但只能获取连接区域的bucket的详细信息。 我很久以前遇到过这个问题并像这样解决了它(当时测试,不是今天):
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.