none
maxresults doesn't work with list blobs

    Question

  • Hi,

    The below script doesn't work when maxresults is used to fetch the blob information using 'list blobs'.

    #############  SCRIPT TO BLOB INFO USING LIST BLOBS  #################

    #!/bin/bash

    storage_account="accountname"
    container_name="demo"
    access_key="accessKey"

    blob_store_url="blob.core.windows.net"
    authorization="SharedKey"

    request_method="GET"
    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"

    # Build the signature string
    canonicalized_headers="${x_ms_date_h}\n${x_ms_version_h}"
    canonicalized_resource="/${storage_account}/${container_name}"

    string_to_sign="${request_method}\n\n\n\n\n\n\n\n\n\n\n\n${canonicalized_headers}\n${canonicalized_resource}\ncomp:list\nmaxresults:1\nprefix:blob1\nrestype:container"
    # NOTE - The below string_to_sign along with commented https request does work -
    # string_to_sign="${request_method}\n\n\n\n\n\n\n\n\n\n\n\n${canonicalized_headers}\n${canonicalized_resource}\ncomp:list\nprefix:blob1\nrestype:container"

    # Decode the Base64 encoded access key, convert to Hex.
    decoded_hex_key="$(printf $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 \
      -H "$x_ms_date_h" \
      -H "$x_ms_version_h" \
      -H "$authorization_header" \
      "https://${storage_account}.${blob_store_url}/${container_name}?comp=list&maxresults=1&prefix=blob1&restype=container"

    #"https://${storage_account}.${blob_store_url}/${container_name}?comp=list&prefix=blob1&restype=container"


    Rahul Naik

    Friday, January 22, 2016 9:07 AM

Answers

  • There's a problem in how you're creating "string_to_sign" variable.

    string_to_sign="${request_method}\n\n\n\n\n\n\n\n\n\n\n\n${canonicalized_headers}\n${canonicalized_resource}\ncomp:list&maxresults=3"

    It should be:

    string_to_sign="${request_method}\n\n\n\n\n\n\n\n\n\n\n\n${canonicalized_headers}\n${canonicalized_resource}\ncomp:list\nmaxresults:3"

    Please see "Constructing the Canonicalized Resource String" section here: https://msdn.microsoft.com/en-us/library/azure/dd179428.aspx.

    Hope this helps.

    • Proposed as answer by Kamalakar K Tuesday, January 26, 2016 6:41 PM
    • Unproposed as answer by Rahul_Naik Wednesday, January 27, 2016 4:03 AM
    • Marked as answer by Kamalakar K Wednesday, January 27, 2016 11:33 AM
    Friday, January 22, 2016 7:02 AM

All replies

  • I am trying to execute List container REST API using Shell script and cURL. Without maxresults option it runs perfectly.

    When I tried to maxresults option in URL it fails. Following is mu shell script. Can anyone please let me know that do we to add maxresults option in string to sign? Whats the problem with my shell scripts.

    ==================

    Shell Script

    ===================

    #!/bin/bash

    # List the blobs in an Azure storage container.

    #echo "usage: ${0##*/} <storage-account-name> <container-name> <access-key>"

    storage_account="<account name>"

    access_key="<Access key>"

    container_name="demo"

    blob_store_url="blob.core.windows.net"

    authorization="SharedKey"

    request_method="GET"

    request_date=$(TZ=GMT date "+%a, %d %h %Y %H:%M:%S %Z")

    storage_service_version="2013-08-15"

    # 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}/"

    string_to_sign="${request_method}\n\n\n\n\n\n\n\n\n\n\n\n${canonicalized_headers}\n${canonicalized_resource}\ncomp:list&maxresults=3"

    # Decode the Base64 encoded access key, convert to Hex.

    decoded_hex_key="$(printf $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 \

      -H "$x_ms_version_h" \

      -H "$x_ms_date_h" \

      -H "$authorization_header" \

      "https://${storage_account}.${blob_store_url}/?comp=list&maxresults=3"

    ====================

    =================

    output

    =================

    # ./list_containers

    <?xml version="1.0" encoding="utf-8"?><Error><Code>AuthenticationFailed</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.

    RequestId:935bfb1c-0001-0027-7203-54ecd6000000

    Time:2016-01-21T04:25:14.6061981Z</Message><AuthenticationErrorDetail>The MAC signature found in the HTTP request 'zKMe+OgxvYyb9khGBtmchgY+bu5E+Q+Fz8IN8HcWtzY=' is not the same as any computed signature. Server used following string to sign: 'GET

    x-ms-date:Thu, 21 Jan 2016 04:25:14 GMT

    x-ms-version:2013-08-15

    /sevenstars/

    comp:list

    maxresults:3'.</AuthenticationErrorDetail></Error>

    Authorization: SharedKey sevenstars:zKMe+OgxvYyb9khGBtmchgY+bu5E+Q+Fz8IN8HcWtzY=

    ===============


    Rahul Naik

    Thursday, January 21, 2016 4:37 AM
  • Hello Rahul,

     

    We are working on the query and would get back to you soon on this. I apologize for the inconvenience and appreciate your time and patience in this matter.

     

    Best Regards,

    Kamalakar K

    Thursday, January 21, 2016 7:42 PM
  • There's a problem in how you're creating "string_to_sign" variable.

    string_to_sign="${request_method}\n\n\n\n\n\n\n\n\n\n\n\n${canonicalized_headers}\n${canonicalized_resource}\ncomp:list&maxresults=3"

    It should be:

    string_to_sign="${request_method}\n\n\n\n\n\n\n\n\n\n\n\n${canonicalized_headers}\n${canonicalized_resource}\ncomp:list\nmaxresults:3"

    Please see "Constructing the Canonicalized Resource String" section here: https://msdn.microsoft.com/en-us/library/azure/dd179428.aspx.

    Hope this helps.

    • Proposed as answer by Kamalakar K Tuesday, January 26, 2016 6:41 PM
    • Unproposed as answer by Rahul_Naik Wednesday, January 27, 2016 4:03 AM
    • Marked as answer by Kamalakar K Wednesday, January 27, 2016 11:33 AM
    Friday, January 22, 2016 7:02 AM
  • Thanks Gaurav for replying on this thread. Its working fine now.

    Rahul Naik

    Wednesday, January 27, 2016 4:23 AM