Kashyap Ravichandran
Kashyap Ravichandran

Reputation: 133

IBM - COS - SDK IAM token

I am trying to access my COS service using python.Referring IBM's Documentation was able to write the following code snippet

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

Is this correct ? Also while trying to execute the above code the compiler is not able to retrieve the authentication token from auth_endpoint. Am i missing something?

Please to help

Thanks in advance!

I am including the output for your reference...

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

And I am using python 3.x

Upvotes: 1

Views: 2284

Answers (3)

python_for_you
python_for_you

Reputation: 1

To Connect with ibm cloud storage account we need api_key, service_instace_id,auth_endpoint and 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)

to create a bucket

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

create_bucket()

to list Buckets in cloud

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

Upvotes: 0

Aragorn
Aragorn

Reputation: 26

As instructed in README, the auth_endpoint should have /oidc/token at the end, for example, 'http://iam.bluemix.net/oidc/token'.

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

Upvotes: 1

lifemikey
lifemikey

Reputation: 86

The auth_endpoint should be https

See the example here https://github.com/IBM/ibm-cos-sdk-python

Upvotes: 0

Related Questions