Reputation: 154
I am working to connect to the google drive, list every files with any formats and then download it physically on my hard. I used the methods which google API prepared for me. But I think the way I worked I just have access to the content of each file and so I can not do that.
Could you please help me to know that which configuration I need to do that?
public static Google.Apis.Drive.v2.Data.FileList ListFiles(DriveService service, FilesListOptionalParms optional = null)
{
try
{
if (service == null)
throw new ArgumentNullException("service");
var request = service.Files.List();
request = (FilesResource.ListRequest)SampleHelpers.ApplyOptionalParms(request, optional);
return request.Execute();
}
catch (Exception ex)
{
throw new Exception("Request Files.List failed.", ex);
}
}
I call this method and for each file I want to download it on my PC:
FileList files_list = DriveListExample.ListFiles(service, null);
if (files_list.Items.Count != 0)
{
foreach (var file in files_list.Items)
{
DriveListExample.DownloadFile(service, file, DownloadDirectoryName);
Console.WriteLine("\"{0}\" ", file.Title + " downloaded completely!");
}
}
here if I can access to the downloadUrl of each file I can test another way for downloading. But it is null and this is my download method:
public static void DownloadFile(DriveService service, File file, string saveTo) {
var request = service.Files.Get(file.Id);
var stream = new System.IO.MemoryStream();
Console.WriteLine(file.FileExtension);
request.MediaDownloader.ProgressChanged += (IDownloadProgress progress) =>
{
switch (progress.Status)
{
case DownloadStatus.Downloading:
{
Console.WriteLine(progress.BytesDownloaded);
break;
}
case DownloadStatus.Completed:
{
Console.WriteLine("Download complete.");
SaveStream(stream, saveTo);
break;
}
case DownloadStatus.Failed:
{
Console.WriteLine("Download failed.");
break;
}
}
};
try
{
request.Download(stream);
}
catch (Exception ex)
{
Console.Write("Error: " + ex.Message);
}
}
the status of download is Failed in this process. I also check the scopes and I found that with these lines just allow me to download the files I created in this application:
private static readonly string[] Scopes = new[] { DriveService.Scope.DriveFile, DriveService.Scope.Drive };
But I need after listing all of files, I can download each of them.
Could you please help me with the solution or other working codes?
Upvotes: 2
Views: 744
Reputation: 427
You need to use a valid file name and handle google doc files:
public static void DownloadFile(DriveService service, Google.Apis.Drive.v2.Data.File file, string saveTo)
{
var request = service.Files.Get(file.Id);
var stream = new System.IO.MemoryStream();
// Add a handler which will be notified on progress changes.
// It will notify on each chunk download and when the
// download is completed or failed.
request.MediaDownloader.ProgressChanged += (IDownloadProgress progress) =>
{
switch (progress.Status)
{
case DownloadStatus.Downloading:
{
Console.WriteLine(progress.BytesDownloaded);
break;
}
case DownloadStatus.Completed:
{
Console.WriteLine("Download complete.");
SaveStream(stream, saveTo + @"/" + file.Title);
break;
}
case DownloadStatus.Failed:
{
if (file.ExportLinks.Any())
SaveStream(new HttpClient().GetStreamAsync(file.ExportLinks.FirstOrDefault().Value).Result, saveTo + @"/" + file.Title);
Console.WriteLine("Download failed.");
break;
}
}
};
try
{
request.Download(stream);
}
catch (Exception ex)
{
Console.Write("Error: " + ex.Message);
}
}
Upvotes: 1