none
Query content of an azure table (with a partition key) and specify correct Shared Key (signature)

    Question

  • Most samples on the net on how to create the Shared Key or Shared Key Lite has very simple queries, and I am able to get that to work. But when I try a more advanced query I get errors.

    I use Shared Key Lite as it is the simplest.

    I get this to work (C# code) to get all content of the table:

    string table = "mytable";
    string path = table;
    string uri = "https://" + Account + ".table.core.windows.net/" + path;
    string canonicalizedResource = "/" + Account + "/" + table;
    
    var client = new HttpClient();
    var request = new HttpRequestMessage();
    request.RequestUri = new Uri(uri);
    request.Method = HttpMethod.Get;
    string dateInRfc1123Format = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture);
    string authorization = "SharedKeyLite " + Account + ":" + GetSignature(dateInRfc1123Format, canonicalizedResource);
    request.Headers.Add("x-ms-date", dateInRfc1123Format);
    request.Headers.Add("x-ms-version", "2015-12-11");
    request.Headers.Add("Authorization", authorization);
    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("odata=nometadata"));
    request.Headers.AcceptCharset.Add(new StringWithQualityHeaderValue("UTF-8"));
    var response = client.SendAsync(request).Result;

    But when I want to specify a partition key to filter the content it fails. I have tried to add this as the third line in the code above:

    path += "(PartitionKey='12345')";

    and several other variants, but I get errors like:

    Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.

    or

     {"odata.error":{"code":"InvalidInput","message":{"lang":"en-US","value":"One of the request inputs is not valid....

    Please enlighten me on how to specify the partitionkey and the signature part adjusted for it.



    • Edited by Vilhelm Hei Sunday, November 6, 2016 6:33 PM
    Sunday, November 6, 2016 6:32 PM

All replies

  • Hello,

    We are checking 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.

    Regards,
    Sumanth BM


    Monday, November 7, 2016 1:30 PM
    Moderator
  • The biggest problem with this topic is the lack of good documentation.
    The best I have found is this:

    https://msdn.microsoft.com/en-us/library/azure/dd179428.aspx

    However, this one is not answering my question.

    After a lot of trial and error I have found a working solution. I have found that anything before any '?' shall be included in the portion that shall be included in the canonicalizedResource. Moreover, if using the syntax that included PartitonKey and RowKey within the parantesis, then it does not work to include only PartitionKey, both must be included.
    I.e. these syntaxes work:

       .../mytable

       .../mytable()

       .../mytable(PartionKey='12345', RowKey='98765432')

    But these do not work:

       .../mytable(PartionKey='12345')

       .../mytable(PartionKey='12345', RowKey='')

    I.e. when I want to specify only the PartitionKey, I must use query syntax. I got the following code to work:

    string table = "mytable";
    string path = table + "()";
    string query = "?$filter=PartitionKey eq '" + userId + "'";
    query = query.Replace(" ", "%20");
    string canonicalizedResource = "/" + Account + "/" + path;
    string uri = "https://" + Account + ".table.core.windows.net/" + path + query;
    


    Monday, November 7, 2016 2:21 PM
  • Hi,

    Thank you for your time and patience.

    Try to use the  storage client library and to look at our source code from the below mentioned link;  www.github.com/Azure/azure-storage-net

    Hope this helps :)

    Regards,
    Sumanth BM

    Monday, November 7, 2016 6:15 PM
    Moderator
  • Thank you, can you point me to where in that source the Shared Key or Shared Key Lite is built?

    Regards,

    Vilhelm Heiberg

    Tuesday, November 8, 2016 9:43 AM
  • Hi,

    Thank you for your time and patience!

    The below mentioned link may help you in this scenario; https://msdn.microsoft.com/en-us/library/azure/dd179428.aspx

    Regards,
    Sumanth BM

    Tuesday, November 15, 2016 2:01 PM
    Moderator