D Sample Public File Transfer Script for RI and AIF

#!/bin/bash

# Sample Public FTS script

### EDIT HERE to reflect your environment

BASE_URL="https://__YOUR_TENANT_BASE_URL__"
TENANT="__YOUR-TENANT_ID__"
IDCS_URL="https://_YOUR__IDCS__URL__/oauth2/v1/token"
IDCS_CLIENTID="__YOUR_CLIENT_APPID__"
IDCS_CLIENTSECRET="__YOUR_CLIENT_SECRET___"
IDCS_SCOPE="rgbu:rsp:psraf-__YOUR_SCOPE__"

### FINISHED

clientToken() {
    curl  -sX POST "${IDCS_URL}" \
          --header "Authorization: Basic ${IDCS_AUTH}" \
          --header "Content-Type: application/x-www-form-urlencoded" \
          --data-urlencode "grant_type=client_credentials" \
          --data-urlencode "scope=${IDCS_SCOPE}"  | jq -r .access_token
}

ping() {
    echo "Pinging"
    curl -sfX GET "${BASE_URL}/${TENANT}/RIRetailAppsPlatformServices/services/private/FTSWrapper/ping" \
         --header 'Accept: application/json' \
         --header 'Accept-Language: en' \
         --header "Authorization: Bearer ${CLIENT_TOKEN}" | jq
}


listPrefixes() {
    echo "Listing storage prefixes"
    curl -sfX GET "${BASE_URL}/${TENANT}/RIRetailAppsPlatformServices/services/private/FTSWrapper/listprefixes" \
         --header 'Accept: application/json' \
         --header 'Accept-Language: en' \
         --header "Authorization: Bearer ${CLIENT_TOKEN}" | jq
}


listFiles() {
    echo "Listing files for ${1}"
    curl -sfX GET "${BASE_URL}/${TENANT}/RIRetailAppsPlatformServices/services/private/FTSWrapper/listfiles?prefix=${1}" \
         --header 'Accept: application/json' \
         --header 'Accept-Language: en' \
         --header "Authorization: Bearer ${CLIENT_TOKEN}" | jq
}

deleteFiles() {
    echo "Deleting files"
    json=$(fileCollection $@)
    curl --show-error -sfX DELETE "${BASE_URL}/${TENANT}/RIRetailAppsPlatformServices/services/private/FTSWrapper/delete" \
         --header 'content-type: application/json' \
         --header 'Accept: application/json' \
         --header 'Accept-Language: en' \
         --header "Authorization: Bearer ${CLIENT_TOKEN}" \
         -d "${json}" | jq
}

fileMover() {
    movement="${1}"
    shift

    json=$(fileCollection $@)
    requestPAR "${movement}" "${json}"
}

fileCollection() {
    local json="{  \"listOfFiles\": [ __FILES__ ] }"

    sp="${1}"
    shift

    while (( "${#}" )); do
        list="${list} { \"storagePrefix\": \"${sp}\", \"fileName\": \"${1}\" }"
        if [[ ${#} -gt "1" ]]; then
            list="${list},"
        fi
        shift
    done

    echo "${json/__FILES__/${list}}"
}

requestPAR() {
    use="${1}"
    echo "Requesting PARs for ${use}"
    pars="$(curl --show-error -sfX POST "${BASE_URL}/${TENANT}/RIRetailAppsPlatformServices/services/private/FTSWrapper/${use}" \
         --header 'content-type: application/json' \
         --header 'Accept: application/json' \
         --header 'Accept-Language: en' \
         --header "Authorization: Bearer ${CLIENT_TOKEN}" \
         -d "${2}")"

    if [[ "$?" -ne "0" ]]; then
        echo "Error retreiving PAR, check files specified."
        echo "${pars}"
        exit 1
    fi

    list=$(jq -r ".parList[]|[.name, .accessUri]|@csv" <<< "${pars}")

    while IFS='' read -r line; do
        fn=$(echo ${line}|cut -d\, -f1|tr -d \")
        par=$(echo ${line}|cut -d\, -f2|tr -d \")

        if [[ ${use} == "upload" ]]; then
            echo "Uploading ${fn} to ${par}"
            curl -o log -X PUT --data-binary "@${fn}" "${par}"
        else
            echo "Downloading ${fn} from ${par}"
            curl -o ${fn} -X GET "${par}"
        fi
    done <<< "${list}"
}


#Entry point
IDCS_AUTH=$(echo -n ${IDCS_CLIENTID}:${IDCS_CLIENTSECRET} | base64 -w0)
CLIENT_TOKEN=$(clientToken)

case "${1}" in
    ping)
        ping
        ;;
    listprefixes)
        shift
        listPrefixes
        ;;
    listfiles)
        shift
        listFiles ${@}
        ;;
    deletefiles)
        shift
        deleteFiles ${@}
        ;;
    uploadfiles)
        shift
        fileMover upload ${@}
        ;;
    downloadfiles)
        shift
        fileMover download ${@}
        ;;
    *)
        echo "Usage: $0"
        echo " ping                                       : test service functionality"
        echo " listprefixes                               : list registered prefixes"
        echo " listfiles [prefix]                         : list files within a prefix"
        echo " deletefiles [prefix] [file1] [file2] ...   : delete files with this prefix"
        echo " uploadfiles [prefix] [file1] [file2] ...   : upload files with this prefix"
        echo " downloadfiles [prefix] [file1] [file2] ... : download files with this prefix"
        echo 
        exit 0
        ;;
esac