Working With Azure Storage Services (BLOB)

Here in this article we will going to learn about azure storage services and see how we can upload any media files in Blob.

Introduction to Azure Storage Service
 
Azure Storage is Microsoft's modern storage solution. If you compare the previous storage techniques with Azure Storage Services, you will find a lot of differences in security, availability, and accessibility.

Here, we will discuss all these Azure Storage benefits and will work on Blob Storage service.
  1. Durable and Highly Available
    Your data in Azure service is distributed over geographical data center and will be available to the users in case of any system, software or hardware Failure. This redundancy ensures that the data is safe and can be accessible even in case of disaster.

  2. Secure
    All the data written to the Azure Storage is encrypted by services.

  3. Highly Scalable
    You can scale up horizontally or vertically as per your requirement.

  4. Accessible
    Data in Azure storage can be accessible from everywhere in the world using HTTP or HTTPS protocol. Microsoft provides SDKs for Azure Storage in various languages.
There are mainly 4 types of standard storage services  in Azure,
  1. BLOB
  2. Table
  3. Queues
  4. File storage
Here in this session, we will work with blob storage.
  • Blob storage is mainly for storing a massive amount of unstructured data such as text or binary data.
  • Blob storage is ideally for Images, audio, video, etc.
  • Its also used for stream Audio and Video.
  • You can also store backup files.
  • Objects in the blob can be accessed from anywhere in the world using HTTP or HTTPS protocol.
  • Blob data is stored in a container, so we can use a large number of containers for storing a large number of the blob data. A container provides the security from the outer world to protect our data.


    Now, let's start working with a Azure Blob storage. To start working with Blob, we need to create a storage account like this,

    • First, go to your Azure Portal and log in with your credentials.
    • Search for Storage Account in the portal.

      Now, create a storage account as shown below.


      Here, to create the account, we need the following details.
      • Name
        The name should be unique and always in small letters.

      • DeploymentModel
        For resources model always choose Resource Manager (for new applications).

      • Account Kind

        • General Purpose V1(GPV1)
          GPV1 account provides access to all Azure storage services but may not have the latest features.
          Ex
          Cool storage is not supported in GPV1.

        • General Purpose V2(GPV2)
          It supports all the latest features of BLOB, FILE, QUEUE and Tables, it supports all APIs supported in GPV1.I and it supports hot and cool accounts.

        • BLOB
          It supports all block blob features as does GPV2.

      • Replication
        We have LRS\ZRS\GRS

        • The hierarchy will be like this.

      • LOCAL REDUNDANT STORAGE(LRS)

        • Replicated 3 times within a single data center in a single region where storage account  is created.
        • It protects your data from server hardware failure.
        • This is only available in a premium storage account.

      • ZONE REDUNDANT STORAGE(ZRS)

        • Replicate 3 copies of your data across 2-3 across 2-3 Zones.
        • ZRS is added in Azure after 2014.

      • GEO-REDUNDENT STORGE(GRS)

        • GRS maintain 6 copies of your data and the data is replicated over zones.
Access Tier
  • Hot: Hot storage has higher storage costs than cool  storage, but the lowest access costs. 
  • Cool:Cool storage tier has lower storage costs and higher access costs compared to hot storage.
Once you create an account ,then you need to create a container. Here is how you can create a container.


Once the container is ready, you can upload your files as blob.


Now let's create an MVC application and save image files to Blob.

Here is my MVC view,
  1. @{  
  2.     ViewBag.Title = "Home Page";  
  3. }  
  4. <script src="~/Scripts/jquery-1.10.2.min.js"></script>  
  5.   
  6. <script type="text/javascript">  
  7.    function show(input) {  
  8.       if (input.files && input.files[0]) {  
  9.       var filerdr = new FileReader();  
  10.       filerdr.onload = function (e) {  
  11.          $('#user_img').attr('src', e.target.result);  
  12.       }  
  13.       filerdr.readAsDataURL(input.files[0]);  
  14.    }  
  15. }  
  16. </script>    
  17.   
  18.   
  19. <div class="jumbotron">  
  20.   
  21.     @using (Html.BeginForm("Index", "Home", FormMethod.Post, new  
  22.     {  
  23.         enctype = "multipart/form-data"  
  24.     }))  
  25.     {  
  26.         <div class="form-group">  
  27.             <div class="col-md-10">  
  28.                 <label>Upload Your Image</label>  
  29.             </div>  
  30.          <br />  
  31.             <img id = "user_img"  height = "300"  width = "500"  style = "border:solid" / >  
  32.             <br />  
  33.             <div class="col-md-10">  
  34.                 <input type="file" name="image" accept="image/*" class="form-control fileupload" onchange="show(this)" />  
  35.             </div>  
  36.             <div class="col-md-10">  
  37.                 <input type="submit" title="save" />  
  38.             </div>  
  39.               
  40.   
  41.         </div>  
  42.   
  43.         <div>  
  44.         </div>  
  45.          
  46.     }  
  47.   
  48.         </div>  
  49.   
  50.           

Now go to the portal container and copy the connection string and paste in web.config as follow.


Now copy the connection string and paste in the Web.config as follow.
  1. <connectionStrings>  
  2.    <add name="StorageConnectionString" connectionString="DefaultEndpointsProtocol=https;AccountName=myazurecontainer;AccountKey=cc1tX3yX9KrlyflrsZDzd4OGSrELk7D+aJP6ntaJokUPD6v1xAIw==;EndpointSuffix=core.windows.net" />  
  3.  </connectionStrings>  
Now download a storage client to work on Azure storege service from Nuget Package.


Now here is the code in controller to save the file in Blob.The below code will read the connection string from config file.
  1. CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ToString());  
Here is the code to save the file in Blob.
  1. [HttpPost]  
  2.        public ActionResult Index(HttpPostedFileBase image)  
  3.        {  
  4.           
  5.            if (image.ContentLength>0)  
  6.            {  
  7.                
  8.                CloudBlobClient client = storageAccount.CreateCloudBlobClient();  
  9.                  
  10.                CloudBlobContainer container = client.GetContainerReference("myazurecontainer");  
  11.                //container.CreateIfNotExists();  
  12.                container.SetPermissions(new BlobContainerPermissions  
  13.                {  
  14.                    PublicAccess = BlobContainerPublicAccessType.Blob  
  15.   
  16.                });  
  17.                //upload image to blob  
  18.                CloudBlockBlob blob = container.GetBlockBlobReference(image.FileName);  
  19.                blob.UploadFromStream(image.InputStream);  
  20.            }  
  21.            return View();  
  22.        }  

Now click submit to save the file in Azure Blob.You can find the file in blob as shown below.


So in this way we can upload files in Azure Blob storage.