读取大文件-2GB以上用于Google Drive API上传

问题描述 投票:1回答:2

我目前正在使用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;
        }
    }

我目前正在使用C#编写的小型备份工具,该工具应该通过其API将指定文件夹中包含的文件上传到Google云端硬盘。该程序的主要功能是...

c# google-api google-drive-api google-api-dotnet-client
2个回答
3
投票

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


0
投票

我相信2GB的限制适用于32位进程。如果使用/ LARGEADDRESSAWARE编译进程,则可以将其增加到3GB。如果它是64位进程,则将有4GB的限制,并且默认情况下启用了/ LARGEADDRESSAWARE。

© www.soinside.com 2019 - 2024. All rights reserved.