muhammedkasva
muhammedkasva

Reputation: 672

Google Drive Api .Net Upload Error

I decided to use Google drive api in my WPF Project.I searched many documents and samples.I learnt and succeeded.Everyhing works well.I used this function for inserting/uploading.

 public static Google.Apis.Drive.v2.Data.File InsertFile(DriveService service, String title, String description, String parentId, String mimeType, String filename)
        {
            Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
            body.Title = title;
            body.Description = description;
            body.MimeType = mimeType;
            if (!String.IsNullOrEmpty(parentId))
            {
                body.Parents = new List<ParentReference>() { new ParentReference() { Id = parentId } };
            }
            byte[] byteArray = System.IO.File.ReadAllBytes(filename);
            MemoryStream stream = new MemoryStream(byteArray);
            try
            {
                FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, mimeType);
                request.Upload();
                Google.Apis.Drive.v2.Data.File file = request.ResponseBody;
                return file;
            }
            catch (Exception e)
            {
                Console.WriteLine("An error occurred: " + e.Message);
            }
        }

When I want to upload a small sized file to google drive,it works. But I choose to upload large sized file,it gives an error and fails.I got this error

System.Net.WebException was caught HResult=-2146233079 Message=The request was aborted: The request was canceled.Source=System StackTrace:
   at System.Net.ConnectStream.InternalWrite(Boolean async, Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
   at System.Net.ConnectStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   at Google.Apis.Upload.ResumableUpload`1.SendChunk(Stream stream, Uri uri, Int64 position)
   at Google.Apis.Upload.ResumableUpload`1.Upload()
   at Google.Apis.Util.Utilities.InsertFile(DriveService service, String title, String description, String parentId, String mimeType, String filename) in ..

I seek this error and meet same problem but I can't understand where I'm wrong.Can anyone help me or fix my code clearly. Thanks :)

Upvotes: 0

Views: 1731

Answers (2)

Andrew
Andrew

Reputation: 71

Try changing the chunk size of the request.

Using a fast internet connection we had no issues.

However moving to an ADSL connection we noticed that it would timeout with any files > 5MB.

We set ours to

request.ChunkSize = 256 * 1024;

By default Google uses 10,485,760 bytes, which is 10MB. Therefore if you cannot upload 10MB within the time out period, you will get your error.

To help you debug the issue, subscribe to the ProgressChanged event and output everytime it gets hit

request.ProgressChanged += request_ProgressChanged;

....

static void request_ProgressChanged(Google.Apis.Upload.IUploadProgress obj)
{
    var output = String.Format("Status: {0} Bytes: {1}  Exception: {2}", obj.Status, obj.BytesSent, obj.Exception);
    System.Diagnostics.Debug.WriteLine(output);
}

My personal opinion is to receive a response every 10-20 seconds. 60 seconds + is too long.

You can also use something like http://www.speedtest.net/ to work out your upload speed and to determine a reliable chunk size without causing too much overhead.

Upvotes: 0

Kiran
Kiran

Reputation: 57949

I see that you are reading all the content of the file into a MemoryStream which would obviously consume huge amount of memory when uploading large files. Can you avoid it?

Can you just do like below and try?

FilesResource.InsertMediaUpload request = service.Files.Insert(body, File.OpenRead(filename), mimeType);

Edited:

Also, If you are trying to upload large files, I think you should probably look into increasing the timeout value to prevent your request from being aborted.

Upvotes: 2

Related Questions