Hide
Google Cloud Storage

.NET Example

This page discusses how you can use the Google APIs Client Library for .NET to send requests to the Google Cloud Storage JSON API.

Specifically, the sample shown here gets metadata about a bucket you specify and lists the objects in the bucket. You can use the general steps shown here to run your own code.

This document assumes that you have .NET installed, and you are familiar with Java and the Google Cloud Storage concepts and operations presented in the Getting Started: Using the Developers Console guide.

1. Set up your environment

The code sample requires the google-api-dotnet-client and its dependent libraries. You can add the library and dependencies to an existing project with the NuGet Package Manager by adding the Google.Apis.Google.v1 package.

If you don't have an existing project to run the project, you can:

  • Create a new C# Console Application. For example, you can use the free Visual Studio Community IDE.
  • Run the NuGet package manager and add the Google.Apis.Google.v1 package.

2. Get authentication credentials

In the example code, you can use a service account credential or an installed application credential to authenticate with the Google Cloud Storage JSON API. Service account credentials are used when authenticating on behalf of a service. Installed application credentials are used for applications that are installed on devices such as computers, mobile devices, and tablets

For a service account, you can create and download a service account credential (PKCS12 key file) file from the Google Developers Console.

To create a service account credential:

  1. Go to the Google Developers Console.
  2. Select a project to which the service account will be associated.
  3. In the left sidebar, under APIs & auth, select Credentials.
  4. Click Create new Client ID.
  5. In the Create Client ID window, choose Service account.

    Client ID for Service Account

  6. Click Create Client ID.
  7. A New Public/Private key pair generated window is displayed and the private key (JSON format) is downloaded automatically. Click Okay, got it.

3. Create your code file

In your C# project, create a C# code file called StorageSample.cs. Copy the code from below and make the following changes:

  • Set projectId to that name of the project in which you created the client ID.
  • Set bucketName to the name of a bucket in the project.
  • Set serviceAccountEmail to the name of the service account email.
  • Set certificateFile to the path and name of the key file.
  • If you are using an installed application credential, uncomment the code in the example to use it and comment out the code that uses the service credential.

StorageSample.cs

/*
 * Copyright (c) 2014 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

/**
 * Sample code to issue several basic Google Cloud Store (GCS) operations
 * using the Google Client Libraries.
 * For more information, see documentation for Compute Storage .NET client
 * https://developers.google.com/api-client-library/dotnet/apis/storage/v1
 *
 * Usage:
 * Add projects references using NuGet to
 * "Google.Apis.Storage.v1"   (other dependencies will get added automatically)
 * http://www.nuget.org/packages/Google.Apis.Storage.v1/
 * Generate and download service account .p12 from place it to c:\
 * specify the bucketName, projectId, serviceAccountEmail and certificateFile below.
 * authorize serviceAccountEmail for your target bucket.
 */

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using Google.Apis;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Download;
using Google.Apis.Services;
using Google.Apis.Storage.v1;
using Google.Apis.Storage.v1.Data;
using Google.Apis.Util.Store;

namespace GCSSearch
{
    internal class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                new Program().Run().Wait();
            }
            catch (AggregateException ex)
            {
                foreach (var err in ex.InnerExceptions)
                {
                    Console.WriteLine("ERROR: " + err.Message);
                }
            }
            Console.ReadKey();
        }

        #region Consts

        private const int KB = 0x400;
        private const int DownloadChunkSize = 256 * KB;

        #endregion

        private async Task Run()
        {
            string projectId = "YOUR_PROJECT_ID";
            string bucketName = "YOUR_BUCKET_NAME";

            //credentials for certificate-based service accounts
            string certificateFile = "c:\\YOUR_CERTIFICATE.p12";
            string serviceAccountEmail = "[email protected]";
            var certificate = new X509Certificate2(certificateFile, "notasecret", X509KeyStorageFlags.Exportable);
            ServiceAccountCredential credential = new ServiceAccountCredential(
               new ServiceAccountCredential.Initializer(serviceAccountEmail)
               {
                   Scopes = new[] { StorageService.Scope.DevstorageReadWrite }
               }.FromCertificate(certificate));

            //credentials when running in google compute engine
            //ComputeCredential credential = new ComputeCredential(new ComputeCredential.Initializer());

            //credentials for user interactive webflow
            /*
            UserCredential credential;
            string clientId = "YOUR_CLIENT_ID.apps.googleusercontent.com";
            string clientSecret = "YOUR_CLIENT_SECRET";
            credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets{ClientId= clientId,ClientSecret=clientSecret},
                new[] { StorageService.Scope.DevstorageFullControl }, Environment.UserName, CancellationToken.None);
            //Console.WriteLine("Credential file saved at: " + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
             */

            var service = new StorageService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "GCS Sample",
            });

            Console.WriteLine("List of buckets in current project");
            Buckets buckets = await service.Buckets.List(projectId).ExecuteAsync();

            foreach (var bkt in buckets.Items)
            {
                Console.WriteLine(bkt.Name);
            }

            Console.WriteLine("Total number of items in bucket: " + buckets.Items.Count);
            Console.WriteLine("=============================");

            // using  Google.Apis.Storage.v1.Data.Object to disambiguate from System.Object
            Google.Apis.Storage.v1.Data.Object fileobj = new Google.Apis.Storage.v1.Data.Object() { Name = "somefile.txt" };

            Console.WriteLine("Creating " + fileobj.Name + " in bucket " + bucketName);
            byte[] msgtxt = Encoding.UTF8.GetBytes("Lorem Ipsum");

            using (var streamOut = new MemoryStream(msgtxt))
            {
                await service.Objects.Insert(fileobj, bucketName, streamOut, "text/plain").UploadAsync();
            }

            Console.WriteLine("Object created: " + fileobj.Name);

            Console.WriteLine("=============================");

            Console.WriteLine("Reading object " + fileobj.Name + " in bucket: " + bucketName);
            var req = service.Objects.Get(bucketName, fileobj.Name);
            Google.Apis.Storage.v1.Data.Object readobj = await req.ExecuteAsync();

            Console.WriteLine("Object MediaLink: " + readobj.MediaLink);

            // download using Google.Apis.Download and display the progress
            string pathUser = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
            var fileName = Path.Combine(pathUser, "Downloads") + "\\" + readobj.Name;
            Console.WriteLine("Starting download to " + fileName);
            var downloader = new MediaDownloader(service)
            {
                ChunkSize = DownloadChunkSize
            };
            // add a delegate for the progress changed event for writing to console on changes
            downloader.ProgressChanged += Download_ProgressChanged;

            using (var fileStream = new System.IO.FileStream(fileName,
                System.IO.FileMode.Create, System.IO.FileAccess.Write))
            {
                var progress = await downloader.DownloadAsync(readobj.MediaLink, fileStream);
                if (progress.Status == DownloadStatus.Completed)
                {
                    Console.WriteLine(readobj.Name + " was downloaded successfully");
                }
                else
                {
                    Console.WriteLine("Download {0} was interpreted in the middle. Only {1} were downloaded. ",
                        readobj.Name, progress.BytesDownloaded);
                }
            }

            /*
            // or download as a stream
            Stream getStream = await service.HttpClient.GetStreamAsync(readobj.MediaLink);
            Console.WriteLine("Object Content: ");
            using (var reader = new StreamReader(getStream))
            {
                Console.WriteLine(reader.ReadToEnd());
            }
            */
            Console.WriteLine("=============================");
        }

        #region Progress and Response changes

        static void Download_ProgressChanged(IDownloadProgress progress)
        {
            Console.WriteLine(progress.Status + " " + progress.BytesDownloaded + " bytes");
        }

        #endregion
    }
}

4. Run the sample

Run (F5) the project containing StorageSample.cs. A console opens with the list of buckets in the project and a test of uploading and reading back a file in the bucket. Press return to exit the console.