Azure BLOB Storage: Using the Storage Client Library

Introduction

Today, I will introduce Azure BLOB storage, a very simple and useful way to create a storage account and container, where we can maintain files (byte arrays and objects). There are basically these
following 4 basic hierarchies for Azure BLOB storage:

  1. CloudStorageAccount: This is a reference to storage account (we will create a new sample storage account in this article).
  2. CloudBlobClient: This reference to the object is used to perform operations on a BLOB storage account.
  3. CloudBlobContainer: This is a reference to the container where the BLOB resides in.
  4. CloudBlockBlob: This refer to actual BLOB.
When we write the code samples you will have the better understanding of these 4 points mentioned above.

First of all we will need to create a new storage account in Azure.

After creating a sample account we will get the storageaccount and sample key where we will create the container and put our BLOB. Now we will create a sample application for the BLOB storage.

Creating a Sample Application

Create a simple Console application, AzureStorageSample.

Then add a sample class library project.

Now add a WindowsAzure.storage Nuget package.

Now I am adding the following code to the TestAzureRespository class.

 

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Configuration;    
  4. using System.Linq;    
  5. using System.Text;    
  6. using System.Threading.Tasks;    
  7. using Microsoft.WindowsAzure.Storage;    
  8. using Microsoft.WindowsAzure.Storage.Blob;    
  9. namespace AzureBlobRepository    
  10. {    
  11.     public class TestAzureRespository    
  12.     {    
  13.         public CloudBlobClient BlobClient    
  14.         {    
  15.             get;    
  16.             set;    
  17.         }    
  18.         public CloudBlobContainer BlobContainer    
  19.         {    
  20.             get;    
  21.             set;    
  22.         }    
  23.         public TestAzureRespository()    
  24.         {       }    
  25.         private void GetContainer(string ContainerName)    
  26.         {    
  27.             var connectionString = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;    
  28.             var storageAccount = CloudStorageAccount.Parse(connectionString);    
  29.             BlobClient = storageAccount.CreateCloudBlobClient();    
  30.             // Retrieve a reference to a container.    
  31.             BlobContainer = BlobClient.GetContainerReference(ContainerName);    
  32.             // Create the container if it doesn't already exist.    
  33.             BlobContainer.CreateIfNotExists();    
  34.         }    
  35.         public string AddToBlobSTorage(string ContainerName, byte[] FileStream)    
  36.         {    
  37.             GetContainer(ContainerName);    
  38.             string blobName = Guid.NewGuid().ToString();    
  39.             CloudBlockBlob blockBlob = BlobContainer.GetBlockBlobReference(blobName);    
  40.             blockBlob.UploadFromByteArray(FileStream, 0, FileStream.Length);    
  41.             return blobName;    
  42.         }    
  43.         public string AddToBlobSTorage(string ContainerName, string Text)    
  44.         {    
  45.             GetContainer(ContainerName);    
  46.             string blobName = Guid.NewGuid().ToString();    
  47.             CloudBlockBlob blockBlob = BlobContainer.GetBlockBlobReference(blobName);    
  48.             blockBlob.UploadText(Text);    
  49.             return blobName;    
  50.         }    
  51.         public byte[] GetBytesFromBlobStorage(string ContainerName, string BlobName)    
  52.         {    
  53.             GetContainer(ContainerName);    
  54.             CloudBlockBlob blockBlob = BlobContainer.GetBlockBlobReference(BlobName);    
  55.             blockBlob.FetchAttributes();    
  56.             byte[] byteArray = new byte[blockBlob.Properties.Length];    
  57.             blockBlob.DownloadToByteArray(byteArray, 0);    
  58.             return byteArray;    
  59.         }    
  60.         public string GetStringFromBlobStorage(string ContainerName, string BlobName)    
  61.         {    
  62.             GetContainer(ContainerName);    
  63.             CloudBlockBlob blockBlob = BlobContainer.GetBlockBlobReference(BlobName);    
  64.             blockBlob.FetchAttributes();    
  65.             return blockBlob.DownloadText();    
  66.         }    
  67.     }    
  68. }  

 

Where the connection string is set on the web config file like this:

 

  1. <connectionStrings>    
  2.     <add name="connectionstring_name" connectionString="DefaultEndpointsProtocol=https;AccountName=your_azure_storage_account_name;AccountKey=your_azure_storage_account_key" />    
  3. </connectionStrings>  

 

Now I will introducing the calling code as in the following:

 

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5. using System.Threading.Tasks;    
  6. using AzureBlobRepository;    
  7. namespace AzureStorageSample    
  8. {    
  9.     class Program    
  10.     {    
  11.         static void Main(string[] args)    
  12.         {    
  13.             TestAzureRespository aurRepos = new TestAzureRespository();    
  14.             string test = "Hi Vivek This is only for test purpose";    
  15.             byte[] array = Encoding.ASCII.GetBytes(test);    
  16.             string guid1 = aurRepos.AddToBlobSTorage("Azure_Container_Name_1", array);    
  17.             string guid2 = aurRepos.AddToBlobSTorage("Azure_Container_Name_2", test);    
  18.             string getmesg = aurRepos.GetStringFromBlobStorage("Azure_Container_Name_2", guid2);    
  19.             Console.WriteLine(getmesg);    
  20.         }    
  21.     }    
  22. }    

 

When running this code you will see the same string message when fetching from the BLOB.

For a better understanding, please follow the link:

How to use BLOB.