Google Drive V3 Resumable Download

时间:2017-04-24 16:21:03

标签: vb.net google-drive-api

我使用此代码使用Google Drive api V3从Google云端硬盘下载文件:

    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

这可以很好地与稳定的连接,但如果我失去连接它会停止并等待永远,即使我再次重新连接。

此代码中的更新(上传)功能没有此问题:

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

这完全符合我的要求,如果我失去连接一段时间(15~30s),它会发出上传失败的消息,并让用户选择恢复或退出。一切都很完美。

所以我的问题是:如何使下载功能像上传功能一样工作,或者至少让它不会永远等待并发出失败信息。

2 个答案:

答案 0 :(得分:1)

我认为你应该改变你的Download_ProgressChanged程序,如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

链接在这里:https://developers.google.com/drive/v3/web/manage-downloads

您可以使用部分下载来计算开始和结束字节。

答案 1 :(得分:1)

我有同样的问题,我暂时解决了“while”

    Request.DownloadAsync(Stream)

    Dim bytes_downloaded As Long = 0
    Dim not_downloaded As Integer = 0
    Dim progress As Integer = Stream.Length
    Do While progress < file_size
        progress = Stream.Length

        If progress = bytes_downloaded Then
            not_downloaded += 1
            If not_downloaded >= 900 Then '90 seconds
                ' msg download falhou
                Exit Do
            End If
        Else
            not_downloaded = 0
        End If
        bytes_downloaded = progress
        Threading.Thread.Sleep(100)
    Loop