Skip to content

Instantly share code, notes, and snippets.

@velizarn
Last active August 15, 2023 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save velizarn/e0b4346be975efe028de2877ffb85276 to your computer and use it in GitHub Desktop.
Save velizarn/e0b4346be975efe028de2877ffb85276 to your computer and use it in GitHub Desktop.
Shell script to upload file by using Cloudinary API + cURL

Shell script to upload file by using Cloudinary API + cURL

  1. Create a new file e.g. cloudinary-curl-upload.sh

  2. Then execute the file with required input parameters

  ./cloudinary-curl-upload.sh "path/to/file.ext" "cloud-name" "your-api-key" "your-api-secret"
  ./cloudinary-curl-upload.sh "path/to/file.ext" "cloud-name" "your-api-key" "your-api-secret" "remote-folder"

Resources

#!/bin/bash
# ------------------------------
# Input parameters
fileName=$1
cloudName=$2
apiKey=$3
apiSecret=$4
folder=${5:-""}
# ------------------------------
timestamp=$(date +%s)
publicId=$(basename $fileName)
if [ ! -z $folder ]; then
folderStr="folder=${folder}"
else
folderStr=""
fi
datatobehashed="${folderStr}&public_id=${publicId}&timestamp=${timestamp}$apiSecret"
hash=$(echo -n ${datatobehashed} | sha1sum | awk '{print $1}')
commandArr=()
commandArr+=("curl -X POST https://api.cloudinary.com/v1_1/${cloudName}/auto/upload") # -s
commandArr+=("-F \"file=@${fileName}\"")
commandArr+=("-F \"public_id=${publicId}\"")
if [ ! -z $folder ]; then
commandArr+=("-F \"${folderStr}\"")
fi
commandArr+=("-F \"api_key=${apiKey}\"")
commandArr+=("-F \"timestamp=${timestamp}\"")
commandArr+=("-F \"signature=${hash}\"")
# commandArr+=("> /dev/null")
commandStr="${commandArr[@]}"
eval $commandStr
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment