Skip to content

Instantly share code, notes, and snippets.

@biharitomi
Forked from martonsereg/lease_blob.sh
Created November 23, 2015 17:57
Show Gist options
  • Save biharitomi/f173907b49e5bd9b80c5 to your computer and use it in GitHub Desktop.
Save biharitomi/f173907b49e5bd9b80c5 to your computer and use it in GitHub Desktop.

Revisions

  1. Marton Sereg created this gist Nov 23, 2015.
    48 changes: 48 additions & 0 deletions lease_blob.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    # 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"
    3 changes: 3 additions & 0 deletions usage.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    ```
    ./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==
    ```