读取大文件 - Google 驱动器 API 上传 2GB+

Read large files - 2GB+ for Google Drive API Upload

我目前正在开发一个用 C# 编写的小型备份工具,它可以通过 API 将指定文件夹中包含的文件上传到 Google 驱动器。该程序基本上可以正常运行,唯一的问题是它无法处理大于 2GB 的文件。

问题是由下面附上的上传功能本身引起的,它使用字节数组读取文件以随后创建内存流。据我所知(我还是c#的初学者),字节数组在返回溢出异常之前只能包含2GB的信息。为了解决这个问题,我尝试使用 FileStream.Read(下面附上的第二位代码)而不是 System.IO.File.ReadAllBytes,尽管这再次导致字节数组的溢出异常。我知道此时我必须拆分文件,但是,由于 C# 的 GDrive API 文档相当有限——至少从我所看到的来看——以及我对 C# 的了解有限我对如何解决这个问题几乎一无所知。

很抱歉阅读时间过长,非常感谢所有对此问题的帮助。

上传函数V1 (System.IO.File.ReadAllBytes):

    private static Google.Apis.Drive.v3.Data.File UploadFile(Boolean useFolder, String mime, DriveService _service, string _uploadFile, string _parent, string _descrp = "")
    {
        if (System.IO.File.Exists(_uploadFile))
        {
            Google.Apis.Drive.v3.Data.File body = new Google.Apis.Drive.v3.Data.File
            {
                Name = System.IO.Path.GetFileName(_uploadFile),
                Description = _descrp,
                MimeType = mime
            };
            if (useFolder)
            {
                body.Parents = new List<string> { _parent };
            }
            byte[] byteArray = System.IO.File.ReadAllBytes(_uploadFile);
            MemoryStream stream = new System.IO.MemoryStream(byteArray);
            try
            {
                FilesResource.CreateMediaUpload request = _service.Files.Create(body, stream, mime);
                request.SupportsTeamDrives = true;
                request.Upload();
                return request.ResponseBody;
            }
            catch (Exception e)
            {
                Console.WriteLine("Error Occured: " + e);
                return null;
            }
        }
        else
        {
            Console.WriteLine("The file does not exist. 404");
            return null;
        }
    }

上传方法 V2 (FileStream):

    private static Google.Apis.Drive.v3.Data.File UploadFile(Boolean useFolder, String mime, DriveService _service, string _uploadFile, string _parent, string _descrp = "")
    {
        if (System.IO.File.Exists(_uploadFile))
        {
            Google.Apis.Drive.v3.Data.File body = new Google.Apis.Drive.v3.Data.File
            {
                Name = System.IO.Path.GetFileName(_uploadFile),
                Description = _descrp,
                MimeType = mime
            };
            if (useFolder)
            {
                body.Parents = new List<string> { _parent };
            }
            //byte[] byteArray = System.IO.File.ReadAllBytes(_uploadFile);
            using (FileStream fileStream = new FileStream(_uploadFile, FileMode.Open, FileAccess.Read))
            {
                Console.WriteLine("ByteArrayStart");
                byte[] byteArray = new byte[fileStream.Length];
                int bytesToRead = (int)fileStream.Length;
                int bytesRead = 0;
                while (bytesRead > 0)
                {
                    int n = fileStream.Read(byteArray, bytesRead, bytesToRead);
                    if (n == 0)
                    {
                        break;
                    }
                    bytesRead += n;
                    Console.WriteLine("Bytes Read: " + bytesRead);
                    bytesToRead -= n;
                    Console.WriteLine("Bytes to Read: " + bytesToRead);
                }
                bytesToRead = byteArray.Length;
                MemoryStream stream = new System.IO.MemoryStream(byteArray);
                try
                {
                    FilesResource.CreateMediaUpload request = _service.Files.Create(body, stream, mime);
                    request.SupportsTeamDrives = true;
                    request.Upload();
                    return request.ResponseBody;
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error Occured: " + e);
                    return null;
                }
            }
        }
        else
        {
            Console.WriteLine("The file does not exist. 404");
            return null;
        }
    }

MemoryStream 的构造函数仅适用于限制为 Int32.MaxValue 字节的字节数组。为什么不直接使用您的 FileStream 对象?

var fileMetadata = new Google.Apis.Drive.v3.Data.File()
{
    Name = "flag.jpg"
};

FilesResource.CreateMediaUpload request;
using (var stream = new System.IO.FileStream(@"C:\temp\flag.jpg", System.IO.FileMode.Open))
{
    request = service.Files.Create(fileMetadata, stream, "image/jpeg");
    request.Fields = "id";
    request.Upload();
}
var file = request.ResponseBody;

确实是一个这么大的文件,您应该使用可恢复上传,但我将不得不为此挖掘一些示例代码。