Tuesday, May 23, 2017

Google Drive V3 Resumable Download

Leave a Comment

I use this code to download a file from Google Drive using Google Drive api V3:

    Function gDownload() As Boolean              Dim fileID As String = "0B-W1aRTTOB1QM3hPd0dOUFVObHM"             Dim stream = New System.IO.MemoryStream()                     Dim r = service.Files.Get(fileID)             Dim m = r.MediaDownloader             m.ChunkSize = 256 * 1024             AddHandler m.ProgressChanged, AddressOf Download_ProgressChanged         mStart:                 r.Download(stream)                 Return True ' or False if download failed      End Function     Private Sub Download_ProgressChanged(s As IDownloadProgress)             Console.WriteLine(s.Status.ToString & "   " & s.BytesDownloaded)    End Sub 

This works fine with stable connection but if I lose connection it stops and waits forever even if I reconnect again.

I don't have this problem with update (upload) function in this code:

Function gUpload() As Boolean          Dim fileID As String = "0B-W1aRTTOB1QM3hPd0dOUFVObHM"         Dim stream As New System.IO.FileStream("D:\gtest\Test.mp4", System.IO.FileMode.Open)         Dim fBody As File = New File With {.Name = "Test.mp4"}          Dim r = service.Files.Update(fBody, fileID, stream, "application/octet-stream")         r.ChunkSize = ResumableUpload.MinimumChunkSize         AddHandler r.ProgressChanged, AddressOf Upload_ProgressChanged    mStart:         r.Resume()         If r.GetProgress.Status = 3 Then ' UploadStatus.Completed             Return True         Else             If MessageBox.Show("Failed. do you want to resume?", "Failed", MessageBoxButtons.YesNo) = DialogResult.Yes Then                 GoTo mStart             End If         End If         Return False End Function      Private Sub Upload_ProgressChanged(s As IUploadProgress)     Console.WriteLine(s.Status.ToString & "   " & s.BytesSent) End Sub 

This works exactly as I want, if I lose connection for some time (15~30s) it gives a message that upload is failed and gives a user a choice to resume or exit. everything work perfectly.

So my question is: how to make the download function works like the upload function or at least make it don't wait forever and gives a fail message.

1 Answers

Answers 1

I think you should change your Download_ProgressChanged procedure like tihs:

Private Sub Download_ProgressChanged(s As IDownloadProgress)   Select Case s.Status     Case DownloadStatus.Downloading         If True Then             Console.WriteLine(progress.BytesDownloaded)             Exit Select         End If     Case DownloadStatus.Completed         If True Then             Console.WriteLine("Download complete.")             Exit Select         End If     Case DownloadStatus.Failed         If True Then             Console.WriteLine("Download failed.")             Exit Select         End If   End Select End Sub 

Link is here: https://developers.google.com/drive/v3/web/manage-downloads.

You can use partial download bu you have to calculate start and end bytes.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment