【问题标题】:How to use continuation tokens in azure table Using Rest API如何在天蓝色表中使用延续令牌使用 Rest API
【发布时间】:2018-05-12 19:52:20
【问题描述】:

我正在使用 .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;

【问题讨论】:

    标签: c# rest azure-storage


    【解决方案1】:

    如果您想继续查询,请使用原始查询,但将参数添加到请求中 - 而不是标题,查询:

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

    【讨论】:

      【解决方案2】:

      你真的很辛苦,我的意思是,你确定要手动编写所有请求吗?对我来说看起来很容易出错。使用WindowsAzure.Storage NuGet 包,您可以获得许多为您包装它的功能。在那里使用延续令牌很容易:

      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 包方面获得了丰富的经验,并强烈推荐使用它们。

      【讨论】:

      • 像我这样尝试过,它工作正常,但需要使用其他 API 调用来做同样的事情
      • 根据docs.microsoft.com/en-us/rest/api/storageservices/… 的文档,您应该能够从标头中读取密钥并将它们作为 URL 参数传递(请参阅示例响应标头和后续请求
      • 另外,作为下一个提示...... NuGet 库在内部使用 REST API。如果使用库按预期工作,您可能需要使用类似_Fiddler_ 的工具监控 HTTP 请求。您应该能够检查后续调用并了解它们的结构
      • @thmshd 看起来 OP 专门询问了 Rest API
      • @MuhammadMamoorKhan 他/她肯定做到了。所以我的回答更多的是评论,但太大了,不适合 cmets 领域。有时人们不知道 API 包装器。谢谢。
      猜你喜欢
      • 2021-01-31
      • 1970-01-01
      • 2020-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-16
      • 2018-01-15
      • 1970-01-01
      相关资源
      最近更新 更多