Let's implement a console application for downloading file from Google cloud storage with help of Google.Apis.Storage.v1.

My dev machine: .Net 4.5, Visual Studio 2012

Prerequisites: Install Google.Apis.Storage.v1 from NuGet packages.

File details:
Program.cs: Entry point of the application, contains Main function. This file will also contain different other functions required for downloading a file from Google cloud storage.

Code under Program.cs

using System;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using Google.Apis.Download;
using Google.Apis.Storage.v1;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using GCSFileObject = Google.Apis.Storage.v1.Data.Object;
using System.IO;

internal class Program
    public long CurrentFileSizeInByte = 0;
    public string LocalDownloadPath = @"C:\Temp";

    // Below details can be retrieved from Google console site.
    const string GCSBucketName = "MySampleBucket";
    const string P12KeyFullPath = @"C:\Temp\key.p12";
    const string P12KeySecret = "gcssecret";
    const string GCSServiceAccountEmail
                  = "GetItFromGoogleCloudStorageConsole@developer.gserviceaccount.com";
    const string GCSApplicationName = "MySampleProject";

    private static void Main(string[] args)
            new Program().DownloadFile("Test.pdf").Wait();
        catch (AggregateException ex)
            foreach (var e in ex.InnerExceptions)
                Console.WriteLine("ERROR: " + e.Message);
        Console.WriteLine("Press any key to exit...");

    private async Task DownloadFile(string filePathInGcs)
        // Loading the Key file
        var certificate
                 = new X509Certificate2(P12KeyFullPath, P12KeySecret,
                       X509KeyStorageFlags.MachineKeySet |
                       X509KeyStorageFlags.PersistKeySet |

        // Authentication first!
        var credential = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer(GCSServiceAccountEmail)
                Scopes = new[] { StorageService.Scope.DevstorageReadWrite }

        // Well, all setup now!
        // Let's move to initializing service for Google API!
        var service = new StorageService(
            new BaseClientService.Initializer
                    ApplicationName = GCSApplicationName,
                    HttpClientInitializer = credential

        // First get file meta data from GCS, which
        // will contain media link of GCS
        var fileRequest =
new ObjectsResource.GetRequest(service,
GCSBucketName, filePathInGcs);
        var fileMetaDataObj = fileRequest.Execute();

        // Let's download the file now!
        fileRequest.MediaDownloader.ProgressChanged += DownloadProgress;
        // Note: Google cloud storage API support
        // chunk size multiple of 256KB. I kept minimum.
        fileRequest.MediaDownloader.ChunkSize = (256 * 1024);
        if (fileMetaDataObj.Size.HasValue)
+ ": Downloading file of size "
+ fileMetaDataObj.Size.Value + " Bytes.");

        var downloadStream =
new FileStream(Path.Combine(LocalDownloadPath, filePathInGcs),

        await fileRequest.MediaDownloader.DownloadAsync(fileMetaDataObj.MediaLink,

    private void DownloadProgress(IDownloadProgress progress)
        switch (progress.Status)
            case DownloadStatus.Completed:
                Console.WriteLine("Download completed!");
            case DownloadStatus.Downloading:
                Console.WriteLine("Downloaded " +
                      progress.BytesDownloaded + " Bytes.");
            case DownloadStatus.Failed:
                Console.WriteLine("Download failed "
+ Environment.NewLine
+ progress.Exception.Message
                      + Environment.NewLine
+ progress.Exception.StackTrace
                      + Environment.NewLine
+ progress.Exception.Source
                      + Environment.NewLine
+ progress.Exception.InnerException
                      + Environment.NewLine
+ "HR-Result" + progress.Exception.HResult);

Discussion 4 Comments
25 - Five
** To prevent abusing comments from publishing, posted comments will be reviewed and then published!
 Mritunjay Kumar
Works at Mindfire Solutions

I mostly work with C#, ASP.NET, MVC, WCF, Web API, Entity FrameWork, MS Sql.

More under this category...
Convert data table to generic list
Extension method to convert a delimited string to list of generic primitive type
Convert generic list to data table
Generate random string from set of fixed chars
Google cloud storage API - Rename a file
Initializing X509Certificate2, Windows 2012 + IIS8 + .Net 4.5
Google cloud storage API - upload file with progress bar
All under this category...