import ibm_boto3 from ibm_botocore.client import Config, ClientError # Constants for IBM COS values COS_ENDPOINT = # Current list avaiable at https://control.cloud-object-storage.cloud.ibm.com/v2/endpoints COS_API_KEY_ID = # eg "W00YiRnLW4a3fTjMB-odB-2ySfTrFBIQQWanc--P3byk" COS_AUTH_ENDPOINT = "https://iam.cloud.ibm.com/identity/token" COS_RESOURCE_CRN = # eg "crn:v1:bluemix:public:cloud-object-storage:global:a/3bf0d9003abfb5d29761c3e97696b71c:d6f04d83-6c4f-4a62-a165-696756d63903::" # Create resource cos = ibm_boto3.resource("s3", ibm_api_key_id=COS_API_KEY_ID, ibm_service_instance_id=COS_RESOURCE_CRN, ibm_auth_endpoint=COS_AUTH_ENDPOINT, config=Config(signature_version="oauth"), endpoint_url=COS_ENDPOINT ) 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)) def get_bucket_contents(bucket_name): print("Retrieving bucket contents from: {0}".format(bucket_name)) try: files = cos.Bucket(bucket_name).objects.all() for file in files: print("Item: {0} ({1} bytes).".format(file.key, file.size)) except ClientError as be: print("CLIENT ERROR: {0}\n".format(be)) except Exception as e: print("Unable to retrieve bucket contents: {0}".format(e)) # get_bucket_contents('gamification-cos-standard-tkq') def get_item(bucket_name, item_name): print("Retrieving item from bucket: {0}, key: {1}".format(bucket_name, item_name)) try: file = cos.Object(bucket_name, item_name).get() print("File Contents: {0}".format(file["Body"].read())) except ClientError as be: print("CLIENT ERROR: {0}\n".format(be)) except Exception as e: print("Unable to retrieve file contents: {0}".format(e)) get_item('gamification-cos-standard-data', '50017.jpg')