In today’s article, we will create Azure blobs through C# in ASP.NET Core console application using Azure SDK.
Why do we need this?
This article is for those who are trying to get a hands-on experience of Azure. Today, we can see media files everywhere, such as images, videos, audios etc. In order to store them on the cloud securely, we can use Azure Blob Storage.
Let’s get started.
First, you need an Azure account. So, go
here and log in. If you don’t have an Azure account, you can sign up for a free 30-day free trial.
Once logged in, go to the dashboard and click on "Create a resource".
Then, in the search box, type Storage and click on the "Storage Account" option that is shown in the search results.
Now, click "Create".
There are some fields that you need to fill-in.
I have picked "Storage (general purpose v1)" because it is for testing only.
In the replication category, I have picked Locally-redundant because I want my backup data to be stored in the same physical location. You can pick one according to your requirement. For further reading the difference between them, please
click here!
After that, there are some basic fields and my settings. I just clicked on "Create".
Once you click "Create", you will see a progress bar in the notification area.
Once it is completed, click on "Go to resource".
Now, click "Access Keys" on the left menu.
You will see a key and connection string. Store them; we will need them in our app.
Now, our Azure part is done. We will go to our Visual Studio and click on "New project". Then, select "Console Application in ASP.NET Core".
Once our project is created, we need to add some Azure packages from NuGet in order to contact with Azure.
In NuGet search, search for Azure and install "WindowsAzure.Storage".
Now, we will write our code in order to communicate with Azure.
First, we will define a variable and store the connection string in that variable. You can store that in appSettings.json or in config files. This is just for testing purposes, so I have just stored it in a local variable (THIS IS NOT RECOMMENDED).
Then, we will create the object for the CloudStorageAccount class and parse it with our connection string. Similarly, let's create an object of CloudBlobClient and we will use our storage account object to get the reference.
Then, create the object of CloudBlobContainer and then from the blob client object, we will get the reference of our container. Here, that container is azureravicontainer.
What this line will do is that if we have any container with this name, then we will get its reference in our object. As you can see, our next line is to create the container if it does not exist. Once our container is created, we will create the CloudBlobBlock’s object and will get the reference from the container.
It will do the same thing, that is, from the name, it will try to get the reference of the blob within that particular container.
After that, we have to call our method, i.e., "UploadFile".
As we can see in our UploadFile method, we have given a path of the pdf file and converted it into a stream and then, we have uploaded that stream through the CloudBlob block object.
So now, let us see our Azure before running our code.
We have no containers yet. Let’s run our code.
These are the print statements that I have used in the code. Now, let's go back to Azure and see if we can see our file/blob.
When we hit "Refresh", we can see that our container is created. Just click that container.
Here, we can see our blob being successfully uploaded to Azure.
Now, I have created another method to get our file from Azure in the same format as we have uploaded. This method checks whether the blob exists or not. If it exists, then we download the file to the particular location with the file name.
Let’s call and run this.
As we can see, our file is completely downloaded in the same format. If you wish to see/download the code, please
click here.
I have added some extra methods in the code, such as get container attributes, set metadata, get metadata, copy blob, and create directory and upload.
Summary
In this article, we have seen how to connect to Azure storage, create Azure storage containers, create blobs from our files, upload files, download blobs, and use them as our original uploaded files.
Hope you all liked it.
Happy Coding!