简体   繁体   English

如何使用GoogleDrive REST API将大文件(大于1 GB)上传到Google云端硬盘

[英]How to upload a large file (1 GB +) to Google Drive using GoogleDrive REST API

I am tying to upload large files(1 GB+) to Google Drive using GoogleDrive API. 我想使用GoogleDrive API将大文件(大于1 GB)上传到Google云端硬盘。 My code works fine with smaller files. 我的代码可以在较小的文件上正常工作。 But when it comes to larger files error occurs. 但是,当涉及到较大的文件时,会发生错误。

Error occurs in the code part where the the file is converted into byte[]. 在将文件转换为byte []的代码部分中发生错误。

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

Out of memory exception is thrown here. 内存不足异常在这里引发。

Probably you followed developers.google suggestions and you are doing this 可能您遵循了developers.google的建议,并且正在执行此操作

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

I have no idea why the suggest to put the whole file in a byte array and then create a MemoryStream on it. 我不知道为什么建议将整个文件放入字节数组,然后在其上创建MemoryStream 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();
       .
       .
       .
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM