Skip to content

Instantly share code, notes, and snippets.

@chmeee
Created November 12, 2019 14:03
Show Gist options
  • Save chmeee/096200dbe6bae40783fd4d4660d19d45 to your computer and use it in GitHub Desktop.
Save chmeee/096200dbe6bae40783fd4d4660d19d45 to your computer and use it in GitHub Desktop.
Upload a blob to Azure
#!/usr/bin/env bash
if [[ $# != 3 ]]; then
echo "Cowardly refusing to run without exactly three arguments"
echo "Syntax: $0 key-string container-url file"
exit 1
fi
API_VERSION="2015-02-21"
DATE=$(TZ=GMT date +"%a, %d %b %Y %T %Z")
KEY=$1
URL=$2
FILE=$3
FILE_LENGTH=$(wc --bytes < $FILE)
FILE_TYPE=$(file --mime-type -b $FILE)
FILE_MD5=$(md5sum -b $FILE | awk '{ print $1 }')
MS_DATE="x-ms-date:$DATE"
MS_VERSION="x-ms-version:$API_VERSION"
MS_BLOB_TYPE="x-ms-blob-type:BlockBlob"
ACCOUNT=$(echo -n $URL|sed -r 's|https://([^.]*)\..*|\1|')
CONTAINER=$(echo -n $URL|sed -r 's|.*\.net/(.*)|\1|')
CANHEADERS="${MS_BLOB_TYPE}\n${MS_DATE}\n${MS_VERSION}"
CANRESOURCE="/$ACCOUNT/$CONTAINER/$FILE"
SIGSTRING="PUT\n\n\n$FILE_LENGTH\n\n$FILE_TYPE\n\n\n\n\n\n\n$CANHEADERS\n${CANRESOURCE}"
SIGNATURE=$(echo -en "$SIGSTRING" | openssl dgst -sha256 -mac HMAC -macopt "hexkey:$(echo -n $KEY|base64 -d -w0 | xxd -p -c256)" -binary | base64 -w0)
curl -X PUT -sLk $URL/$FILE -H "Authorization: SharedKey $ACCOUNT:$SIGNATURE" -H "$MS_DATE" -H "$MS_VERSION" -H "$MS_BLOB_TYPE" -H "Content-Type: $FILE_TYPE" -T $FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment