I need to create a method for resumable downloads of large file from GoogleDrive under a service account. I'm creating a standalone application in .Net 4.0 using Google.Apis.Drive.v2, Google.Apis.Auth and Google.Apis.Oauth2.v2.
My current code is:
DriveService service = new DriveService(new BaseClientService.Initializer() ...
var pathString = System.IO.Path.Combine(localFilePath, fileName);
using (var fileStream = System.IO.File.Create(pathString))
{
Task<System.IO.Stream> streamTask = service.HttpClient.GetStreamAsync(downloadURL);
System.IO.Stream stream = await streamTask;
await stream.CopyToAsync(fileStream);
}
However GetStreamAsync doesn't seem to support specifying a range parameter.
Some suggestions recommend using HttpWebRequest:
//Partial downloads: https://developers.google.com/drive/web/manage-downloads
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
new Uri(file.DownloadUrl));
request.AddRange(1000, 2000);
authenticator.ApplyAuthenticationToRequest(request);
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
The problem with this is I don't know how you get IAuthenticator authenticator from either DriveService or ServiceAccountCredential. It also appears using HttpWebRequest is slower.