【问题标题】:IBM - COS - SDK IAM tokenIBM - COS - SDK IAM 令牌
【发布时间】:2018-01-09 09:18:09
【问题描述】:

我正在尝试使用 python 访问我的 COS 服务。参考 IBM 的文档能够编写以下代码 sn-p

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

【问题讨论】:

    标签: ibm-cloud object-storage


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      auth_endpoint 应该是 https

      在此处查看示例 https://github.com/IBM/ibm-cos-sdk-python

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-10-24
          • 2019-04-23
          • 2018-10-16
          • 2018-03-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多