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.
The preceding View is a MVC razor view and it is created using the cshtml as shown below:
- @{
- ViewBag.Title = "BLOB Manager";
- }
- <div class="jumbotron">
- <div class="row">
- <div class="col-md-8">
- <section id="loginForm">
- @using (Html.BeginForm("AzureInfo", "Manage", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
- {
- @Html.AntiForgeryToken()
- <h2> Manage BLOB Container</h2>
- <hr />
- @Html.ValidationSummary(true, "", new { @class = "text-danger" })
- <div class="form-group">
- <div class="col-md-10 input-group input-group-lg">
- @Html.TextBox("AccountName", "", new { @class = "form-control", @placeholder = "Account Name" })
- @Html.ValidationMessage("AccountName", "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
-
- <div class="col-md-10 input-group input-group-lg">
- @Html.TextBox("AccountKey", "", new { @class = "form-control", @placeholder = "Account Key" })
- @Html.ValidationMessage("AccountKey", "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
-
- <div class="col-md-10 input-group input-group-lg">
- @Html.TextBox("ContainerName", "", new { @class = "form-control", @placeholder = "Container Name" })
- @Html.ValidationMessage("containername", "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group input-group input-group-lg">
- <div class="col-md-offset-2 col-md-10">
- <input type="submit" value="Create Container" class="btn btn-lg btn-success" />
- </div>
- </div>
-
- }
- </section>
- </div>
- </div>
- </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.
Once the package is successfully installed, add the following namespaces.
- using Microsoft.WindowsAzure.Storage;
- using Microsoft.WindowsAzure.Storage.Auth;
- using Microsoft.WindowsAzure.Storage.Blob;
- 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.
- using Microsoft.WindowsAzure.Storage;
- using Microsoft.WindowsAzure.Storage.Auth;
- using Microsoft.WindowsAzure.Storage.Blob;
- 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.
-
- public static class AzureUtility
- {
- public static string CreateContainer(string AccountName, string AccountKey, string ContainerName)
- {
-
- string UserConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", AccountName, AccountKey);
- CloudStorageAccount storageAccount = CloudStorageAccount.Parse(UserConnectionString);
- CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
- CloudBlobContainer container = blobClient.GetContainerReference(ContainerName.ToLower());
- if (container.CreateIfNotExists())
- {
- container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
- return container.Name;
- }
- else
- {
- return string.Empty;
- }
- }
- }
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.
- [HttpPost]
- public ActionResult AzureInfo(string AccountName, string AccountKey, string ContainerName)
- {
-
- var result= AzureUtility.CreateContainer(AccountName, AccountKey, ContainerName);
-
- if (result != string.Empty)
- {
-
- return RedirectToAction("Index", "Home");
- }
- else
- {
- return RedirectToAction("Index");
- }
- }
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.