我正试图使用此链接从Google Drive使用dotnet下载文件。问题是,我在nuget中找不到这个命名空间-使用google . api . authentication;.
我已经下载了nuget中所有名为"Google"的东西,但是运气不好。
你知道它可以藏在哪里吗?由于
要访问Google drive,您需要下载的唯一nuget包是PM> Install-Package Google. apis . drive .v2。它会自动添加你需要的任何东西。
My download from drive method
/// <summary>
/// Download a file
/// Documentation: https://developers.google.com/drive/v2/reference/files/get
/// </summary>
/// <param name="_service">a Valid authenticated DriveService</param>
/// <param name="_fileResource">File resource of the file to download</param>
/// <param name="_saveTo">location of where to save the file including the file name to save it as.</param>
/// <returns></returns>
public static Boolean downloadFile(DriveService _service, File _fileResource, string _saveTo)
{
if (!String.IsNullOrEmpty(_fileResource.DownloadUrl))
{
try
{
var x = _service.HttpClient.GetByteArrayAsync(_fileResource.DownloadUrl );
byte[] arrBytes = x.Result;
System.IO.File.WriteAllBytes(_saveTo, arrBytes);
return true;
}
catch (Exception e)
{
Console.WriteLine("An error occurred: " + e.Message);
return false;
}
}
else
{
// The file doesn't have any content stored on Drive.
return false;
}
}
摘自Google drive样例项目的代码
我认为有一个更好的样本给你(在官方样本回购,https://github.com/google/google-api-dotnet-client-samples/blob/master/Drive.Sample/Program.cs#L154)。
...
await DownloadFile(service, uploadedFile.DownloadUrl);
...
/// <summary>Downloads the media from the given URL.</summary>
private async Task DownloadFile(DriveService service, string url)
{
var downloader = new MediaDownloader(service);
var fileName = <PATH_TO_YOUR_FILE>
using (var fileStream = new System.IO.FileStream(fileName,
System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
var progress = await downloader.DownloadAsync(url, fileStream);
if (progress.Status == DownloadStatus.Completed)
{
Console.WriteLine(fileName + " was downloaded successfully");
}
else
{
Console.WriteLine("Download {0} was interpreted in the middle. Only {1} were downloaded. ",
fileName, progress.BytesDownloaded);
}
}
}
有关媒体下载的更多文档可在此处获得:https://developers.google.com/api-client-library/dotnet/guide/media_download