Google drive downloading large files with C#

Please Share

Google Drive APIAre you working on an application where you would like to download large files from Google drive? This simple tutorial will show you how to download large files by chunks.


The first thing you will need to do is create a new project on Google developer console. For this project i will be using a console application so we will create a native client. Note this code will NOT work for a web browser application.

You will also need the NuGet package Install-Package Google.Apis.Drive.v3 you will also need to grab my sample class for authencating to Google drive Oauth2Authentication.cs


class Program
        private const string _pathToCreds = @"C:\creds\client_secret.json";

        async static Task Main(string[] args)
            var service = auth.Oauth2Example.GetDriveService(_pathToCreds, "test",
                new[] {Google.Apis.Drive.v3.DriveService.Scope.Drive});

            var request = service.Files.List();
            request.Q = "name=''";
            var result = await request.ExecuteAsync();

            foreach (var file in result.Files)
                await DownloadFile(file.Id, service);

            Console.WriteLine("Hello World!");

        public static async Task DownloadFile(string fileId, DriveService service)
            const int KB = 0x400;
            var chunkSize = 256 * KB; // 256KB;

            var fileRequest = service.Files.Get(fileId);
            fileRequest.Fields = "*";
            var fileResponse = fileRequest.Execute();

            var exportRequest = service.Files.Export(fileResponse.Id, fileResponse.MimeType);
            var client = exportRequest.Service.HttpClient;

            //you would need to know the file size
            var size = fileResponse.Size;

            await using var file = new FileStream(fileResponse.Name, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            file.SetLength((long) size);

            var chunks = (size / chunkSize) + 1;
            for (long index = 0; index < chunks; index++)
                var request = exportRequest.CreateRequest();

                var from = index * chunkSize;
                var to = @from + chunkSize - 1;

                request.Headers.Range = new RangeHeaderValue(@from, to);

                var response = await client.SendAsync(request);

                if (response.StatusCode != HttpStatusCode.PartialContent && !response.IsSuccessStatusCode) continue;

                await using var stream = await response.Content.ReadAsStreamAsync();
                file.Seek(@from, SeekOrigin.Begin);
                await stream.CopyToAsync(file);


Simple download gives us a method for downloading large files from google drive.

Please Share

Linda Lawton

About Linda Lawton

My name is Linda Lawton I have more than 20 years experience working as an application developer and a database expert. I have also been working with Google APIs since 2012 and I have been contributing to the Google .Net client library since 2013. In 2013 I became a a Google Developer Experts for Google Analytics.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.