注意:实际上是试图上传.bak文件,所以mimetype不同。
我将.txt的模仿类型更改为纯文本/文本,但存在如下所示的相同问题。
我正在尝试从vb.net应用程序上传.bak文件。
我正在尝试将其上传到特定的文件夹中。
我同时尝试了Google身份验证v2和v3。
请求上传时,一切正常。甚至数据都是从我的计算机发送的(在任务管理器中可以看到发送速度),但实际上并没有上传文件,消息框响应中也没有得到任何信息。
下面是我尝试的代码。
Private Sub CreateService()
Dim ClientID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Dim Secret = "xxxxxxxxxxxxxxxxxxxxxxx"
Dim uc As UserCredential = GoogleWebAuthorizationBroker.AuthorizeAsync(New ClientSecrets() With {.ClientId = ClientID, .ClientSecret = Secret}, {Google.Apis.Drive.v3.DriveService.Scope.Drive}, "user", CancellationToken.None).Result
service2 = New Google.Apis.Drive.v2.DriveService(New BaseClientService.Initializer() With {.HttpClientInitializer = uc, .ApplicationName = "myprojectGDriveApi"})
service3 = New Google.Apis.Drive.v3.DriveService(New BaseClientService.Initializer() With {.HttpClientInitializer = uc, .ApplicationName = "myprojectGDriveApi"})
End Sub
通过Google auth V2,
Private Sub Uploadfile2(filepath As String)
If service2.ApplicationName <> "myprojectGDriveApi" Then CreateService()
''-----------------------------------UPLOADS A FILE--------------------------------------
'----------------------------Getting Folder ID-----------------------------
Dim findrootid = New File() With {
.Title = "NSQLBACKUP",
.MimeType = "application/vnd.google-apps.folder"
}
Dim findrequest As Google.Apis.Drive.v2.FilesResource.ListRequest = service2.Files.List
Dim list1 As FileList = findrequest.Execute()
Dim FolderID As String = ""
For Each item As File In list1.Items
If item.MimeType = "application/vnd.google-apps.folder" Then
If item.Title = "NSQLBACKUP" Then
item.Id = FolderID
Exit For
End If
End If
Next
Dim myfile As New File()
myfile.Title = "LOLBACKUP"
myfile.OriginalFilename = System.IO.Path.GetFileName(filepath)
myfile.Description = "BackUP file"
myfile.FileExtension = ".bak"
myfile.MimeType = "application/octet-stream"
myfile.Id = FolderID
Dim ByteArray As Byte() = System.IO.File.ReadAllBytes(filepath)
Dim stream As New System.IO.MemoryStream(ByteArray)
Dim uploadrequest As Google.Apis.Drive.v2.FilesResource.InsertMediaUpload = service2.Files.Insert(myfile, stream, myfile.MimeType)
uploadrequest.Upload()
Dim file As Google.Apis.Drive.v2.Data.File = uploadrequest.ResponseBody
MsgBox(uploadrequest.ResponseBody) '------- I get nothing here. Like blank msgbox Pops Up.
End Sub
通过Google Auth V3
Public Function UploadFile3(service As Google.Apis.Drive.v3.DriveService, FilePath As String) As Google.Apis.Drive.v3.Data.File
If service3.ApplicationName <> "myprojectGDriveApi" Then CreateService()
If (System.IO.File.Exists(FilePath)) Then
Dim body As New Google.Apis.Drive.v3.Data.File()
body.Name = System.IO.Path.GetFileName(FilePath)
body.Description = "BackUP file"
body.FileExtension = ".bak"
body.MimeType = "application/octet-stream"
Dim findrequest As Google.Apis.Drive.v3.FilesResource.ListRequest = service3.Files.List
Dim list1 As Google.Apis.Drive.v3.Data.FileList = findrequest.Execute
Dim s As String = ""
For Each item As Google.Apis.Drive.v3.Data.File In list1.Files
If item.MimeType = "application/vnd.google-apps.folder" Then
If item.Name = "NSQLBACKUP" Then
body.Id = item.Id.ToString
Exit For
End If
End If
Next
'Dim plist As List(Of String) = New List(Of String)
'plist.Add(s) 'Set parent folder
'body.Parents = plist
'files content
Dim byteArray As Byte() = System.IO.File.ReadAllBytes(FilePath)
Dim stream As New System.IO.MemoryStream(byteArray)
Try
Dim request As Google.Apis.Drive.v3.FilesResource.CreateMediaUpload = service.Files.Create(body, stream, "application/octet-stream")
request.Upload()
MsgBox(request.ResponseBody) '------- Get nothing here also.
Return request.ResponseBody
Catch e As Exception
MsgBox("An error occurred: " + e.Message)
Return Nothing
End Try
Else
MsgBox("File does not exist: " + FilePath)
Return Nothing
End If
End Function
数据被上传。
我尝试上传一个大的.bak文件,任务管理器显示“发送速度”增加了20 Mbps。但是响应主体在auth v2和v3中均未给出任何内容。谷歌驱动器不包含任何内容(通过浏览器检查)
答案 0 :(得分:0)
Okay, I solved it.
Just removed body.FileExtension = ".bak"
line from Google Auth v3 and it started working.