Configure work with Azure Blob Storage in your solution

Windows Azure Blob storage is a service for storing large amounts of unstructured data that can be accessed from anywhere in the world via HTTP or HTTPS. A single blob can be hundreds of gigabytes in size, and a single storage account can contain up to 100TB of blobs. Common uses of Blob storage include:

 

  •    Serving images or documents directly to a browser
  •    Storing files for distributed access
  •    Streaming video and audio
  •    Performing secure backup and disaster recovery
  •    Storing data for analysis by an on-premises or Windows Azure-hosted service

 

Windows Azure Blob has next hierarchy:

Azure Blob hierarchy

Blob hierarchy

  

The previous topic was about how to add new Web Role to your project. In this topic I will show you how to configure Blob storage and how to use it. I will use the same solution as it was in the previous topic.

Here is two way how to work with Cloud Storage:

 

  •     You can install Cloud Storage Studio on your PC.
  •     You can use Visual Studio instead of installed Cloud Storage Studio. In Visual Studio use Server Explorer tab. In tab Server Explorer go to Windows Azure Storage.

 

I will work with Cloud Storage Studio.

But first we need to create a storage account. For this we need to go to Manage Windows Azure site by link. Then go to tab storage and press button “+ New”:

Tab Storage in Manage Windows Azure site

Tab Storage in Manage Windows Azure site

 

After clicking button “+ New” a new wizard will occur. Enter name of your new storage account and select a location. In my case name is “mvcapplicationstorage” and the location is “West Europe”:

Create new storage account in Azure

Create new storage account

 

After this you will see this storage account in the list:

mvcapplicationstorage storage account in Azure

“mvcapplicationstorage” storage account

 

Now we need get credentials of this storage account. For this left click on the name. You will bring to the next page. Click on bottom button “Manage Access Keys”. You will see next page:

Manage Access Keys page in Azure

Manage Access Keys page

 

Copy this Account Name and Primary Access Key. We will need this after one step.

Now we should go to Cloud Storage Studio and create new Connection Group.  Go to “ File – New Connection Group ”. A new window will occur. Enter Group Name and press Ok:

Create New Connection Group in Cloud Storage Studio

Create New Connection Group in Cloud Storage Studio

 

As a result, we receive new connection group:

Create connection group in Cloud Storage

Create connection group

 

Now we can add created storage account in Manage Windows Azure site to our connection group. For this we should do next. Click on “File – New Storage Account Connection”. A new window will occur. As account name and key we should enter copied account name and account key in the previous step. AS connection group – select created “ConnectionGroup” and press Ok:

Add new Storage Account Connection in Azure

Add new Storage Account Connection

 

As a result, we add our created Storage Account to our created Connection Group. We can see this in the tree:

New Storage Account in Azure

New Storage Account

 

Now we can add now Blob Container to our storage account. For this – right click on “Blob Containers” and select “New Blob container”. (Note: Folder not correct word for this Blob stuffs. Blob do not have a file system. But I will not speak about names in Blob in this topic). A new window will occur. Enter your Blob Container Name in this window:

Create new Azure Blob Container

Create new Blob Container

 

We will see this container in the tree of out storage account. Then we can double click on this container name to open it. Now we should add new Folder. For this right click on white space. A context menu will occur. Select “New Folder” button. A new window will occur. Enter a name of a new folder. Let's say “Images”:

Add new folder “Images” to blob container “Documents” in Azure

Add new folder “Images” to blob container “Documents”

 

After thick double click on folder “Images” to open it.

In the next step we can upload some files to this blob container from our PC. For this we can press button “Upload files” or simply drug and drop files and press button “Start upload”. We will see the result in next image.

Uploaded files to Blob “Documents” in Azure

Uploaded files to Blob “Documents”

 

So we have configured new Connection Group, new Storage account, new Blob container and upload some files in this container. In last step I will show you how to work with this Blob container using our solution that was created in the previous topic.

For this we should open our solution. And in the first step add some configuration to our project.

In Web.config of our MvcApplicationWebRole we should add a connection string for our storage account. Also, we should add Key in Web.config with our Blob container name:

Configure connection string to Storage Account add name of Blob Container in Web.config

Configure connection string to Storage Account add name of Blob Container in Web.config

 

After these settings we need to write the code – to get all files from the folder. Also, we will add new Controller “BlobDocumentsController.cs” to our solution and will return all files to View “AllDocuments.cshtml”. I will not show you how to create Controller and View in this topic. Just will show you the code that returns all files from some folder as the list:

Code that returns all files from folder

Code that returns all files from folder

 

In next screen we can see code for Controller:

Controller code in Azure

Controller code

 

And finally View code:

View code in Azure

View code

 

I will not show you’re the code for downloading the attachment in this topic.

And in final step we can run our project and see result:

List of documents in Azure

List of documents

 

Best Regards. Andrew Tsopych CTO of Diceus

 

Loading