Share Documents with Cloud Object Storage¶
- Login to your IBM Cloud account,
ibmcloud login -u <username>
If you are using Single Sign-On (SSO) use the -sso flag to log in.
- Create an IAM APIKEY for the Cloud Object Storage service, e.g. with service name
remkohdev-cos1. Download and save theiam_apikeyby adding the--fileflag,
COS_NAME=<service-name>
IAM_APIKEY_NAME=$COS_NAME-apikey1
ibmcloud iam api-key-create $IAM_APIKEY_NAME --file $IAM_APIKEY_NAME.txt
-
Set the IAM apikey environment variable,
IAM_APIKEY=$(cat $IAM_APIKEY_NAME.txt | jq -r '.apikey') echo $IAM_APIKEY -
To create an object storage instance with a Lite plan, you need a
resource group. Check if you already have a resource-groupibmcloud resource groupsoutputs,
ibmcloud resource groups OK Name ID Default Group State Default 282d2f25256540499cf99b43b34025bf true ACTIVEIf you do not have a resource group yet, create one,
ibmcloud resource group-create Default -
Create a new Object Storage instance with a
Liteplan. If you prefer a paid plan, chooseStandardplan.Set environment variables,
COS_PLAN=Lite RESOURCEGROUP=DefaultThen create the Cloud Object instance,
ibmcloud resource service-instance-create $COS_NAME cloud-object-storage $COS_PLAN global -g $RESOURCEGROUP -
Get the GUID for the Cloud Object Storage service,
COS_GUID=$(ibmcloud resource service-instance $COS_NAME --output json | jq -r '.[0].guid') echo $COS_GUID -
Create new service credentials with
Role: Readerto share read-only access, and another service credentials withRole: Writerto upload documents,COS_CREDENTIALS1=$COS_NAME-reader-credentials1 COS_CREDENTIALS2=$COS_NAME-writer-credentials2 ibmcloud resource service-key-create $COS_CREDENTIALS1 Reader --instance-name $COS_NAME ibmcloud resource service-key-create $COS_CREDENTIALS2 Writer --instance-name $COS_NAME -
Create environment variables for the apikeys,
COS_READER_APIKEY=$(ibmcloud resource service-key $COS_CREDENTIALS1 --output json | jq -r '.[0].credentials.apikey') echo $COS_READER_APIKEY COS_WRITER_APIKEY=$(ibmcloud resource service-key $COS_CREDENTIALS2 --output json | jq -r '.[0].credentials.apikey') echo $COS_WRITER_APIKEY -
Create a new bucket with a
Standardstorage class,COS_BUCKET=$COS_NAME-bucket1 COS_STORAGECLASS=Standard ibmcloud cos create-bucket --bucket $COS_BUCKET --ibm-service-instance-id $COS_GUID --class $COS_STORAGECLASS -
Verify the new bucket was created successfully.
ibmcloud cos list-buckets --ibm-service-instance-id $COS_GUID -
Retrieve the region of your object storage configuration,
ibmcloud cos config region listOr list your bucket's `LocationRestraint'
ibmcloud cos get-bucket-location --bucket $COS_BUCKET --json | jq -r '.LocationConstraint' -
Set the environment variable for region, e.g.
us-south,COS_REGION=<region> -
Create a new document,
COS_OBJECT_KEY=helloworld.txt echo "Hello World! Today is $(date)" > $COS_OBJECT_KEY -
Upload a document using the S3Manager,
ibmcloud cos upload --bucket $COS_BUCKET --key $COS_OBJECT_KEY --file ./helloworld.txt --content-language en-US --content-type "text/plain" OK Successfully uploaded object 'helloworld.txt' to bucket 'e59a327194-cos-1-bucket1'. -
Get IAM Token using the IAM Apikey:
curl --location --request POST "https://iam.cloud.ibm.com/identity/token" --header "Accept: application/json" --header "Content-Type: application/x-www-form-urlencoded" --header "apikey: $COS_READER_APIKEY" --data-urlencode "apikey=$IAM_APIKEY" --data-urlencode "response_type=cloud_iam" --data-urlencode "grant_type=urn:ibm:params:oauth:grant-type:apikey" -
Set the response
ACCESS_TOKEN=<access_token>Or using the curl statement above,
ACCESS_TOKEN=$(curl --location --request POST "https://iam.cloud.ibm.com/identity/token" --header "Accept: application/json" --header "Content-Type: application/x-www-form-urlencoded" --header "apikey: $COS_READER_APIKEY" --data-urlencode "apikey=$IAM_APIKEY" --data-urlencode "response_type=cloud_iam" --data-urlencode "grant_type=urn:ibm:params:oauth:grant-type:apikey" | jq -r '.access_token') echo $ACCESS_TOKEN -
get bucket:
curl --location --request GET "https://s3.$COS_REGION.cloud-object-storage.appdomain.cloud/$COS_BUCKET" --header "Authorization: Bearer $ACCESS_TOKEN" --header "Accept: application/json" -
Get object key:
ibmcloud cos list-objects --bucket $COS_BUCKETcurl --location --request GET "https://s3.$COS_REGION.cloud-object-storage.appdomain.cloud/$COS_BUCKET/$COS_OBJECT_KEY" --header "Authorization: Bearer $ACCESS_TOKEN" -
Get document