IBM Cloud Object Storage authorization key curl request

1k Views Asked by At

I was trying to download a file from my IBM Cloud Object storage using the following curl command,

curl "https://(endpoint)/(bucket-name)/(object-key)"-H "Authorization: bearer (token)"

got the end point from my IBM Object storage dashboard, what does object-key and token mean here. I was able to get 4 tokens for this particular service.

Thanks in advance.

2

There are 2 best solutions below

0
On BEST ANSWER

After exploring i found the following details...

object-key : Object Name bearer token : IAM token

0
On

Scavenging for useful information on this topic was difficult. So let me put some here:

Generic API doc: https://cloud.ibm.com/docs/cloud-object-storage/api-reference?topic=cloud-object-storage-compatibility-api

Retrieving and storing objects: https://cloud.ibm.com/docs/cloud-object-storage?topic=cloud-object-storage-object-operations

Tutorial: https://blog.speduconsulting.co.uk/2021/07/12/getting-started-with-ibm-cloud-object-storage/

Retrieving the token

Using some bash, curl and jq we can create a token retrieval function:

function getToken {
    local authenticationUrl=$1
    local apiKey=$2

    curl -X "POST" "${authenticationUrl}" \
         -H 'Accept: application/json' \
         -H 'Content-Type: application/x-www-form-urlencoded' \
         --data-urlencode "apikey=${apiKey}" \
         --data-urlencode "response_type=cloud_iam" \
         --data-urlencode "grant_type=urn:ibm:params:oauth:grant-type:apikey" \
         --silent | jq .access_token -r
}

To use the function, run something similar to:

authenticationUrl="https://iam.cloud.ibm.com/identity/token"
apiKey="my-cos-api-key"

token=$(getToken ${authenticationUrl} ${apiKey})

That will output a token that you can use in the next functions.

bash function to put a file in COS using curl:

function putFileInCOS {
    local token=$1
    local bucketUrl=$2
    local bucketName=$3
    local filePath=$4
    local fileName=$5
  
    echo "Uploading file: ${fileName}"
  
    curl -X "PUT" "${bucketUrl}/${bucketName}/${fileName}" \
         -H "Authorization: bearer ${token}" \
         --data-binary "@${filePath}"
}

Usage:

bucketUrl="https://s3.eu-de.cloud-object-storage.appdomain.cloud"
bucketName="bucket-name"
filePath="./my-file-path.csv"
fileName="file-name-in-cos"

putFileInCOS ${token} ${bucketUrl} ${bucketName} ${filePath} ${fileName}

bash function to retrieve a file from COS using curl

function getFileFromCOS {
    local token=$1
    local bucketUrl=$2
    local bucketName=$3
    local fileName=$4
  
    echo "Retrieving file: ${fileName}"
  
    curl -X "GET" "${bucketUrl}/${bucketName}/${fileName}" \
         -H "Authorization: bearer ${token}"
}

Usage:

bucketUrl="https://s3.eu-de.cloud-object-storage.appdomain.cloud"
bucketName="bucket-name"
fileName="file-name-in-cos"

getFileFromCOS ${token} ${bucketUrl} ${bucketName} ${fileName}