Last active
March 29, 2021 11:17
-
-
Save angeloc/e7d3166807c124503dc2e339f13483c5 to your computer and use it in GitHub Desktop.
Download Azure blob from bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
usage() { | |
echo "usage: ${0##*/} <azure storage key> <azure storage account> <container-name> <blob-name>" | |
} | |
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ]; then | |
usage | |
exit 1 | |
fi | |
access_key="$1" | |
storage_account="$2" | |
container_name="$3" | |
blob_store_url="blob.core.windows.net" | |
authorization="SharedKey" | |
request_method="GET" | |
request_date=$(TZ=GMT LC_ALL=en_US.utf8 date "+%a, %d %h %Y %H:%M:%S %Z") | |
storage_service_version="2011-08-18" | |
# HTTP Request headers | |
x_ms_date_h="x-ms-date:$request_date" | |
x_ms_version_h="x-ms-version:$storage_service_version" | |
# Build the signature string | |
canonicalized_headers="${x_ms_date_h}\n${x_ms_version_h}" | |
canonicalized_resource="/${storage_account}/${container_name}/$4" | |
string_to_sign="${request_method}\n\n\n\n\n\n\n\n\n\n\n\n${canonicalized_headers}\n${canonicalized_resource}" | |
# Decode the Base64 encoded access key, convert to Hex. | |
decoded_hex_key="$(echo -n $access_key | base64 -d -w0 | xxd -p -c256)" | |
# Create the HMAC signature for the Authorization header | |
signature=$(printf "$string_to_sign" | openssl dgst -sha256 -mac HMAC -macopt "hexkey:$decoded_hex_key" -binary | base64 -w0) | |
authorization_header="Authorization: $authorization $storage_account:$signature" | |
URL="https://${storage_account}.${blob_store_url}/${container_name}/$4" | |
curl -s \ | |
-H "$x_ms_date_h" \ | |
-H "$x_ms_version_h" \ | |
-H "$authorization_header" \ | |
"$URL" -o "$4" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment