In this tutorial, I will show you how to start using Google Cloud Platform with Visual Studio 2019 and C#.
The main goal is to create a Cloud Storage buckets.
First, create a console project into your Visual Studio 2019 I.D.E.
Open the cloud google.com dotnet webpage and use Try It Free option.
This allows you to use a free account with 300$ free option:
Make all settings for your account, add your account credit card, and a new project will create and named My First Project.
Into your Visual Studio 2019 open into main menu Tools – NuGet Package Manager – Package manager Console and use this command to install Google client library to access the Google Cloud Storage API:
1 | Install-Package Google.Cloud.Storage.V1 -Version 3.1.0 |
Use the Set up a project button from the same webpage to set up your project.
This will show you a window with some information for your project and download the JSON file:
The JSON file can be found on this URL: https://console.cloud.google.com/apis/credentials?project=YOUR-PROJECT-ID.
If you want to create another JSON or set an authentication credential, see this webpage.
You need to have a billing account in order to use this example, see this webpage.
If not you will get this error:
1 2 3 | The project to be billed is associated with an absent billing account. [403] Errors [ Message[The project to be billed is associated with an absent billing account.] Location[Authorization - header] Reason[accountDisabled] Domain[global] |
This information should be used in the following source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | using System; using System.Diagnostics; using Google.Cloud.Storage.V1; namespace Google_Cloud_001 { class StorageQuickstart { static void Main(string[] args) { // Your Google Cloud Platform project ID. string projectId = "YOUR-PROJECT-ID"; //Authenticate to the service by using Service Account System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS","D:\\your_project.json"); // Instantiates a client. using (StorageClient storageClient = StorageClient.Create()) { // The name for the new bucket. string bucketName = projectId + "-test-bucket"; try { // Creates the new bucket. storageClient.CreateBucket(projectId, bucketName); Console.WriteLine($"Bucket {bucketName} created."); } catch (Google.GoogleApiException e) when (e.Error.Code == 409) { // The bucket already exists. That's fine. Console.WriteLine(e.Error.Message); } } } } } |
Build the project and run it, will work well: