【问题标题】:Downloading Files using VB.net使用 VB.net 下载文件
【发布时间】:2020-04-01 13:45:25
【问题描述】:

我正在尝试编写一个简单的示例,以允许我使用 VB.net 从我的 Google 驱动器下载文件

我尝试翻译 C# 代码,但遇到了一些似乎无法解决的错误。

任何帮助将不胜感激

这是我的代码

Private Sub Download()

    Dim storageService = New StorageService(New BaseClientService.Initializer() With {.HttpClientInitializer = credential, .ApplicationName = "APP_NAME_HERE"})
    Dim getRequest = storageService.Objects.[Get]("BUCKET_HERE", "OBJECT_HERE")

    Using fileStream = New System.IO.FileStream("FILE_PATH_HERE", System.IO.FileMode.Create, System.IO.FileAccess.Write)
        getRequest.MediaDownloader.ProgressChanged += Download_ProgressChanged()
        getRequest.Download(fileStream)
    End Using
End Sub
Private Shared Sub Download_ProgressChanged(ByVal progress As IDownloadProgress)
    Console.WriteLine(progress.Status & " " + progress.BytesDownloaded)
End Sub

错误是 StorageService 无法识别,IDownloadProgress 也无法识别。

我拥有所有的 Include 语句,我可以在另一个代码区域使用我的凭据登录S

【问题讨论】:

  • 如果您将插入符号放入,例如IDownloadProgress,然后按ALT+ENTER,弹出窗口会提示有用的信息吗?
  • 要让它识别 IDownloadProgress,添加一个导入语句:Imports Google.Apis.Download
  • 谢谢。这消除了 Idownload 问题,但给我留下了 2 个问题。 StorageService 无法识别,也没有任何有用的建议,并且 Using Filestream 下的行缺少 Download_ProgressChanged 调用的参数
  • 对于StorageService,您还需要Imports Google.Apis.Storage.v1。还;如果将getRequest.MediaDownloader.ProgressChanged 传递回Download_ProgressChanged() 函数会发生什么?那么getRequest.MediaDownloader.ProgressChanged += Download_ProgressChanged(getRequest.MediaDownloader.ProgressChanged)?
  • 谢谢。这解决了存储服务问题。但是您知道“BUCKET_HERE”和“OBJECT_HERE”中的内容吗?我已经设法通过暂时忽略 Progress 更改位来使代码正常工作(我认为这应该是一个处理程序),但我猜测存储桶和对象应该是什么,并且我的文件正在以零字节下载。

标签: vb.net google-drive-api google-api-dotnet-client


【解决方案1】:

答案:

您使用的代码不是用于从 Google Drive 下载文件;用于从 Google Cloud Storage 获取文件。

更多信息:

查看您的问题后,我意识到您正在尝试翻译从 Google Cloud Storage 下载数据的代码,而不是从 Google Drive 下载数据。这些不是一回事,一个不能用于另一个。在这里,我将为您的代码提供修复,以及如何使用 VB.NET 对 Google Drive 进行身份验证。

还需要注意的是,取决于您是要下载本地 Google Drive 文件(Docs/Sheets/Slides/etc)还是仅下载存储在那里的文件,因为无法直接下载本地 Google Drive 文件,并且必须导出为与下载兼容的格式,例如 .docx.csv

谷歌云存储的代码修复:

作为上面 cmets 中已经提到的内容的汇编,您的代码中缺少两个方法导入:

Imports Google.Apis.Storage.v1
Imports Google.Apis.Download

你也问过; BUCKET_HEREOBJECT_HERE 是来自 Google Cloud Storage 的 bucketsobjects - 它们分别是数据容器和数据片段本身。

从 Google Drive 下载文件:

要从 Google Drive 而不是从 Google Cloud Storage 下载,您需要使用Google Drive API

Google 提供了关于如何使用set up a project for the .NET framework 的快速入门,但他们的示例专门针对 C#。可以在here 找到 .NET Drive Library 的综合库文档。然而,感兴趣的主要页面是:

  1. Google.Apis.Drive.v3.FilesResource Class Reference
  2. GetRequest Class Reference
  3. ExportRequest Class Reference

代码sn-p:

这里有一个代码 sn-p,包括导入和创建驱动服务:

Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.Drive.v3
Imports Google.Apis.Drive.v3.Data
Imports Google.Apis.Services
Imports Google.Apis.Util.Store
Imports System.IO
Imports System.Threading

Module Module1
    Dim Scopes() As String = {DriveService.Scope.Drive}
    Dim ApplicationName As String = "Your-Application-Name"
    Private Service As DriveService = New DriveService

    Public Sub Main()
        Dim creds As UserCredential
        'Store your credentials file in the project directory as 'credentials.json'
        'Don't forget to include it in your project
        Using Stream = New FileStream("credentials.json", FileMode.Open, FileAccess.Read)
            'Creates a token file for this auth, make sure to delete it and re-auth 
            'if you change scopes
            Dim credentialFile As String = "token.json"
            creds = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(Stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    New FileDataStore(credentialFile, True)).Result
            Console.WriteLine("Credentials saved to: " + credentialFile)

        End Using
        'Create Drive API service.
        Dim Service = New DriveService(New BaseClientService.Initializer() With
            {
                .HttpClientInitializer = creds,
                .ApplicationName = ApplicationName
            })
        'Define parameters of request here, depending on whether you need to use
        'the get or export methods
        Dim fileId As String = "your-file-id"

        'File processing goes here!
    End Sub
End Module

查看快速入门以了解如何设置项目并获取您的凭据。

参考资料:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多