使用 VB.net 下载文件
Downloading Files using VB.net
我正在尝试编写一个简单的示例,以允许我使用 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 的另一个区域使用我的凭据登录
答案:
您使用的代码不是用于从 Google 驱动器下载文件;它用于从 Google 云存储中获取文件。
更多信息:
查看您的问题后,我意识到您正在尝试翻译从 Google Cloud Storage 而不是 Google Drive 下载数据的代码。这些不是一回事,一个不能用于另一个。在这里,我将为您的代码提供修复,以及如何使用 VB.NET 通过 Google 驱动器进行身份验证。
还需要注意的是,这取决于您是要下载本机 Google 驱动器文件 (Docs/Sheets/Slides/etc) 还是只下载存储在那里的文件,作为本机 Google 驱动器文件不能直接下载,必须导出为兼容下载的格式,例如 .docx
或 .csv
.
Google 云存储的代码修复:
作为上面评论中已经提到的内容的汇编,您的代码中缺少两个方法导入:
Imports Google.Apis.Storage.v1
Imports Google.Apis.Download
正如你所问; BUCKET_HERE
和 OBJECT_HERE
来自 Google 云存储的 buckets and objects - 它们分别是数据容器和数据片段本身。
正在从 Google 驱动器下载文件:
要从 Google 驱动器而不是从 Google 云存储下载,您需要使用 Google Drive API.
Google 提供了如何 set up a project for the .NET
framework, but their example is specifically for C#. There is comprehensive library documentation for the .NET Drive Library which can be found here 的快速入门。然而,感兴趣的主要页面是:
- Google.Apis.Drive.v3.FilesResource Class Reference
GetRequest
Class Reference
ExportRequest
Class Reference
代码片段:
为了帮助您开始,这里有一个代码片段,包括导入和创建云端硬盘服务:
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
查看快速入门以了解如何设置项目和获取凭据。
参考文献:
我正在尝试编写一个简单的示例,以允许我使用 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 的另一个区域使用我的凭据登录
答案:
您使用的代码不是用于从 Google 驱动器下载文件;它用于从 Google 云存储中获取文件。
更多信息:
查看您的问题后,我意识到您正在尝试翻译从 Google Cloud Storage 而不是 Google Drive 下载数据的代码。这些不是一回事,一个不能用于另一个。在这里,我将为您的代码提供修复,以及如何使用 VB.NET 通过 Google 驱动器进行身份验证。
还需要注意的是,这取决于您是要下载本机 Google 驱动器文件 (Docs/Sheets/Slides/etc) 还是只下载存储在那里的文件,作为本机 Google 驱动器文件不能直接下载,必须导出为兼容下载的格式,例如 .docx
或 .csv
.
Google 云存储的代码修复:
作为上面评论中已经提到的内容的汇编,您的代码中缺少两个方法导入:
Imports Google.Apis.Storage.v1
Imports Google.Apis.Download
正如你所问; BUCKET_HERE
和 OBJECT_HERE
来自 Google 云存储的 buckets and objects - 它们分别是数据容器和数据片段本身。
正在从 Google 驱动器下载文件:
要从 Google 驱动器而不是从 Google 云存储下载,您需要使用 Google Drive API.
Google 提供了如何 set up a project for the .NET
framework, but their example is specifically for C#. There is comprehensive library documentation for the .NET Drive Library which can be found here 的快速入门。然而,感兴趣的主要页面是:
- Google.Apis.Drive.v3.FilesResource Class Reference
GetRequest
Class ReferenceExportRequest
Class Reference
代码片段:
为了帮助您开始,这里有一个代码片段,包括导入和创建云端硬盘服务:
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
查看快速入门以了解如何设置项目和获取凭据。