在IBM文档之后,我尝试使用Python和ibm 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):指定的桶不存在。
我很确定桶确实存在,因为我可以在输出中清楚地看到
>>> for b in cos.buckets.all():
... print(b.name)
...
sql-efbfb11b-fa01-4c49-8fe1-c70793be3f5f
我在这里做错什么了?
发布于 2020-03-24 16:11:22
错误的原因是概念性的。您可以看到所有的桶,但只能获得连接区域中的桶的详细信息。我很久以前就遇到了这个问题,并像这样解决了它(当时测试,而不是今天):
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
https://stackoverflow.com/questions/60834454
复制相似问题