Windows Azure - Local Storage Feature


In this article, we are trying to experiment the local storage feature of Windows Azure. From the previous article we found that local storage is one of the primitive storage mechanisms in Windows Azure.

The core features of Local Storage are:

  • Temporary Storage
  • Feature provided by the Operating System
  • Similar to Hard Drive
  • Exposed as logical file system
  • .Net Directory/File IO Framework used to access it
  • Can be used for storing Cache items

Creating a Local Storage

Here we are creating a local resource through the step by step activities.

Step 1: Create new Web Role project

You will be familiar with web role creation and you can repeat the same. After the solution is created, the Solution Explorer will be looking like below:

AzrSrt1.gif

Step 2: Open the local storage pane

Now double click on the WebRole1 item shown above to open the project configuration, Form there click the Local Storage pane item.

AzrSrt2.gif

Step 3: Add a new Local Storage item

Add a new local storage items by clicking the Add Local Storage button. Rename it to TestStorage Change the Size to 10 MB as shown in the figure and save the configuration.

AzrSrt3.gif

The above change will modify the configuration file of the application.

Step 4: Show Information about the Local Resource

Now we can place a label on the page, rename it to InfoLabel. Then on the page load event we can access the local resource and show the properties it has.

AzrSrt4.gif

The RootPath property gives the current path of the resource in machine.
The MaximumSizeInMegabytes property returns the size allocated to it.

On executing the application we can see the following output.

AzrSrt5.gif

Step 5: Start using the folder

Now we can use the above folder for our storage purpose. We can create folder, files inside it.

Create a new button on the page and name it as CreateFolderAndFileButton. On click of the button place the code to create a directory and file.

protected void CreateFolderAndFileButton_Click(object sender, EventArgs e)
{
  
LocalResource resource = RoleEnvironment.GetLocalResource("TestStorage");
  
Directory.CreateDirectory(resource.RootPath + "\\NewFolder");
  
File.Create(resource.RootPath + "\\NewFile.txt");

  ShowContents(resource.RootPath);
}

private void ShowContents(string path)
{
    InfoLabel.Text =
"Directories inside it: <br>";
   
foreach (string dir in Directory.GetDirectories(path))
    {
        InfoLabel.Text += dir +
"<br>";
    }

    InfoLabel.Text += "<br>Files inside it";
   
foreach (string file in Directory.GetFiles(path))
    {
        InfoLabel.Text += file +
"<br>";
    }
}


Execute the application and on clicking the button we can see the following output.

Azure.gif

Summary

In this article we have found how to use the local storage feature of Windows Azure and creating folder and file inside it.