如何使用VB.net将.txt文件上传到谷歌驱动器?

问题描述 投票:0回答:1

注意:实际上尝试上传.bak文件,因此mimetype是不同的。 我将mimetype更改为.txt的plain / text,但问题如下所示。 我正在尝试从我的vb.net应用程序上传.bak文件。 我正在尝试将其上传到特定文件夹中。

我尝试了Google auth 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

Viei Google Outh

 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中都没有提供任何内容。谷歌驱动器不包含任何内容(通过浏览器检查)

vb.net google-drive-sdk
1个回答
0
投票

好的,我解决了。刚从Google Auth v3中删除了body.FileExtension = ".bak"系列,它就开始工作了。

© www.soinside.com 2019 - 2024. All rights reserved.