我正在尝试弄清楚如何在vb.net上使用Google驱动器v3可恢复上传来上传大文件。
我已经检查了这个https://developers.google.com/drive/v3/web/resumable-upload,并且能够使用基本上传来上传小文件,但无法使用vb.net找到任何可恢复上传的代码示例。
答案 0 :(得分:0)
我设法完成了它,这是最终的代码以防其他人需要:
Imports System.IO
Imports System.Net.Http
Imports System.Net.Http.Headers
Imports System.Threading
Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.Drive.v3
Imports Google.Apis.Services
Imports Google.Apis.Upload
Imports Google.Apis.Util.Store
Module Module1
Dim credential As UserCredential
Dim ApplicationName As String = "NET"
Dim Scopes As String() = {DriveService.Scope.Drive}
Sub Main()
Using stream = New FileStream("client_secret.json", FileMode.Open, FileAccess.Read)
Dim credPath As String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)
credPath = Path.Combine(credPath, ".credentials/drive-dotnet-quickstart.json")
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, Scopes, "user", CancellationToken.None, New FileDataStore(credPath, True)).Result
Console.WriteLine(Convert.ToString("Credential file saved to: ") & credPath)
End Using
Dim service = New DriveService(New BaseClientService.Initializer() With {
.HttpClientInitializer = credential,
.ApplicationName = ApplicationName
})
Task.Run(Async Function()
Await UploadFileAsync(service, credential.Token.AccessToken, "welcome.mp4", "video/mp4", DateTime.Now.ToString("HH:mm:ss tt"))
End Function).GetAwaiter().GetResult()
Console.WriteLine("Press any key to continue...")
Console.ReadKey()
End Sub
Private Async Function UploadFileAsync(service As DriveService, accessToken As String, filePath As String, mimeType As String, newFileName As String) As Task(Of Boolean)
Dim uri As Uri
Dim uploadStream = New System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
Using client = New HttpClient()
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken)
client.DefaultRequestHeaders.Add("X-Upload-Content-Type", mimeType)
client.DefaultRequestHeaders.Add("X-Upload-Content-Length", uploadStream.Length.ToString())
Dim request = New HttpRequestMessage(HttpMethod.Post, "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable")
request.Content = New StringContent("{""name"": """ + newFileName + """, ""parents"":[""folder_id_goes_here""]}")
request.Content.Headers.ContentType = New MediaTypeHeaderValue("application/json")
Dim response = Await client.SendAsync(request)
uri = response.Headers.Location
End Using
Dim uploader = ResumableUpload.CreateFromUploadUri(uri, uploadStream, New ResumableUploadOptions())
AddHandler uploader.ProgressChanged, AddressOf Upload_ProgressChanged
Dim progress As IUploadProgress
progress = Await uploader.UploadAsync()
If progress.Status <> UploadStatus.Completed Then
While (Await uploader.ResumeAsync()).Status <> UploadStatus.Completed
Await Task.Delay(10000)
End While
End If
uploadStream.Dispose()
Return True
End Function
Private Sub Upload_ProgressChanged(progress As IUploadProgress)
Console.WriteLine(progress.Status.ToString() & " " & progress.BytesSent)
End Sub
End Module