使用VB.net下载文件

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

[我正在尝试编写一个简单的示例,以允许我使用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语句,并且可以使用我的凭据在另一个CodeS区域中登录

vb.net google-drive-api google-api-dotnet-client
1个回答
0
投票

答案:

您正在使用的代码不是用于从Google云端硬盘下载文件;它用于从Google Cloud Storage获取文件。

更多信息:

查看了您的问题后,我意识到您正在尝试翻译从Google云端存储而不是从Google云端硬盘下载数据的代码。这些不是同一件事,一个不能用于另一个。在这里,我将为您的代码提供修复程序,以及如何使用VB.NET从Google云端硬盘下载文件。

还要注意的是,取决于您是要下载本地Google云端硬盘文件(Docs / Sheets / Slides / etc)还是仅下载其中存储的文件,因为不能直接下载本地Google云端硬盘文件,因此必须导出为下载兼容格式,例如.docx.csv

[Google Cloud Storage的代码修复:

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

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

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

[从Google云端硬盘下载文件:

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

Google提供了有关如何set up a project for the .NET framework的快速入门,但它们的示例专门针对C#。有关.NET驱动器库的完整库文件,请参见.NET。但是,感兴趣的主要页面是:

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

代码段:

为了让您开始使用,这是一个包括导入和创建Drive Service的代码段:

GetRequest

快速浏览一下,以了解如何设置项目并获取凭据。

希望对您有帮助!

参考:

  • ExportRequest Class Reference
    • ExportRequest
    • 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
  • Key Terms | Cloud Storage | Google Cloud
  • Objects
© www.soinside.com 2019 - 2024. All rights reserved.