Skip to content

Instantly share code, notes, and snippets.

@martonsereg
Created November 23, 2015 17:54
Show Gist options
  • Save martonsereg/5a59064106634fb7e73e to your computer and use it in GitHub Desktop.
Save martonsereg/5a59064106634fb7e73e to your computer and use it in GitHub Desktop.
# List the blobs in an Azure storage container.
echo "usage: ${0##*/} <storage-account-name> <container-name> <access-key>"
storage_account="$1"
container_name="$2"
access_key="$4"
blob_store_url="blob.core.windows.net"
http="https"
blob_name="$3"
authorization="SharedKey"
request_method="PUT"
request_date=$(TZ=GMT 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"
x_ms_lease_action="x-ms-lease-action:break"
# Build the signature string
canonicalized_headers="${x_ms_date_h}\n${x_ms_lease_action}\n${x_ms_version_h}"
canonicalized_resource="/${storage_account}/${container_name}/${blob_name}"
string_to_sign="${request_method}\n\n\n2000\n\n\n\n\n\n\n\n\n${canonicalized_headers}\n${canonicalized_resource}\ncomp:lease"
echo -e "$string_to_sign"
# 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"
curl \
-X PUT \
-H "$x_ms_date_h" \
-H "$x_ms_version_h" \
-H "$x_ms_lease_action" \
-H "Content-Length: 2000" \
-H "$authorization_header" \
"https://${storage_account}.blob.core.windows.net/${container_name}/${blob_name}?comp=lease"
./lease_blob.sh 0095233545063f2097westus vm-images cb-centos71-amb210-2015-08-25-ofs_2015-August-25_10-1-os-2015-08-25.vhd 4vTIvDrTVNMS21svn1BFYzQmHj7Ho7cQpT8AqS0wrMawc80nFSd4lMHuTQTYqlZV8Tr3lm6+D2ZFVlkfkXAGow==
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment