1

Note: Although Google has a working system for batch requests, it does not appear to be compatible with requests involving media. So far, I've only been able to get it to work with File objects, which do not consist of media content.

Below is my current code used to download media from a Drive folder. Note that all of the media consist of simple tiny rtf documents, and so you should have an idea of the file sizes. My issue is that even though the files download as expected, it is a SLOW process! I have a folder consisting of more than 300 files on average. There has to be a way to speed up this process.

private async void Button_Click()
{
    foreach (var driveFile in driveFileList)
    {
        await GetFileContentAsync(driveFile.Id, CancellationToken.None);
    }
}

public async Task<AccessResult> GetFileContentAsync (string driveId, CancellationToken cancellationToken) 
{
    cancellationToken.ThrowIfCancellationRequested ();
    GetRequest getRequest = new GetRequest (DriveService, driveId);
    getRequest.Fields = "*";
    string incomingJsonContent;

    try 
    {
        using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream ())
        using (System.IO.StreamReader streamReader = new System.IO.StreamReader (memoryStream)) 
        {
            IDownloadProgress downloadProgress = await getRequest.DownloadAsync (memoryStream, cancellationToken);
            memoryStream.Position = 0;
            incomingJsonContent = await streamReader.ReadToEndAsync ();
        }

        return new AccessResult (true, incomingJsonContent);
    } catch {}
}
LeBrown Jones
  • 887
  • 6
  • 17
  • 2
    Downloading Content of list of files is not possible. You need to iterate over them. If you need performance try using TPL or Task.WhenAll method – Sanjay Idpuganti Nov 05 '18 at 09:54
  • Thanks for reply, and sorry about the confusion. I updated the example to clarify things. I created a test button that pretty much abstracts a lot of things, but you should get what I'm doing now. For every File object (there are 300 File objects) I have to asynchronously download them, 1-by-1.... with my current novice code. There has to be SOME WAY to download multiple files in parallel to speed things up. – LeBrown Jones Nov 05 '18 at 10:00
  • asynchronous = parallel – pinoyyid Nov 06 '18 at 12:23
  • 1
    AFAIK, @Sanjay is correct. Also, Google doesn't support batch request operations for media, either for upload or download. According to this related [SO post](https://stackoverflow.com/a/50675690/5995040), there is a certain limit to how fast the application doing the download can run and how much it can access at the same time. – Mr.Rebot Nov 07 '18 at 02:22
  • 1
    Given that limitation, keep in mind there are documents that you can use like [Handling API Errors](https://developers.google.com/drive/api/v3/handle-errors#403_user_rate_limit_exceeded) which provide suggestions like exponential backoff or slowing down of request, using of service account and applying for a higher quota. – Mr.Rebot Nov 07 '18 at 02:22
  • 1
    An alternative approach could be to zip the files you need and download the archive. Maybe something like this could help? https://stackoverflow.com/questions/13259041/creating-a-zip-file-inside-google-drive-with-apps-script – Gruber Nov 27 '18 at 05:10

0 Answers0