Skip to content

Instantly share code, notes, and snippets.

@giskou
Created December 19, 2019 13:47
Show Gist options
  • Select an option

  • Save giskou/531865114be945421784bb2da84ad432 to your computer and use it in GitHub Desktop.

Select an option

Save giskou/531865114be945421784bb2da84ad432 to your computer and use it in GitHub Desktop.
Check if azure storage container exists
#!/usr/bin/env bash
# vim: ai ts=2 sw=2 et sts=2 ft=sh
set -euf -o pipefail
storage_account="$1"
container_name="$2"
access_key="$3"
# HTTP Request headers
x_ms_date_h="x-ms-date:$(TZ=GMT date "+%a, %d %h %Y %H:%M:%S %Z")"
x_ms_version_h="x-ms-version:2019-02-02"
# Build the signature string
canonicalized_headers="${x_ms_date_h}\\n${x_ms_version_h}"
canonicalized_resource="/${storage_account}/${container_name}"
string_to_sign="GET\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n${canonicalized_headers}\\n${canonicalized_resource}\\nrestype:container"
# Decode the Base64 encoded access key, convert to Hex.
decoded_hex_key="$(echo -n "$access_key" | base64 -d | tr -d '\n' | xxd -p -c256 | tr -d '[:space:]')"
# 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 | tr -d '\n')
authorization_header="Authorization: SharedKey $storage_account:$signature"
curl -v \
-H "$x_ms_date_h" \
-H "$x_ms_version_h" \
-H "$authorization_header" \
"https://${storage_account}.blob.core.windows.net/${container_name}?restype=container"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment