IBM - COS - SDK IAM 令牌

IBM - COS - SDK IAM token

我正在尝试使用 python.Referring 访问我的 COS 服务 IBM 的文档能够编写以下代码片段

import ibm_boto3
from ibm_botocore.client import Config

api_key = 'key'
service_instance_id = 'resource-service-id'
auth_endpoint = 'http://iam.bluemix.net/'
service_endpoint = 'endpoint'
s3 = ibm_boto3.resource('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)
s3.Bucket('bucket name').download_file('object name','location where the object must be saved')

这是正确的吗?此外,在尝试执行上述代码时,编译器无法从 auth_endpoint 检索身份验证令牌。我错过了什么吗?

请帮忙

提前致谢!

我包含输出供您参考...

  ibm_botocore.exceptions.CredentialRetrievalError: Error when retrieving credentials from https://iam.ng.bluemix.net/oidc/token: Retrieval of tokens from server failed

我正在使用 python 3.x

auth_endpoint 应该是 https

请看这里的例子 https://github.com/IBM/ibm-cos-sdk-python

按照 README 中的说明,auth_endpoint 的末尾应该有 /oidc/token,例如,'http://iam.bluemix.net/oidc/token'。

auth_endpoint = 'https://iam.bluemix.net/oidc/token'

要连接 ibm 云存储帐户,我们需要 api_key、service_instace_id、auth_endpoint 和 service_endpoint.

import ibm_boto3    
from ibm_botocore.client import Config       
api_key = '......' # u can find api_key in service credentials in ibm cloud account    
service_instance_id = '.....' u can find service_instance_id in service credentials in ibm cloud account      
auth_endpoint = 'https://iam.bluemix.net/oidc/token'    
service_endpoint = 'https://s3-api.us-geo.objectstorage.softlayer.net'


cos = ibm_boto3.resource('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)

创建存储桶

    new_bucket = 'abcd1234'
    def create_bucket():
        cos.create_bucket(Bucket=new_bucket)
    return "Bucket created sucessfully"

create_bucket()

列出云中的存储桶

def get_buckets():
    print("Retrieving list of buckets")
try:
    buckets = cos.buckets.all()
    for bucket in buckets:
        print("Bucket Name: {0}".format(bucket.name))
except ClientError as be:
    print("CLIENT ERROR: {0}\n".format(be))
except Exception as e:
    print("Unable to retrieve list buckets: {0}".format(e))       

get_buckets()