How to Create a Container in Azure Storage From MVC Application

I am working on a MVC application in which I need to work with Microsoft Azure storage. I had a requirement to create an Azure storage container from the MVC application. In this article I will share what I learned about creating a container programmatically.

I created the view as shown in the following image. The user must enter the Azure storage account name, Azure storage key and the name of the container to be created.

manage blog

The preceding View is a MVC razor view and it is created using the cshtml as shown below:

  1. @{  
  2.     ViewBag.Title = "BLOB Manager";  
  3. }  
  4. <div class="jumbotron">  
  5.     <div class="row">  
  6.         <div class="col-md-8">  
  7.             <section id="loginForm">  
  8.                 @using (Html.BeginForm("AzureInfo""Manage"new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))  
  9.                 {  
  10.                     @Html.AntiForgeryToken()  
  11.                     <h2> Manage BLOB Container</h2>  
  12.                     <hr />  
  13.                     @Html.ValidationSummary(true""new { @class = "text-danger" })  
  14.                     <div class="form-group">  
  15.                         <div class="col-md-10 input-group input-group-lg">  
  16.                             @Html.TextBox("AccountName"""new { @class = "form-control", @placeholder = "Account Name" })  
  17.                             @Html.ValidationMessage("AccountName"""new { @class = "text-danger" })  
  18.                         </div>  
  19.                     </div>  
  20.                     <div class="form-group">  
  21.   
  22.                         <div class="col-md-10 input-group input-group-lg">  
  23.                             @Html.TextBox("AccountKey"""new { @class = "form-control", @placeholder = "Account Key" })  
  24.                             @Html.ValidationMessage("AccountKey"""new { @class = "text-danger" })  
  25.                         </div>  
  26.                     </div>  
  27.                     <div class="form-group">  
  28.   
  29.                         <div class="col-md-10 input-group input-group-lg">  
  30.                             @Html.TextBox("ContainerName"""new { @class = "form-control", @placeholder = "Container Name" })  
  31.                             @Html.ValidationMessage("containername"""new { @class = "text-danger" })  
  32.                         </div>  
  33.                     </div>  
  34.                     <div class="form-group input-group input-group-lg">  
  35.                         <div class="col-md-offset-2 col-md-10">  
  36.                             <input type="submit" value="Create Container" class="btn btn-lg btn-success" />  
  37.                         </div>  
  38.                     </div>  
  39.   
  40.                 }  
  41.             </section>  
  42.         </div>    
  43.     </div>  
  44. </div>  
I am creating that using the following:
  • Three text boxes with the placeholder and the class set to bootstrap form-control.

  • A submit button with the class set to bootstrap btn-lg and btn-success.

  • All the controls are inside the form.

  • On form post operation, AzureInfo action of the Manage controller will be called.

Once the View is ready, let us proceed and create an Azure utility class. In this class we will put all the operations related to the Azure storage. But before you do you need to add an Azure storage library to the project. You can use either the NuGet Package Manager or the console to add the library. I am using the console to install the Azure storage library. I have taken the 4.3 version because when this article was written, this was the latest stable version available.

window Azure

Once the package is successfully installed, add the following namespaces.

  1. using Microsoft.WindowsAzure.Storage;  
  2. using Microsoft.WindowsAzure.Storage.Auth;  
  3. using Microsoft.WindowsAzure.Storage.Blob;  
  4. using Microsoft.WindowsAzure;  
To create the container in the Azure storage, I have created a function CreateContainer. This function takes three input parameters, account name, account key and the container name.
  1. using Microsoft.WindowsAzure.Storage;  
  2. using Microsoft.WindowsAzure.Storage.Auth;  
  3. using Microsoft.WindowsAzure.Storage.Blob;  
  4. using Microsoft.WindowsAzure;  
  5.   
  6. To create the container in the Azure storage, I have created a function CreateContainer. This function takes three input parameters, account name, account key and the container name.   
  7.   
  8. public static class AzureUtility  
  9.     {  
  10.         public static string  CreateContainer(string AccountName, string AccountKey, string ContainerName)  
  11.         {  
  12.   
  13.             string UserConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", AccountName, AccountKey);  
  14.             CloudStorageAccount storageAccount = CloudStorageAccount.Parse(UserConnectionString);             
  15.             CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();  
  16.             CloudBlobContainer container = blobClient.GetContainerReference(ContainerName.ToLower());  
  17.             if (container.CreateIfNotExists())  
  18.             {  
  19.                 container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });  
  20.                 return container.Name;  
  21.             }  
  22.             else  
  23.             {  
  24.                 return string.Empty;   
  25.             }  
  26.         }  
  27.     }  
Now let us examine the preceding code line by line.  
  • In the first line I created the connection string for the Azure storage. It takes two parameters, account name and the account key. These values are passed as input parameters to the function. I am using these two parameter to create the connection string.
  • Using CloudStorageAccount to parse the connection string from the constructed string.

  • Creating the CloudBlobClient.

  • Getting the container reference of the BLOB.

  • If the container reference does not exist, creating a new container.

  • On successful creation of the container the function returns the name of the container else an empty string. 

 

  1. [HttpPost]  
  2.         public ActionResult AzureInfo(string AccountName, string AccountKey, string ContainerName)  
  3.         {  
  4.            
  5.            var result= AzureUtility.CreateContainer(AccountName, AccountKey, ContainerName);  
  6.   
  7.            if (result != string.Empty)  
  8.              {  
  9.                   
  10.                  return RedirectToAction("Index""Home");  
  11.              }  
  12.              else  
  13.              {  
  14.                  return RedirectToAction("Index");  
  15.              }  
  16.         }  
In the action we are using the utility class to create the container. In this way an Azure container can be created from the MVC application.

I hope it is useful. Thanks for reading. Happy Coding.

 

Next Recommended Readings