如何使用GoogleDrive REST API将大文件(大于1 GB)上传到Google云端硬盘 [英] How to upload a large file (1 GB +) to Google Drive using GoogleDrive REST API

查看:143
本文介绍了如何使用GoogleDrive REST API将大文件(大于1 GB)上传到Google云端硬盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用GoogleDrive API将大文件(大于1 GB)上传到Google云端硬盘.我的代码可以在较小的文件上正常工作.但是当涉及到较大的文件时,会发生错误.

I am tying to upload large files(1 GB+) to Google Drive using GoogleDrive API. My code works fine with smaller files. But when it comes to larger files error occurs.

在将文件转换为byte []的代码部分中发生错误.

Error occurs in the code part where the the file is converted into byte[].

byte[] data = System.IO.File.ReadAllBytes(filepath); 

内存不足异常在这里引发.

Out of memory exception is thrown here.

推荐答案

可能您遵循了 developers.google 建议,而您正在这样做

Probably you followed developers.google suggestions and you are doing this

byte[] byteArray = System.IO.File.ReadAllBytes(filename);
MemoryStream stream = new MemoryStream(byteArray);
try {
  FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, mimeType);
  request.Upload();

我不知道为什么建议将整个文件放在一个字节数组中,然后在其上创建一个MemoryStream. 我认为这是一种更好的方法:

I have no idea why the suggest to put the whole file in a byte array and then create a MemoryStream on it. I think that a better way is this:

using(var stream = new System.IO.FileStream(filename,
                                            System.IO.FileMode.Open, 
                                            System.IO.FileAccess.Read))
{
    try 
    {
       FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, mimeType);
       request.Upload();
       .
       .
       .
}

这篇关于如何使用GoogleDrive REST API将大文件(大于1 GB)上传到Google云端硬盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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