我目前正在为网络连接较差的环境进行开发.我的应用程序有助于自动为用户下载所需的 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
标头,如开发人员文档中所述Range
header like stated in the developer documenation这篇关于C# - 以字节块从 Google Drive 下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!