Create Google Cloud GCP Storage Bucket using C#
Today in this article, we shall learn how to Create Google Cloud Storage Bucket using C# in the GCP environment programmatically. We shall be using the C# GCP library to perform the same.
Today in this article, we will cover below aspects,
It’s pretty simple to use Google storage libraries and perform numerous operations on the cloud storage like read, create or delete the storage.
Getting Started
Create any .NET or .NET Core application.
Add below Nuget packages ,
Install Nuget package for Google.Apis.Auth
OR
PM> Install-Package Google.Apis.Auth -Version 1.50.0
Install Nuget package for Google.Cloud.Storage.V1
OR
PM> Install-Package Google.Cloud.Storage.V1 -Version 3.4.0
Please add below using statements to your class files,
using Google.Apis.Storage.v1.Data;
using Google.Cloud.Storage.V1;
Below is a sample example for creating New Bucket storage,
var storage = StorageClient.Create();
Bucket bucket = new Bucket
{
Location = "us",
Name = bucketName,
StorageClass = "STANDARD"
};
var newlyCreatedBucket = storage.CreateBucket(projectId, bucket);
What is Storage class
In the above example, the user can define a storage location and use the storage class as required. If the storage class value is not specified when the bucket is created, it will default to the STANDARD storage class.
The storage class type defines how the objects in the bucket are stored. Storage class determines the SLA and the cost of storage.
Users can use any of the below-supported Storage classes available.
Types of Storage classes in GCS
Users can use any of the below types of storage classes based on their SLA and Cost requirements of storing the data.
- NEARLINE – Nearline Storage is a low-cost, highly durable suitable for lower availability storage requirements.
- COLDLINE – Coldline Storage is a very-low-cost, highly durable storage service with slightly lower availability.
- ARCHIVE – Archive Storage is the lowest-cost, highly durable storage service mainly used for use cases like data archiving, online backup, or disaster recovery
- STANDARD – Standard Storage is best for data that is frequently accessed for brief periods of time. It is the default storage class when the storage class value is not specified when the bucket is created,
- MULTI_REGIONAL_LEGACY
- REGIONAL_LEGACY
That’s All once you run the above logic, you shall see your storage bucket is ready to be used.
Please make sure to set the Environment variable GOOGLE_APPLICATION_CREDENTIALS using a secured service account file.
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "secured-key.json");
Do you have any comments or ideas or any better suggestions to share?
Please sound off your comments below.
Happy Coding !!
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.