Let's implement a console application for uploading file to 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 uploading a file from local to Google cloud storage.

Code under Program.cs

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

internal class Program
    long CurrentFileSizeInByte = 0;

    // 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().UploadFile(@"C:\Temp\sample.pdf",
        catch (AggregateException ex)
            foreach (var e in ex.InnerExceptions)
                Console.WriteLine("ERROR: " + e.Message);
        Console.WriteLine("Press any key to exit...");

    /// <summary>
    /// Description: Upload file to GCS, as per passed arguments.
    /// </summary>
    /// <param name="localFileFullPathToUpload">
/// Full file path in local machine</param>
    /// <param name="uploadedFilePathGcs">File name in GCS,
    /// if we want sub-directories then separate it with "/" </param>
    /// <returns></returns>
    private async Task UploadFile(string localFileFullPathToUpload,
string uploadedFilePathGcs)
        // File should exist first!
        if (!string.IsNullOrWhiteSpace(localFileFullPathToUpload) &&
            // 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

            // If different file, then get the correct MIME type here!
            var fileMimeType = "application/pdf";
            var fileStream = new FileStream(localFileFullPathToUpload,
FileMode.Open, FileAccess.Read);

            var fileObj = new GCSFileObject
                Name = uploadedFilePathGcs,
                Updated = DateTime.Now,
                Size = (ulong)fileStream.Length
            CurrentFileSizeInByte = fileStream.Length;

            // go for upload now!
            var mediaUpload
= new ObjectsResource.InsertMediaUpload(service,
fileObj, GCSBucketName, fileStream,
            mediaUpload.ProgressChanged += UploadProgress;
// Note: Google cloud storage API support
// chunk size multiple of 256KB. I kept minimum.
            mediaUpload.ChunkSize = (256 * 1024);
            await mediaUpload.UploadAsync(CancellationToken.None);

            Console.WriteLine("Provided file name does not exist in local machine.");

    private void UploadProgress(IUploadProgress progress)
        switch (progress.Status)
            case UploadStatus.Starting:
                Console.WriteLine("Upload starting, file size: " +
                                  CurrentFileSizeInByte + " Bytes!");
            case UploadStatus.Completed:
                Console.WriteLine("Upload completed!");
            case UploadStatus.Uploading:
                Console.WriteLine("Uploaded "
+ progress.BytesSent + " Bytes.");
            case UploadStatus.Failed:
                Console.WriteLine("Upload 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 6 Comments
6 X 9 =
** 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 - download file with progress bar
All under this category...