Tutorial: Using Google Cloud Storage from C# and .NET

There is almost no samples or documentation available anywhere for this, so I thought I would write this up myself as using the storage from GCS (similar to Amazon S3 and Azure Blob Storage) is one of the most basic things which people do with the cloud.

The very first thing to do after creating the C# project in Visual Studio is to install the Nuget package mentioned here:

https://www.nuget.org/packages/Google.Apis.Storage.v1/

There is a lot of confusion with multiple packages available, but this is what you want for GCS.

Install-Package Google.Apis.Storage.v1

You are going to need to reference the following namespaces:

using Google.Apis.Auth.OAuth2;
using Google.Apis.Storage.v1;

Setup the basic items you need for making the calls:

private static ClientSecrets _clientSecrets = new ClientSecrets();
        private static UserCredential _userCredential = null;
        private static StorageService _storageService = new StorageService();

        private const string ClientId = "<client id>";
        private const string ClientSecret = "<client secret>";
        private static string[] Scopes = new[] { @"https://www.googleapis.com/auth/devstorage.full_control" };

You need to go to the google console to get the client id and secret. Go to APIs and auth, Credentials and create a new client ID. Mine is a general windows application (not a service) so I selected that option. It will ask you to configure stuff in between, go ahead and do that as well.

Get the token from the API using the below code in your method:

_clientSecrets.ClientId = ClientId;
            _clientSecrets.ClientSecret = ClientSecret;

            CancellationTokenSource cts = new CancellationTokenSource();
            _userCredential = await GoogleWebAuthorizationBroker.AuthorizeAsync(_clientSecrets, Scopes, "<your email account>", cts.Token);

            //You need to do this to handle token expiry.
            await RefreshTokenIfExpired(cts);

To refresh the token use the code below. The token expires in 3600 seconds

private static async Task RefreshTokenIfExpired(CancellationTokenSource cts)
        {
            if (_userCredential.Token.IsExpired(Google.Apis.Util.SystemClock.Default))
            {
                await _userCredential.RefreshTokenAsync(cts.Token);
            }
        }

Use the below code to get the list of buckets in your project:

var bucketsQuery = _storageService.Buckets.List("<project name>");
            bucketsQuery.OauthToken = _userCredential.Token.AccessToken;

            var buckets = bucketsQuery.Execute();
            string bucketToUpload = buckets.Items[0].Name;

The project name above is the weird string which Google generates for you when you create your project. It is is not the friendly name you gave the project.

To upload the file:

Google.Apis.Storage.v1.Data.Object blob = null;
            ObjectsResource.InsertMediaUpload uploadRequest = null;

blob = new Google.Apis.Storage.v1.Data.Object();
                blob.Bucket = bucketToUpload;
                blob.Name = "keyName";

                using (var fileStream = new FileStream(file.FullName, FileMode.Open))
                {
                    uploadRequest = new ObjectsResource.InsertMediaUpload(_storageService, blob, bucketToUpload, fileStream, "text/plain");
                    uploadRequest.OauthToken = _userCredential.Token.AccessToken;
                    await uploadRequest.UploadAsync();
                }

Remember to refresh the tokens if you do lot of operations and before you go to the next batch.

To download the file:

ObjectsResource.GetRequest downloadRequest = null;

downloadRequest = new ObjectsResource.GetRequest(_storageService, bucketToUpload, "keyName");
                MemoryStream stream = new MemoryStream();
                downloadRequest.Download(stream);

I am just downloading to a memory stream. You can download to a file stream as well.

To delete the file:

ObjectsResource.DeleteRequest deleteRequest = null;

deleteRequest = new ObjectsResource.DeleteRequest(_storageService, bucketToUpload, "keyName");
                    deleteRequest.OauthToken = _userCredential.Token.AccessToken;
                    deleteRequest.Execute();

References:

http://stackoverflow.com/questions/18125477/get-file-from-google-cloud-storage-bucket-using-restsharp-or-c-sharp

https://developers.google.com/api-client-library/dotnet/get_started

https://www.nuget.org/packages/Google.Apis.Storage.v1/

http://stackoverflow.com/questions/23086905/bucket-insert-fails-with-error-code-400

http://stackoverflow.com/questions/26196515/uploading-objects-to-google-cloud-storage-buckets-in-c-sharp/26661603#26661603

http://stackoverflow.com/questions/13231158/uploading-csv-files-to-google-cloud-storage-using-net-libraries

https://developers.google.com/api-client-library/dotnet/get_started

https://cloud.google.com/compute/docs/reference/latest/


Comments

  1. bro awesome content helped me a lot
    please make a code spinet on loading data to bigquery table with UserCredential(client id and secret) ..

    ReplyDelete

Post a Comment

Popular posts from this blog

Late 2008 Macbook only giving 1.5 gb/s speed with 6 gb/s Intel SSD?

Enable Visual Studio to use more than 2GB of memory