DEV Community

Cover image for Google Storage + .NET 7
Caio Ragazzi
Caio Ragazzi

Posted on

Google Storage + .NET 7

Hello, fellow dev friends. I hope you all are doing great.

When I am writing this article is a national holiday here in Portugal (Assumption of Mary) 🇵🇹 and how to enjoy most of this beautiful day? Going to the park? beach? eat some delicious pastries? Not at all! Let's write my first (yesss that's it, my first!) post here at dev.to!

Being a Brazilian leaving in Portugal is quite easy, to be honest (mostly because of the language), but writing here for developers all over the world, well, this can be a bit tricky for me! But let's try it out!
Enough of introductions and let's start talking about what is actually saying in the title!

In the last few days, I have started learning some Google Cloud concepts through https://www.cloudskillsboost.google/ (by the way, such an amazing platform to learn GCP). And one of the first concepts they teach us is Google Cloud Storage. And for me, the best way to learn those concepts is by applying them in a practical way.

So First I created a really small .NET 7 Class Library to upload some files in Cloud Storage Bucket.

Let's start by creating a new Class Library project. Here I am using the C# Dev Kit extension for Visual Code to keep things simple (I could also use the dotnet CLI):

Visual Code New Project Creation

Visual Code New Project Type Class Library Creation

What is cool about the C# Dev Kit is that when you create a Class Library in an empty folder it also creates the solution for you:

VSCode Solution Explorer side tab visualization

Here is where things really got me excited, because I thought the process to import a file to the GCP Cloud Storage would be a bit complex, but it is actually not.


⚠️ Before we continue is important to go through the following steps:

We need to authenticate our application in the GCP by using the Application Default Credentials or commonly known as ADC. There are a couple of ways of doing it but for me, the easier is by using the gcloud CLI:

gcloud auth application-default login
Enter fullscreen mode Exit fullscreen mode

If you want to know more about the command above, you can visit the URL:

https://cloud.google.com/sdk/gcloud/reference/auth/application-default/login

But basically, you will need to follow some steps to obtain user access credentials and then the CLI will place it in a location where the ADC will get it.


Now we can start configuring our project. We will need to install the Google Cloud Storage V1 Nuget Package:

https://www.nuget.org/packages/Google.Cloud.Storage.V1

And then with a couple of lines, we can easily upload files to buckets in Google Cloud:

public class FileUploader
{
    public async Task<string> UploadFile(string base64String)
    {
        var client = await StorageClient.CreateAsync();

        var bucket = await client.GetBucketAsync("bucket name");

        var bytes = Convert.FromBase64String(base64String);
        var dataObject = await client.UploadObjectAsync(bucket.Name, "folder/fileName.png", "text/plain", new MemoryStream(bytes));

        return dataObject.Id;
    }
}
Enter fullscreen mode Exit fullscreen mode

Let's go through line by line to see what is happening:

var client = await StorageClient.CreateAsync();

First, we use this static method CreateAsync to get an instance of the class StorageClient.

var bucket = await client.GetBucketAsync("bucket name");

Here we are getting an instance of an existing bucket by its name, but if you don't have a bucket yet, you could create a new one using the method CreateBucketAsync which will also return a bucket instance.

var bytes = Convert.FromBase64String(base64String);

Then we are getting a little help from the method FromBase64String to convert a "probably" base64 photo to an array of bytes.

var dataObject = await client.UploadObjectAsync(bucket.Name, "folder/fileName.png", "text/plain", new MemoryStream(bytes));

And to finally upload the file, we pass three parameters to the method UploadObjectAsync

  • Bucket Name.
  • File Name (But here you could also organize your bucket with folders by passing the folder name in the string like this "folder1/folder2/filename.png").
  • Stream - and that's why we had to convert our base64 string to an array of bytes!

return dataObject.Id;

And finally, here I just returned the Id of the newly created object, which is basically the ID of the object, including the bucket name, object name, and generation number.

And that's it, incredible isn't it? With a small effort, we can create a method that will upload some files to a GCP Bucket.

Thanks a lot, guys! I will try to post more content here so please tell me what you think about this post and if you are in a good day tell me how can I improve it 😄!

Now I can come back to the holiday and eat some delicious pastries that only Portugal can give to us 🍪 🍰! If you have been here you know what I am talking about (Pastéis de Nata)

Top comments (2)

Collapse
 
glaudsonsilva profile image
Glaudson Silva

Really cool!
Thanks for sharing your learning path.
I'd love to see a pic of the pastries you had in the next article. That's as important as code, right?

Collapse
 
caioragazzi profile image
Caio Ragazzi

Thanks @glaudsonsilva !
hahahaha of course, maybe in the next article some pastries pic may appear.