Using Azure Blob Storage In C#

Introduction

 
In today’s article, we will look at a feature of Microsoft Azure, which is Microsoft’s Cloud offering. One of the main services provided with Azure is Cloud Storage. This allows us to store different artifacts in the Cloud. These include Tables, Queues, Files, and Containers. Containers are used to store Blobs which can be any type of file including text files, image files, video files, etc. You can read more about the different types of Blobs on the web.
 
In this article, we will look at how to create an Azure Blob Container and then using C#, upload a text file there.

How to create the Azure Storage Account and Container

 
In order to complete this step, you would need a Microsoft Azure account with an active subscription. You can get a free one for initial use. However, as my free subscription ended a long time ago, I have set up a pay-as-you-go one. Kindly follow the below steps,

Once you have created your Azure portal account and subscription, you see the below. You can see I have one subscription which is “Pay-As-You-Go”,
 
Using Azure Blob Storage in C# 
 
Select “Create a resource”,
 
Using Azure Blob Storage in C#
 
Type “Storage Account”,
 
Using Azure Blob Storage in C#
 
Using Azure Blob Storage in C# 
 
Click “Create”,
 
Using Azure Blob Storage in C#
 
Here, you select the subscription and resource group (you can create a new one if required),
 
Using Azure Blob Storage in C# 
 
Then, enter a Storage account name, region, performance, and redundancy value. I will not discuss these options in detail here as there are many articles that detail them.
 
Once done click “Review + Create”, you will see the below,
 
Using Azure Blob Storage in C# 
 
Click the Create button, resource group and storage account will be created.
 
Using Azure Blob Storage in C#
 
Using Azure Blob Storage in C#
 
Next, we need to create the container. Go to the main page,
 
Using Azure Blob Storage in C# 
 
Here, you see the resource group and a storage account you have just created. Select the storage account and then the “Containers” option under “Data storage” as below,
 
Using Azure Blob Storage in C# 

Next, select “+ Container” to add a new container as below,
 
Using Azure Blob Storage in C# 

Name the container “blobcontainer” and create it.
 
Finally, copy the connection string from the storage account as this is needed to access the container from the C# code.
 
Using Azure Blob Storage in C# 
 
Now, we will create our C# application to upload a file to this container we have just created.

How to create the Visual Studio 2019 application


Next, we will create a console application using Visual Studio 2019 Community edition as below,
 
Using Azure Blob Storage in C#
 
Using Azure Blob Storage in C#
 
Using Azure Blob Storage in C#
 
Using Azure Blob Storage in C#
 
Once done we will add a class library to the project.
 
Using Azure Blob Storage in C# 
 
Next, we will add the required Nugget packages,
 
Using Azure Blob Storage in C#
 
Using Azure Blob Storage in C#
 
Using Azure Blob Storage in C# 
 
Now that we have the environment all setup, add the code below to the “Program.cs” and “AzureBlobClient.cs” files,
 
Program.cs
  1. using AzureBlobStorageClient;  
  2. using System;  
  3. await AzureBlobClient.UploadBlob();  
  4. Console.ReadKey();   
AzureBlobClient.cs
  1. using Azure.Storage.Blobs;  
  2. using System;  
  3. using System.IO;  
  4. using System.Threading.Tasks;  
  5. namespace AzureBlobStorageClient {  
  6.     public class AzureBlobClient {  
  7.         public static async Task UploadBlob() {  
  8.             var connectionString = "<Enter the connection string here>";  
  9.             string containerName = "blobcontainer";  
  10.             var serviceClient = new BlobServiceClient(connectionString);  
  11.             var containerClient = serviceClient.GetBlobContainerClient(containerName);  
  12.             var path = @ "c:\temp";  
  13.             var fileName = "Testfile.txt";  
  14.             var localFile = Path.Combine(path, fileName);  
  15.             await File.WriteAllTextAsync(localFile, "This is a test message");  
  16.             var blobClient = containerClient.GetBlobClient(fileName);  
  17.             Console.WriteLine("Uploading to Blob storage");  
  18.             using FileStream uploadFileStream = File.OpenRead(localFile);  
  19.             await blobClient.UploadAsync(uploadFileStream, true);  
  20.             uploadFileStream.Close();  
  21.         }  
  22.     }  
  23. }  
Now, we run the application and after that when we look into our container, we see that the new file has been uploaded there.
 
Using Azure Blob Storage in C#

Summary

 
In this article, we looked at creating a Microsoft Azure storage account and then added a blob container to it. Then, we created a Visual Studio project to create and upload a file to this blob container. In the coming articles, I will talk about the other data storage types.
 
Happy coding!


Similar Articles