1. <tfoot id='91zae'></tfoot>

      <i id='91zae'><tr id='91zae'><dt id='91zae'><q id='91zae'><span id='91zae'><b id='91zae'><form id='91zae'><ins id='91zae'></ins><ul id='91zae'></ul><sub id='91zae'></sub></form><legend id='91zae'></legend><bdo id='91zae'><pre id='91zae'><center id='91zae'></center></pre></bdo></b><th id='91zae'></th></span></q></dt></tr></i><div id='91zae'><tfoot id='91zae'></tfoot><dl id='91zae'><fieldset id='91zae'></fieldset></dl></div>

          <bdo id='91zae'></bdo><ul id='91zae'></ul>
      1. <small id='91zae'></small><noframes id='91zae'>

      2. <legend id='91zae'><style id='91zae'><dir id='91zae'><q id='91zae'></q></dir></style></legend>

        C# - 以字节块从 Google Drive 下载

        时间:2023-08-28
          <tfoot id='yKcv0'></tfoot>
          1. <small id='yKcv0'></small><noframes id='yKcv0'>

              <bdo id='yKcv0'></bdo><ul id='yKcv0'></ul>

                <legend id='yKcv0'><style id='yKcv0'><dir id='yKcv0'><q id='yKcv0'></q></dir></style></legend>
                  <i id='yKcv0'><tr id='yKcv0'><dt id='yKcv0'><q id='yKcv0'><span id='yKcv0'><b id='yKcv0'><form id='yKcv0'><ins id='yKcv0'></ins><ul id='yKcv0'></ul><sub id='yKcv0'></sub></form><legend id='yKcv0'></legend><bdo id='yKcv0'><pre id='yKcv0'><center id='yKcv0'></center></pre></bdo></b><th id='yKcv0'></th></span></q></dt></tr></i><div id='yKcv0'><tfoot id='yKcv0'></tfoot><dl id='yKcv0'><fieldset id='yKcv0'></fieldset></dl></div>
                    <tbody id='yKcv0'></tbody>
                  本文介绍了C# - 以字节块从 Google Drive 下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我目前正在为网络连接较差的环境进行开发.我的应用程序有助于自动为用户下载所需的 Google Drive 文件.它对于小文件(从 40KB 到 2MB)工作得相当好,但对于大文件(9MB)却经常失败.我知道这些文件大小可能看起来很小,但就我客户的网络环境而言,Google Drive API 经常出现 9MB 文件失败.

                  I'm currently developing for an environment that has poor network connectivity. My application helps to automatically download required Google Drive files for users. It works reasonably well for small files (ranging from 40KB to 2MB), but fails far too often for larger files (9MB). I know these file sizes might seem small, but in terms of my client's network environment, Google Drive API constantly fails with the 9MB file.

                  我已经得出结论,我需要以较小的字节块下载文件,但我不知道如何使用 Google Drive API 做到这一点.我一遍又一遍地阅读了 this,并尝试了以下代码:

                  I've concluded that I need to download files in smaller byte chunks, but I don't see how I can do that with Google Drive API. I've read this over and over again, and I've tried the following code:

                  // with the Drive File ID, and the appropriate export MIME type, I create the export request
                  var request = DriveService.Files.Export(fileId, exportMimeType);
                  
                  // take the message so I can modify it by hand
                  var message = request.CreateRequest();
                  var client = request.Service.HttpClient;
                  
                  // I change the Range headers of both the client, and message
                  client.DefaultRequestHeaders.Range =
                      message.Headers.Range =
                      new System.Net.Http.Headers.RangeHeaderValue(100, 200);
                  var response = await request.Service.HttpClient.SendAsync(message);
                  
                  // if status code = 200, copy to local file
                  if (response.IsSuccessStatusCode)
                  {
                      using (var fileStream = new FileStream(downloadFileName, FileMode.CreateNew, FileAccess.ReadWrite))
                      {
                          await response.Content.CopyToAsync(fileStream);
                      }
                  }
                  

                  但是,生成的本地文件(来自 fileStream)仍然是全长的(即 ​​40KB 驱动器文件为 40KB 文件,9MB 文件为 500 内部服务器错误).在旁注中,我还尝试了 ExportRequest.MediaDownloader.ChunkSize,但据我观察,它只会改变 ExportRequest.MediaDownloader.ProgressChanged 回调的频率调用(即如果 ChunkSize 设置为 256 * 1024,回调将每 256KB 触发一次).

                  The resultant local file (from fileStream) however, is still full-length (i.e. 40KB file for the 40KB Drive file, and a 500 Internal Server Error for the 9MB file). On a sidenote, I've also experimented with ExportRequest.MediaDownloader.ChunkSize, but from what I observe it only changes the frequency at which the ExportRequest.MediaDownloader.ProgressChanged callback is called (i.e. callback will trigger every 256KB if ChunkSize is set to 256 * 1024).

                  我该如何继续?

                  推荐答案

                  您似乎正朝着正确的方向前进.从您上次的评论来看,请求将根据块大小更新进度,因此您的观察是准确的.

                  You seemed to be heading in the right direction. From your last comment, the request will update progress based on the chunk size, so your observation was accurate.

                  查看 SDK 中 MediaDownloader 的源代码找到了以下(强调我的)

                  核心下载逻辑.我们下载媒体并将其写入一次输出流 ChunkSize 个字节,提高 ProgressChanged每个块之后的事件.分块行为在很大程度上是历史性的工件:先前的实现发出多个 Web 请求,每个请求对于 ChunkSize 字节.现在我们在一个请求中完成所有事情,但是 API为了兼容性,保留了客户端可见的行为.

                  The core download logic. We download the media and write it to an output stream ChunkSize bytes at a time, raising the ProgressChanged event after each chunk. The chunking behavior is largely a historical artifact: a previous implementation issued multiple web requests, each for ChunkSize bytes. Now we do everything in one request, but the API and client-visible behavior are retained for compatibility.

                  您的示例代码只会下载一个从 100 到 200 的块.使用这种方法,您必须跟踪索引并手动下载每个块,将它们复制到每个部分下载的文件流中

                  Your example code will only download one chunk from 100 to 200. Using that approach you would have to keep track of an index and download each chunk manually, copying them to the file stream for each partial download

                  const int KB = 0x400;
                  int ChunkSize = 256 * KB; // 256KB;
                  public async Task ExportFileAsync(string downloadFileName, string fileId, string exportMimeType) {
                  
                      var exportRequest = driveService.Files.Export(fileId, exportMimeType);
                      var client = exportRequest.Service.HttpClient;
                  
                      //you would need to know the file size
                      var size = await GetFileSize(fileId);
                  
                      using (var file = new FileStream(downloadFileName, FileMode.CreateNew, FileAccess.ReadWrite)) {
                  
                          file.SetLength(size);
                  
                          var chunks = (size / ChunkSize) + 1;
                          for (long index = 0; index < chunks; index++) {
                  
                              var request = exportRequest.CreateRequest();
                  
                              var from = index * ChunkSize;
                              var to = from + ChunkSize - 1;
                  
                              request.Headers.Range = new RangeHeaderValue(from, to);
                  
                              var response = await client.SendAsync(request);
                  
                              if (response.StatusCode == HttpStatusCode.PartialContent || response.IsSuccessStatusCode) {
                                  using (var stream = await response.Content.ReadAsStreamAsync()) {
                                      file.Seek(from, SeekOrigin.Begin);
                                      await stream.CopyToAsync(file);
                                  }
                              }
                          }
                      }
                  }
                  
                  private async Task<long> GetFileSize(string fileId) {
                      var file = await driveService.Files.Get(fileId).ExecuteAsync();
                      var size = file.size;
                      return size;
                  }
                  

                  这段代码对驱动 api/server 做了一些假设.

                  This code makes some assumptions about the drive api/server.

                  • 服务器将允许分块下载文件所需的多个请求.不知道请求是否受到限制.
                  • 服务器仍然接受 Range 标头,如开发人员文档中所述
                  • That the server will allow the multiple requests needed to download the file in chunks. Don't know if requests are throttled.
                  • That the server still accepts the Range header like stated in the developer documenation

                  这篇关于C# - 以字节块从 Google Drive 下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Google Drive API - 从服务帐户转移所有权 下一篇:如何使用 .NET API 在 Google Drive 中创建文件夹?

                  相关文章

                  最新文章

                    <small id='4fGdz'></small><noframes id='4fGdz'>

                    1. <tfoot id='4fGdz'></tfoot>
                      • <bdo id='4fGdz'></bdo><ul id='4fGdz'></ul>
                    2. <i id='4fGdz'><tr id='4fGdz'><dt id='4fGdz'><q id='4fGdz'><span id='4fGdz'><b id='4fGdz'><form id='4fGdz'><ins id='4fGdz'></ins><ul id='4fGdz'></ul><sub id='4fGdz'></sub></form><legend id='4fGdz'></legend><bdo id='4fGdz'><pre id='4fGdz'><center id='4fGdz'></center></pre></bdo></b><th id='4fGdz'></th></span></q></dt></tr></i><div id='4fGdz'><tfoot id='4fGdz'></tfoot><dl id='4fGdz'><fieldset id='4fGdz'></fieldset></dl></div>
                    3. <legend id='4fGdz'><style id='4fGdz'><dir id='4fGdz'><q id='4fGdz'></q></dir></style></legend>