C#-以字节为单位从Google云端硬盘下载 [英] C# - Downloading from Google Drive in byte chunks

查看:96
本文介绍了C#-以字节为单位从Google云端硬盘下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为网络连接较差的环境进行开发.我的应用程序可帮助自动为用户下载所需的Google云端硬盘文件.它适用于较小的文件(从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来完成.我一遍又一遍地阅读了,并且我尝试了以下代码:

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.

查看

核心下载逻辑.我们下载媒体并将其写入到 一次输出流ChunkSize字节,提高ProgressChanged 每个块之后的事件.碎片行为在很大程度上是历史性的 工件:先前的实现发出了多个Web请求,每个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/服务器进行了一些假设.

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

  • 服务器将允许分块下载文件所需的多个请求.不知道请求是否受到限制.
  • 服务器仍然像开发人员文档中所述接受Range标头

这篇关于C#-以字节为单位从Google云端硬盘下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆