如何在azure表中使用continuation令牌使用Rest API

时间:2017-11-29 07:20:41

标签: c# rest azure-storage

我使用.net Azure存储客户端库从服务器检索数据。

我的实体包含超过10000条记录&它一次检索1000条记录。给出响应标题x-ms-continuation-NextPartitionKey& x-ms-continuation-NextRowKey

我提到了这个

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

但是下次不知道如何使用这些标题来使用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;

2 个答案:

答案 0 :(得分:0)

如果要继续查询,请使用原始查询,但要添加参数以进行请求-而不是标题以进行查询:

http://account.table....?query...&NextPartitionKey={value from x-ms-continuation-NextPartitionKey response header}&NextRowKey={value from x-ms-continuation-NextRowKey response header}

答案 1 :(得分:-1)

你真的这么做,我的意思是,你确定要手动编写所有请求吗?看起来很容易出错。使用WindowsAzure.Storage NuGet包,您可以获得许多功能,为您提供此功能。使用Continuation Token很容易:

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);

我们对这些Microsoft NuGet软件包有很好的体验,强烈建议您使用它们。