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

人盡茶涼 提交于 2020-01-17 01:39:07

问题


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.

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.


回答1:


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();

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();
       .
       .
       .
}


来源:https://stackoverflow.com/questions/32992970/how-to-upload-a-large-file-1-gb-to-google-drive-using-googledrive-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!