Skip to content

Instantly share code, notes, and snippets.

@manzinge
Created February 7, 2022 08:07
Show Gist options
  • Save manzinge/5af5af5a29ac36f9032a3dcc162e0bfa to your computer and use it in GitHub Desktop.
Save manzinge/5af5af5a29ac36f9032a3dcc162e0bfa to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# expected to be defined in the environment
# - AZURE_STORAGE_ACCOUNT
# - AZURE_CONTAINER_NAME
# - AZURE_ACCESS_KEY
# inspired by
# https://stackoverflow.com/questions/20103258/accessing-azure-blob-storage-using-bash-curl
# modified from
# https://gist.github.com/rtyler/30e51dc72bed23718388c43f9c11da76
AZURE_ACCESS_KEY=""
AZURE_CONTAINER_NAME=""
AZURE_STORAGE_ACCOUNT=""
authorization="SharedKey"
HTTP_METHOD="PUT"
request_date=$(TZ=GMT date "+%a, %d %h %Y %H:%M:%S %Z")
storage_service_version="2015-02-21"
# HTTP Request headers
x_ms_date_h="x-ms-date:$request_date"
x_ms_version_h="x-ms-version:$storage_service_version"
x_ms_blob_type_h="x-ms-blob-type:BlockBlob"
# Debug purposes
# find $WORKSPACE/
cd $WORKSPACE/.build/last/default-webgl/extra_data/addrs/WebGL/
for file in *; do
echo "${file}"
FILE_LENGTH=$(wc -c < ${file})
FILE_TYPE=$(file --mime-type -b ${file})
canonicalized_headers="${x_ms_blob_type_h}\n${x_ms_date_h}\n${x_ms_version_h}"
canonicalized_resource="/${AZURE_STORAGE_ACCOUNT}/${AZURE_CONTAINER_NAME}/${file}"
string_to_sign="${HTTP_METHOD}\n\n\n${FILE_LENGTH}\n\n${FILE_TYPE}\n\n\n\n\n\n\n${canonicalized_headers}\n${canonicalized_resource}"
decoded_hex_key="$(printf ${AZURE_ACCESS_KEY} | base64 -d | 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)
authorization_header="Authorization: $authorization $AZURE_STORAGE_ACCOUNT:$signature"
OUTPUT_FILE="https://${AZURE_STORAGE_ACCOUNT}.blob.core.windows.net/${AZURE_CONTAINER_NAME}/${file}"
curl -X ${HTTP_METHOD} \
-T ${file} \
-H "$x_ms_date_h" \
-H "$x_ms_version_h" \
-H "$x_ms_blob_type_h" \
-H "$authorization_header" \
-H "Content-Type: ${FILE_TYPE}" \
${OUTPUT_FILE}
done
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment