Created
January 12, 2020 18:27
-
-
Save aashish-chaubey/1479a6afbbf64a6cd1c0f92f0ff4f60d to your computer and use it in GitHub Desktop.
Python Code to pull out data from IBM Cloud Object Store
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!