Azure

Utilizing Azure Blob Storage In C#

Introduction

 

In at the moment’s article, we’ll take a look at a characteristic of Microsoft Azure, which is Microsoft’s Cloud providing. One of many most important providers supplied with Azure is Cloud Storage. This enables us to retailer totally different artifacts within the Cloud. These embrace Tables, Queues, Information, and Containers. Containers are used to retailer Blobs which could be any kind of file together with textual content recordsdata, picture recordsdata, video recordsdata, and many others. You may learn extra in regards to the various kinds of Blobs on the internet.

 

On this article, we’ll take a look at easy methods to create an Azure Blob Container after which utilizing C#, add a textual content file there.

The right way to create the Azure Storage Account and Container

 

With a view to full this step, you would want a Microsoft Azure account with an energetic subscription. You may get a free one for preliminary use. Nonetheless, as my free subscription ended a very long time in the past, I’ve arrange a pay-as-you-go one. Kindly comply with the under steps,

Upon getting created your Azure portal account and subscription, you see the under. You may see I’ve one subscription which is “Pay-As-You-Go”,

 

 

 

Choose “Create a useful resource”,

 

Using Azure Blob Storage in C#

 

Sort “Storage Account”,

 

Using Azure Blob Storage in C#

 

Using Azure Blob Storage in C# 

 

Click on “Create”,

 

Using Azure Blob Storage in C#

 

Right here, you choose the subscription and useful resource group (you possibly can create a brand new one if required),

 

Using Azure Blob Storage in C# 

 

Then, enter a Storage account title, area, efficiency, and redundancy worth. I cannot focus on these choices intimately right here as there are a lot of articles that element them.

 

As soon as finished click on “Overview + Create”, you will note the under,

 

Using Azure Blob Storage in C# 

 

Click on the Create button, useful resource group and storage account shall be created.

 

Using Azure Blob Storage in C#

 

Using Azure Blob Storage in C#

 

Subsequent, we have to create the container. Go to the principle web page,

 

Using Azure Blob Storage in C# 

 

Right here, you see the useful resource group and a storage account you’ve simply created. Choose the storage account after which the “Containers” possibility below “Knowledge storage” as under,

 

Using Azure Blob Storage in C# 

Subsequent, choose “+ Container” so as to add a brand new container as under,

 

Using Azure Blob Storage in C# 

Identify the container “blobcontainer” and create it.

 

Lastly, copy the connection string from the storage account as that is wanted to entry the container from the C# code.

 

Using Azure Blob Storage in C# 

 

Now, we’ll create our C# software to add a file to this container we now have simply created.

The right way to create the Visual Studio 2019 software

Subsequent, we’ll create a console software utilizing Visual Studio 2019 Group version as under,

 

Using Azure Blob Storage in C#

 

Using Azure Blob Storage in C#

 

Using Azure Blob Storage in C#

 

Using Azure Blob Storage in C#

 

As soon as finished we’ll add a category library to the challenge.

 

Using Azure Blob Storage in C# 

 

Subsequent, we’ll 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 now have the atmosphere all setup, add the code under to the “Program.cs” and “AzureBlobClient.cs” recordsdata,

 

Program.cs

  1. utilizing AzureBlobStorageClient;  
  2. utilizing System;  
  3. await AzureBlobClient.UploadBlob();  
  4. Console.ReadKey();   

AzureBlobClient.cs

  1. utilizing Azure.Storage.Blobs;  
  2. utilizing System;  
  3. utilizing System.IO;  
  4. utilizing System.Threading.Duties;  
  5. namespace AzureBlobStorageClient {  
  6.     public class AzureBlobClient {  
  7.         public static async Process UploadBlob() {  
  8.             var connectionString = “<Enter the connection string right 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.Mix(path, fileName);  
  15.             await File.WriteAllTextAsync(localFile, “This is a check message”);  
  16.             var blobClient = containerClient.GetBlobClient(fileName);  
  17.             Console.WriteLine(“Importing to Blob storage”);  
  18.             utilizing FileStream uploadFileStream = File.OpenRead(localFile);  
  19.             await blobClient.UploadAsync(uploadFileStream, true);  
  20.             uploadFileStream.Shut();  
  21.         }  
  22.     }  
  23. }  

Now, we run the applying and after that once we look into our container, we see that the brand new file has been uploaded there.

 

Using Azure Blob Storage in C#

Abstract

 

On this article, we checked out making a Microsoft Azure storage account after which added a blob container to it. Then, we created a Visual Studio challenge to create and add a file to this blob container. Within the coming articles, I’ll discuss in regards to the different information storage sorts.

 

Pleased coding!

Tags
Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Close