IBM - COS - SDK IAM令牌

时间:2018-01-09 09:18:10

标签: ibm-cloud object-storage

我正在尝试使用python访问我的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

3 个答案:

答案 0 :(得分:0)

auth_endpoint应为https

请参阅此处的示例 https://github.com/IBM/ibm-cos-sdk-python

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

要连接 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)

创建一个bucket

    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()