Windows Azure provides various cloud enabled storage services and Blob storage is one of them. BLOB stands for “Binary Large Object” which means unstructured data that typically includes images, documents, audio, video or other multimedia objects. The main purpose for using BLOB cloud services is it has a many advantages over storing files in local file systems.
As the msdn suggests the advantages are :-
And many more, for the time these are of more focus.
This article mainly focuses on use of cloud services as here is Azure, checking for containers in our development storage and creating blobs dynamically into it and uploading files to it, as its being used in almost every sites.
In order to develop Azure Cloud enabled applications, we need to install SDK(Software Development Kit-A software development tool required to create application for specific software) first. The best way to develop SDK is :
It will then redirect you to download the SDK if its not installed.
Now lets start our play with MVC, Add a class to it that would create and check for the containers(I have named as BlobStorageServices.cs) and the code goes as mentioned below:-
//namespaces used using Microsoft.WindowsAzure.ServiceRuntime; using Microsoft.WindowsAzure.Storage; using Microsoft.WindowsAzure.Storage.Blob; using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MvcWebRole1 { //Container information public class BlobStorageServices { public CloudBlobContainer GetCloudBlobContainer() { CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("ImageUploadSetting"));//gets settings value CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();//activates the storage account on local CloudBlobContainer blobContainer = blobClient.GetContainerReference("imagesupload"); if(blobContainer.CreateIfNotExists()) { blobContainer.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });//Allowing public Access } return blobContainer; } } }
Now add a action method to your HomeController. I have named the action as Upload[HttpGet]/[HttpPost]. The code goes below:-
BlobStorageServices blobStorageServices = new BlobStorageServices();//Instantiate object of the class we just created for containers. /// <summary> /// Uploads this Image. /// </summary> /// <returns>Views of blobs</returns> [HttpGet] public ActionResult Upload() { CloudBlobContainer blobContainer = blobStorageServices.GetCloudBlobContainer(); List<string> blobs=new List<string>(); foreach(var blobItem in blobContainer.ListBlobs()) { blobs.Add(blobItem.Uri.ToString());//Returns all the blobs Uri in the container. } return View(blobs);//Returns to view to display all blobs. }
/// <summary> /// Uploads the specified image. /// </summary> /// <param name="image">The image.</param> /// <returns>Redirected to Upload Action</returns> [HttpPost] public ActionResult Upload(HttpPostedFileBase image) //Accept the posted file { try { if (image.ContentLength > 0) { CloudBlobContainer blobContainer = blobStorageServices.GetCloudBlobContainer(); CloudBlockBlob blob = blobContainer.GetBlockBlobReference(image.FileName); //First Blob // Upload content to the blob, which will create the blob if it does not already exist. blob.UploadFromStream(image.InputStream); } } catch(Exception ex) //Catches Null Exception here and stores it to blob { CloudBlobContainer blobContainer = blobStorageServices.GetCloudBlobContainer(); CloudBlockBlob blob2 = blobContainer.GetBlockBlobReference("myfile.txt"); //Second Blob blob2.UploadText(ex.ToString()); } return RedirectToAction("Upload"); }
Here I have gone for an exception handling and a better way is I am uploading the exception content creating another blob (myfile.txt). Here we upload image and a text file that includes exception, incase it occurs.
Now lets work on the UI part. I am coding a simple UI design and the code goes here:-I have used Razor in MVC4.
@{ ViewBag.Title = "Upload"; } <h2>Upload Image</h2> <p> @using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) { <input type="file" name="image" /> <input type="submit" value="Upload" /> } </p> <ul> @foreach (var item in Model) { <li> <img src="@item" alt="Images uploaded"width="150"height="150" /> </li> } </ul>
Lets just overview about the WebRole, WorkerRole and Queue in Azure:-
Web Role in Windows Azure are special purpose services and provide a dedicated IIS used for hosting web application, we can easily add and deploy application to webrole and scale as per our compatibility as per requirement.It is basically front-end that appears to the users.
Worker Role are processes that can do some work as the name suggests. For example, compressing an uploaded image automatically or on the fly, any changes in the database, best is notifications that are recieved.
Queue contain set of messages and those messages should be queue. The main thing is that Web Role, Worker Role and Queues are Virtual Machines running on cloud.
]]>