Azure Data Services: Blobbing did you say.. What is that? (Part 3)


Hello and welcome to this tutorial series of Azure Data Services. Through the course of this tutorial, I will take you through what Azure has to offer in Data Services. I will start with the basics of relational databases, dig into the goodness of non-relational offerings and end with what is new and what is exciting in Azure Data Services

Part 1: An Introduction to Azure Data Services
Azure Data Services: SQL in the Cloud (Part 2)

image

Yes, you read right. In this ‘blog’ post I am going to discuss ‘blobs’ (love the ring to it). And no, it is not the X-Men character and neither is it an alien. What I am referring to is a Binary Large Object, in short, BLOB. Blob Storage is one of the three (other two being table and queue) non-relational, persistent, PaaS data storage service from Azure.

What are Blobs and why should you care ?

Well, to begin with it is highly scalable(500 TB per storage account. A storage account can have a combination of blob, table and queue storage), highly available (99.9% uptime as per the SLA), persistent, redundant (triply replicated either locally or geographically) storage service from Azure primarily used to store files such as images, PDFs, videos, etc . This Blob Storage can be easily accessed using REST endpoints and programmatically (.NET, Java, Ruby, etc). The stored files can be made accessible to the public or secure it by allowing access to only authorized users or provide temporary access through tokens. If that is not enough to convince you of the resilience of Blob storage, Microsoft OneDrive uses Blob storage. In fact if you are building apps that plays around with files as described, you should consider using Blob Storage.

There are essentially two kinds of Blobs: Page Blobs and Block Blobs. Page Blobs are 512 byte pages which are optimized for random access and are tuned for high performance and reliability. A Page Blob can be no larger than 1TB. Block Blobs on the other hand are comprised of a series of blocks which is optimized for multimedia streaming scenarios. Block Blobs can be no larger than 200GB.

How would you use Blob?

First of all, we need to create a storage account on Azure. A storage account will contain one or many containers. Each container can contain one or more blobs. Each blob can be thought of as a 2 level hierarchical structure:

image (Note: The snapshots below are from the new preview portal: https://portal.azure.com)

Step 1: Create the storage container

image

Once you login into the new preview portal for Azure, you can select New in the bottom left, since Storage does not appear on the first blade, you will have to click on Everything when the ‘Marketplace’ opens, choose storage and then click on Storage.

image Now, after clicking on create a Storage account, there are a few parameters which need mention. It definitely needs a unique storage account name (step 2). The pricing tier (step 3) is interesting as the pricing varies based on the redundancy that you want your storage to incorporate. There are essentially 3 forms of redundancy: Locally Redundant (LRS), Geographically Redundant (GRS) and Read-Access GRS (RA-GRS). Locally implies within the same data center or facility where as Geographic implies triply replicated across different regions but in the same country. 

You can define a new Resource Group (step 4) or use an existing one. A resource group can be thought of as a collection of resources for a particular project. For example, you have created a website which will use a blob storage. In this case, you should select the same resource group to ensure that all your logical entities of your project belong to the same resource group. Finally, you can select the location where your primary storage should reside (step 5) and then you could click on Create (Step 6).

Step 2: Get details to access the blob storage and write client app to manage blob

image

Once you have created your storage account, you can get in the storage account, click on settings, properties and keys to see the relevant details .As you can see if you click properties, you get to see the 3 different end points per account storage that you create. An endpoint to a blob, a queue and a table. Using these endpoints you can access these storage containers.

If you click Keys, you get the primary/secondary access keys that are used to programmatically access the storage. Now let us create a client application that accesses and uses this storage.

Lets start with a blank console app, install the Windows Azure Storage Nuget Packages:

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Text;
   5: using System.Threading.Tasks;
   6:  
   7: using Microsoft.WindowsAzure.Storage;
   8: using Microsoft.WindowsAzure.Storage.Auth;
   9:  
  10: namespace BlobAccessApp
  11: {
  12:     class Program
  13:     {
  14:         static void Main(string[] args)
  15:         {
  16:         }
  17:     }
  18: }

Now lets connect with the Blob Storage created using constructs such as CloudStorageAccount and using the key retrieved from the keys section in the Azure Portal as shown above. Note that in this example we use the Primary Access Key, however if we were to use CloudStorageAccount.Parse then we would need to use the Primary Connection String:

   1: CloudStorageAccount account = new CloudStorageAccount(
   2:                 new StorageCredentials("myblobdemo",
   3:                     "*****************************************************************************"),
   4:                     true
   5:                 );

After having connected to the storage account, lets upload an image in the account. In the following code snippet, the explanation of each line is included in comments:

   1: //Create a blob container within the storage account that is created 
   2: var blobclient = account.CreateCloudBlobClient();
   3:  
   4: //Get hte container reference which is created in order to store the image in the container
   5: var container = blobclient.GetContainerReference("images");
   6: //This creates the blob if it does not already exist. 
   7: container.CreateIfNotExists();
   8:  
   9: //Get the blob reference and then upload the image to the continer using the blob reference
  10: var blob = container.GetBlockBlobReference("diagrams.png");
  11: blob.UploadFromFile(@"C:\Users\addatta\Desktop\blob.jpeg", FileMode.Open);
  12: Console.Read();

Now that we have written and understood the steps of uploading a file in the blob storage through a simple console app, let us get back to the portal and see where was this container created and access the image:

image

As you can see above, the container tile shows one container and the Container Blade shows the container “images” created and the corresponding URL. You can see the same thing from your Visual Studio Server Explorer too in Azure Storage.

Step 3: Access Control

The next part is to ensure access to the uploaded files. One way is to have a key using which you can directly read the blob at any time. However, there are 3 settings for “Public Read Access” which otherwise determine whether you can read the blob or not.

image

As you can see, there are 3 values for Public Read Access: Off, Blob, Container. If it is set to off, you can not access unless you have the key (which is true for all the other instances). If the setting is at Blob, then you can read the blob and if it is set at Container, you can enumerate Container blobs and read the blob. Either this value can be set in Visual Studio or it can be set on the portal as shown below:

image In Azure Portal:

image

If we set the permissions to Blob and then try to access the URL, we are able to access the uploaded image.

That is how we access a file in blob storage. However, there are other access methods such as Vallet Key Pattern using SAS tokens and Stored Access Policies which are more efficient and could be considered to define the access to your blob.

Summary

That will definitely get you started on Blobs. We started with discussing what a storage account in Azure is and the 3 different kinds of data services that can be availed. We then dived into what a Blob storage is and how scalable and available it is. We discussed how to create a blob in a storage account through a console app and finally the access control mechanisms providing secure access.

In the next blog post,  I will be discussing Tables in Storage account and some use cases where tables are preferred. Stay tuned and connect with me @AdarshaDatta and do share your experience using Azure with me. In the meantime I will leave you with one thought.


Comments (0)

Skip to main content