4

Iam using .net Azure storage client library to retrieve data from server.

My Entity contains more than 10000 records & it is retrieving 1000 records at once & giving response Headers x-ms-continuation-NextPartitionKey & x-ms-continuation-NextRowKey

I referred this

https://learn.microsoft.com/en-us/rest/api/storageservices/Query-Entities?redirectedfrom=MSDN]

But did not understand how to use the those headers next time to get continuous records using Rest API

string storageAccount = "MyAccount";
string accessKey = "MYAccessKey";
string TableName = "TableName";
string uri = @"https://" + storageAccount + ".table.core.windows.net/" + TableName  + "?$top=100";
// Web request 
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.Method = "GET";
request.ContentType = "application/json";
request.Accept = "application/json;odata=nometadata";
request.Headers["x-ms-date"] = DateTime.UtcNow.ToString("R", System.Globalization.CultureInfo.InvariantCulture);
request.Headers["x-ms-version"] = "2015-04-05";           
string stringToSign = request.Headers["x-ms-date"] + "\n";    
stringToSign += "/" + storageAccount + "/" + TableName;
System.Security.Cryptography.HMACSHA256 hasher = new System.Security.Cryptography.HMACSHA256(Convert.FromBase64String(accessKey));
string strAuthorization = "SharedKeyLite " + storageAccount + ":" + System.Convert.ToBase64String(hasher.ComputeHash(System.Text.Encoding.UTF8.GetBytes(stringToSign)));


request.Headers["Authorization"] = strAuthorization;

Task<WebResponse> response = request.GetResponseAsync();
HttpWebResponse responseresult = (HttpWebResponse)response.Result;
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49
champ
  • 41
  • 1
  • 3

2 Answers2

2

If you want to continue in query, use original query, but add parameters to request - not to headers, to query:

http://account.table....?query...&NextPartitionKey={value from x-ms-continuation-NextPartitionKey response header}&NextRowKey={value from x-ms-continuation-NextRowKey response header}
Michal Dobrodenka
  • 1,104
  • 8
  • 27
0

You're really doing it the hard way, I mean, are you sure you want to manually write all requests? Looks very error prone to me. With the WindowsAzure.Storage NuGet Package, you get many function which wrap this for you. Using the Continuation Token is easy there:

Example copied from Microsoft Docs:

//List blobs to the console window, with paging.
Console.WriteLine("List blobs in pages:");

int i = 0;
BlobContinuationToken continuationToken = null;
BlobResultSegment resultSegment = null;

//Call ListBlobsSegmentedAsync and enumerate the result segment returned, while the continuation token is non-null.
//When the continuation token is null, the last page has been returned and execution can exit the loop.
do
{
    //This overload allows control of the page size. You can return all remaining results by passing null for the maxResults parameter,
    //or by calling a different overload.
    resultSegment = await container.ListBlobsSegmentedAsync("", true, BlobListingDetails.All, 10, continuationToken, null, null);
    if (resultSegment.Results.Count<IListBlobItem>() > 0) { Console.WriteLine("Page {0}:", ++i); }
    foreach (var blobItem in resultSegment.Results)
    {
        Console.WriteLine("\t{0}", blobItem.StorageUri.PrimaryUri);
    }
    Console.WriteLine();

    //Get the continuation token.
    continuationToken = resultSegment.ContinuationToken;
}
while (continuationToken != null);

We made great experience with those Microsoft NuGet Packages and highly recommend to use them.

thmshd
  • 5,729
  • 3
  • 39
  • 67
  • 1
    Like this i tried, it is working fine but need to do same thing using rest API Calls – champ Nov 29 '17 at 09:37
  • According to Documentation at https://learn.microsoft.com/en-us/rest/api/storageservices/query-timeout-and-pagination you should be able to read the Keys from the Header and just pass them as the URL parameter (See **Sample Response Headers and Subsequent Request**) – thmshd Nov 29 '17 at 11:29
  • 1
    Also, as a next hint... the NuGet Library is internally using the REST API. If something works as expected using the Library, you might want to monitor the HTTP Requests with a Tool like_Fiddler_. You should be able to inspect the subsequent calls and get the idea how they're structured – thmshd Nov 29 '17 at 11:36
  • 1
    @thmshd it looks like the OP asked specifically for Rest API – Muhammad Mamoor Khan Apr 30 '19 at 16:08
  • @MuhammadMamoorKhan he/she surely did. So my answer was more a comment but too big to fit in the comments field. Sometimes people are not aware of API wrappers. Thanks. – thmshd May 02 '19 at 06:08