ibm-cloudibm-cloud-storage

How to create a bucket using the python SDK?


I'm trying to create a bucket in cloud object storage using python. I have followed the instructions in the API docs.

This is the code I'm using

COS_ENDPOINT = "https://control.cloud-object-storage.cloud.ibm.com/v2/endpoints"

# Create client 
cos = ibm_boto3.client("s3",
    ibm_api_key_id=COS_API_KEY_ID,
    ibm_service_instance_id=COS_INSTANCE_CRN,
    config=Config(signature_version="oauth"),
    endpoint_url=COS_ENDPOINT
)
s3 = ibm_boto3.resource('s3') 

def create_bucket(bucket_name):
    print("Creating new bucket: {0}".format(bucket_name))
    s3.Bucket(bucket_name).create()
    return
        
bucket_name = 'test_bucket_442332'
create_bucket(bucket_name)

I'm getting this error - I tried setting CreateBucketConfiguration={"LocationConstraint":"us-south"}, but it doesnt seem to work

"ClientError: An error occurred (IllegalLocationConstraintException) when calling the CreateBucket operation: The unspecified location constraint is incompatible for the region specific endpoint this request was sent to."

Solution

  • Resolved by going to https://cloud.ibm.com/docs/cloud-object-storage?topic=cloud-object-storage-endpoints#endpoints

    And choosing the endpoint specific to the region I need. The "Endpoint" provided with the credentials, is not the actual endpoint.