Create a Windows Azure Storage service to store the blobs


Objective:

This article describes what Windows Azure Blobs are, how to create and display the same on our local environment and lastly how to create a Windows Azure Storage service to store the blobs on the cloud.

The article explains all of the above using a very simple web application, which will be uploading the files (blobs) to the storage. The web application will also be retrieving the URIs of the blobs which are already uploaded to the storage.

Introduction:

Windows Azure: Microsoft's Windows Azure platform is a cloud platform, which provides a wide range of internet services that can be consumed.

Azure Storage: Windows Azure storage services allow us to store/retrieve the NON RELATIONAL data to/from Windows Cloud environment. (For relational data, SQL Azure services are used).

In Windows Azure Storage, the data can be stored in 3 different formats (v.i.z. Blobs, Tables and Queues). The retrieval/storage of the above data is done in RESTful way.

What is a Blob?

Blob stands for 'binary large object', which is an array of raw bytes. The following figure shows the hierarchical structure used for the Blob storage in Windows Azure:

1.gif

As shown in the above diagram, one Windows Azure Storage Account can have multiple Containers. A Container can be considered as an array or collection of one or more Blobs. Also, each Blob can have one or more metadata properties to define what the Blob content is all about. The metadata properties are Name-Value collection of strings.

As mentioned before, the contents of every Blob on Windows Azure can be accessed by browsing its corresponding URI (REST). The URI is usually of the following format:

https://<Account>.blob.core.windows.net/<Container>/<BlobName>

Now that we have covered the hierarchical structure of the Blob storage, let us delve into the prerequisites which shall help us to create the above mentioned structure and later store and retrieve the Blobs from the same.

Working on the Development Storage and later on the actual Windows Azure Cloud Storage:

For working on any of the Windows Azure applications, we need a development environment, which simulates the actual cloud environment. Hence to have this simulated cloud environment, we need the Windows Azure Tools for VS2008/ VS 2010 to enable the development and packaging of applications meant for Windows Azure. 

The Windows Azure Tools can be downloaded from the following link:


The sample application that we will be writing would use VS2008 + sp1 and the above mentioned Azure tools. 

Now that we have the prerequisites in place, we shall delve into the coding nitty-gritty.

Building the Web Application:

Step 1: Creating the new Cloud Service Web Application 

Open VS2008 as an administrator. Click on File -> New -> Project. Select the Cloud Service as the project type (under Visual C# node). We will name our app as 'BlobStorageManipulation'. The same is described in the below snap shot. Click on OK.

2.gif

After we click OK, Visual Studio displays the next screen having a different set of applications. We want to create a simple web app. So we select 'ASP.NET Web Role' only by selecting it and clicking on the '>' key. Click on OK.

3.gif

After we follow the above mentioned steps, we should be able to see our solution explorer as follows:

4.gif

By following the above mentioned steps, we have created a new Cloud Service Web Application. Now we need to fill it up a bit to make it working.

Step 2: Designing the GUI

We will keep a very simple GUI primarily to do two things:

  1. Enable us to upload a document from our local computer to the storage. For this we would require a file upload control and an 'Upload' server control button, to "trigger" this act of uploading to the cloud storage.
  2. Enable us to view the already uploaded documents (blobs) in our container. For this we would need a 'DisplayBlobs' server control button, to trigger the view event and 'lblURIs' label control to display the URIs of the uploaded the Blobs.
Hence, we will design our GUI in the 'Default.aspx' page of our 'WebRole1' project. The Default.aspx page at the design time should look something like below:

5.gif

The following is the source code for the above GUI:

6.gif

Step 3: Writing the code behind:

As we have written the GUI code in the Default.aspx, our code behind code will come in Default.aspx.cs file.

First things first. We need to add the below mentioned references to write our code behind.

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

Now that we are uploading the blobs to the cloud storage, we need to create a container first. And we also have to set its permission settings. We shall write a method for the same in our code behind file by the name 'SetContainerAndPermissions' to do this job for us and we would call this method on the very first call to our page.

Hence, our code for the same would be as follows:

7.gif

In the above code, we are calling the method 'SetContainerAndPermissions' to initialize our container settings on the Page_Load event of our page.  The method first creates the container by the name 'documents' in the storage and then it sets the permissions. The storage account information settings are obtained from the configuration settings. We have stored the same under the name 'BlobConn'. We would look into the settings, a bit later.

We have, till this point, created the container for uploading our documents. Now we need to write the code to upload our documents to the cloud storage. As we have 'btnUpload' button in the GUI to trigger the event, we need to write our code in its click event. The same would be as follows:

8.gif

The code is quite self explanatory here. We first look for our container by the name 'documents' and take its reference. We then set the name of the uploaded file to some unique name. We then create a blob in the 'documents' container and set its metadata. And finally, we upload the blob to the mentioned container.

Now that we have written the code to upload the files, the only functionality remaining is to browse to our container 'documents', where we have stored all our blobs and list their respective URIs. The code would come in the click event of the button 'btnDisplayBlobs' and the same would be as follows:

9.gif

Needless to say, it is a very simple code. . We first look for our container by the name 'documents' and take its reference. We then get the reference to the blob collection in this container and then we display their respective URIs on the screen.

With this, we have completed the desired code behind. But we now need to set the configuration settings. The following sections deals with the same:

Step 4: Configuration Settings

To set the configuration settings, in our solution explorer, go to the Azure project. Go to BlobStorageManipulation -> Roles -> WebRole1. Right click on WebRole1 and go to properties.

Now click on the Settings tab (on the left hand side of the screen), click on Add Setting and fill in the following details:

Name: BlobConn (Same name used in SetContainerAndPermissions method.)
Type: ConnectionString
Value: UseDevelopmentStorage=true

Your screen should look as follows:

10.gif

To set the 'Value' to 'UseDevelopmentStorage=true', click the browse button. You will get the following screen:

11.gif

Select the 'Use development storage' radio button. 

You would be wondering what 'UseDevelopmentStorage=true' is all about. As I mentioned in the very beginning, when we develop any application meant for Windows Azure, we first run it on the local environment (thanks to Windows Azure SDK tools). The SDK sets up a virtual cloud environment on our development machine and hence when we try to run and test it on our local environment, we need to point our storage location to our local simulated environ.

'UseDevelopmentStorage=true' does the trick for us. Once we want to access the actual Windows Azure storage, we will select the 'Enter storage credentials' radio button and key-in our Windows Azure storage details (which I shall be explaining later).

Also, we need to take care of one more idiosyncrasy. We need to set up a handler to update the CloudStorageAccount instances when the corresponding settings change in the service configuration. To write this handler, go to the WebRole.cs. In the end, our WebRole.cs should look as follows:

12.gif

Now we are all set to run our application and store and retrieve the blobs to our local simulated environment.

Step 5: Running the application to access the Development Storage

Compile the entire solution and run the WebRole1 project and browse to Default.aspx page.

When we run it, the Development Fabric and the Development Storage starts for us to exploit it. The Default.aspx page looks as follows:

13.gif

Browse to the desired text file. (We will experiment only with the .txt files for the sake of simplicity). And hit the 'Upload' button. It will store the uploaded text file in the form of a blob in a container by the name 'documents'. Please note that the storage used currently is our local Development Storage.

Now if we want to look for the URIs of the already uploaded files (blobs), hit the 'DsiplayBlobs' key. And we get the following list:

14.gif

Kindly note that the names would be different on every developer's machine as we have used GUID for uniqueness. 

With this, we have successfully completed an app which loads the blobs to the Development Storage and retrieves the URIs from the same. 

Please note the URIs that we obtain are of the format:

http://<Account>/<Container>/<BlobName> 

If you browse to any one of the URIs, you get to see the file as follows:

15.gif

Now comes the fun part. Doing the same on the actual Windows Azure Storage.

Step 6: Creating the Windows Azure Storage Account

To create a Windows Azure Storage account, we need a Windows Azure account credentials. I shall use mine for our demo.
  1. Go to the Windows Azure developer portal at: https://windows.azure.com and create a new project. I have already created one and hence my screen looks something as follows: 

    16.gif
  2. Now click on the project, and then click 'New Service' link on the top right corner and then click on 'Storage Account' to create an Azure Storage account.

    17.gif
  3. Now, we key in the details like Service Label and Service Description.
  4. On the next screen, we enter the desired storage account name. Kindly note that the account name should be all lower-case. After we enter the account name, we need to check the availability of the domain. We then select the desired region where we want the storage to be hosted. I shall enter the following options and click on the 'Create' button: 

    18.gif
  5. On the next screen, we see the Cloud Storage end points and the Primary and the Secondary Access key as follows: Kindly note that I have deliberately rubbed off the primary access key details from the below image.

    19.gif
Now that we have the endpoints and the Primary access key, we have everything in our arsenal to load our blobs to the Windows Azure Storage. The same can be achieved as described in the next step.

Step 7: Uploading the blobs to Windows Azure Storage

We initially ran our application to point to our local Development Storage. Now we need to point our application to our newly created Windows Azure Account. For the same we need to follow the following steps:
  1. Go to BlobStorageManipulation -> Roles -> WebRole1. Right click on WebRole1 and go to properties.
  2. Now we need to modify the Connection String settings as follows:

    Name: BlobConn (Same name used in SetContainerAndPermissions method.)
    Type: ConnectionString
  3. To set the 'Value', click on the browse button and enter the details as follows:

    20.gif

    Kindly note that I have deliberately rubbed off the Account key details. Click on OK and save.

    Do the same settings for the 'DiagnosticsConnectionString', as mentioned above.
  4. Now run the application. When the default.aspx page opens, upload any text document and click on the 'Upload' button. This time, your blob gets stored in the Windows Azure Storage environment. 
  5. To confirm the same, click on the 'DsiplayBlobs' button. You should get a similar display as follows:

    21.gif

Now try to browse the URL, we see the uploaded document as follows:

22.gif

Please note that the URIs for the blob is of the format:

https://<Account>.blob.core.windows.net/<Container>/<BlobName>

With this, we have successfully uploaded the blobs to the Windows Azure Storage environment. 

I hope the article gave a very basic understanding of how to work with Windows Azure Blob Storage. 

Happy programming.