« Windows Azure CloudDrive - Creating a VHD and Uploading it to Azure Storage (Part 1 of 2, 30 minutes) | Main

Windows Azure CloudDrive - Creating a Web Role that mounts the CloudDrive from Azure Storage and reads text files (Part 2 of 2, 22 minutes)

This is part 2 of 2 screencasts I recorded that helps a Microsoft .NET developer use the new Windows Azure CloudDrive feature -

  • For Part 1, Creating a VHD and Uploading it to Azure Storage (Part 1 of 2, 30 minutes) – click here.
  • For the Summary entry on blog.ehuna.org, click here.

Once the VHD file has been uploaded to Azure Storage you can use the Azure SDK to cache it in your web role instance and mount it - you then have a NTFS hard-drive in the cloud where you can use the standard System.IO classes to read and write files. 

For more articles, screencasts and tips check out www.ehuna.org - follow me on twitter.com/ehuna

See below for details on what's covered in the video, and download the Visual Studio 2010 RC VB.NET sample projects used in the videos here:

  http://blog.ehuna.org/files/Visual-Studio-Solution-AzureSpeed1.zip (134 KB)

Windows Azure CloudDrive - Creating a Web Role that mounts the CloudDrive from Azure Storage and reads text files (Part 2 of 2, 22 minutes)

Here’s what is covered in the video:

  • 00:00 - Introduction / Overview (see notes here).
    • Mounting a VHD in the Azure fabric, reading text files and spitting them out
  • 00:50 - New Visual Studio 2010 RC cloud project with one web role
    • MyVHDWebRole - it's a Windows Azure web role but is like a console app - runs and spits out results.
  • 01:40 - Settings in ServiceDefinition.csdef with LocalResource "on-disk" cache
<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="MyVHDWebRole" 
xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition"> <WebRole name="MyVHD_WebRole"> <InputEndpoints> <InputEndpoint name="HttpIn" protocol="http" port="90" /> </InputEndpoints> <ConfigurationSettings> <Setting name="DiagnosticsConnectionString" /> <Setting name="AzureStorageAccount" /> <Setting name="AzureStorageKey" /> <Setting name="BlobStorageEndpoint" /> <Setting name="QueueStorageEndpoint" /> <Setting name="TableStorageEndpoint" /> <Setting name="BlobContainePCLDrives" /> <Setting name="VHDDriveName" /> </ConfigurationSettings> <LocalResources> <LocalStorage name="MyCloudDriveCache" cleanOnRoleRecycle="true" sizeInMB="30" /> </LocalResources> </WebRole> </ServiceDefinition>
  • 02:40 - Settings in ServiceConfiguration.cscfg with osVersion set to host OS 1.1 
<?xml version="1.0"?>
  <Role name="MyVHD_WebRole">
    <Instances count="1" />
      <Setting name="DiagnosticsConnectionString" value="UseDevelopmentStorage=true" />
            <Setting name="AzureStorageAccount" value="devstoreaccount1" />
            <Setting name="AzureStorageKey" value="Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==" />
            <Setting name="BlobStorageEndpoint" value="" />
            <Setting name="QueueStorageEndpoint" value="" />
            <Setting name="TableStorageEndpoint" value="" />
            <Setting name="BlobContainePCLDrives" value="vhddrives" />
            <Setting name="VHDDriveName" value="myvhddrive.vhd" />

Host 1.1 does not yet support .NET Framework 4.0 yet (in the cloud)
This sample uses the local development storage - not Azure Storage in the cloud

  • 04:55 - Make sure your Page Blob snapshot and CloudDrive instance is public - so you can unmount and delete when the web role shuts down.
    • Specially in developmnent, where for a example a 1 GB VHD would be copied to a 1 GB snapshot
    • In the cloud, this is not true, a snapshot is very small - one page.
  • 06:10 - Tip to read templates if running outside of local development fabric (in IIS for faster development)
  • 06:30 - ASPX file - a button and a label - the label contains the results.
    • If an exception occurs, it's easy to see it and follow up in the forums.
  • 07:45 - We only enable the "Mount VHD" button if we are running in the fabric.
  • 08:30 - Function BP() - just used to build up a string that is then displayed through the page's label.
  • 09:20 - We dig into the cache code -
oLocalResource = RoleEnvironment.GetLocalResource("MyCloudDriveCache")
sCacheFolder = AddTrailingSlash(oLocalResource.RootPath) + "cache"
If Not (IO.Directory.Exists(sCacheFolder)) Then IO.Directory.CreateDirectory(sCacheFolder)

Make sure your cache folder does not have a trailing backslash "\" - the Azure SDK 1.1 has a bug that will throw an exception if it does.

  • 10:30 - Telling the CloudDrive API to cache the VHD page blob -
CloudDrive.InitializeCache(sCacheFolder, oLocalResource.MaximumSizeInMegabytes)
  • 11:00 - Do the Windows Azure dance to get a reference to the VHD stored in Azure Blob Storage
uriBlobStorageEndpoint = New Uri(sBlobStorageEndpoint)
uriQueueStorageEndpoint = New Uri(sQueueStorageEndpoint)
uriTableStorageEndpoint = New Uri(sTableStorageEndpoint)

oStorageCredentialsAccountAndKey = New StorageCredentialsAccountAndKey(sAzureStorageAccount, sAzureStorageKey)
oCloudStorageAccount = New CloudStorageAccount(oStorageCredentialsAccountAndKey, _ 
uriBlobStorageEndpoint, uriQueueStorageEndpoint, uriTableStorageEndpoint) oCloudBlobClient = oCloudStorageAccount.CreateCloudBlobClient() oCloudBlobContainer = oCloudBlobClient.GetContainerReference(sBlobContainePCLDrives) oCloudBlobContainer.CreateIfNotExist()
  • 11:35 - Creating an instance of the CloudDrive class -
' Existing Drive
oCloudPageBlob = oCloudBlobContainer.GetPageBlobReference(sVHDDriveName)
sVHDUri = oCloudPageBlob.Uri.ToString
  • 12:00 - Creating a snapshot of the page blob and the read-only CloudDrive -
oCloudDrive = oCloudStorageAccount.CreateCloudDrive(sVHDUri)
uriVHDSnapshot = oCloudDrive.Snapshot
sVHDSnapshotUri = uriVHDSnapshot.ToString 

oCloudDriveReadOnly = New CloudDrive(uriVHDSnapshot, oCloudStorageAccount.Credentials)
  • 12:45 - Mount the VHD drive in read-only mode -
sLocalPath = oCloudDriveReadOnly.Mount(oLocalResource.MaximumSizeInMegabytes, _ 

Make sure you handle exceptions here!

  • 13:45 - Use the regular System.IO classes to enumerate files in a folder in the VHD file -
If IO.Directory.Exists(sLocalPath) Then
  sFiles = IO.Directory.GetFiles(sDataFolder, "*.TXT")
End If

Mentioned Visual Studio 2010 RC Code Snippets in VB.NET (for the C# developers, here’s a tutorial on code snippets).  Spent a bit of time to read the text content from each file and displayed it in the results.

  • 16:50 - Running the web role in the Windows Azure local development fabric
    • Showing the deployment in the service details
    • Showing the default.aspx page in the browser.
    • Clicked on 'Mount VHD' -
      • Showed the snapshot directory in C:\Users\emmanuel_huna\AppData\Local\dftmp\wadd\devstoreaccount1\vhddrives\myvhddrive.vhd!2010-03-12T21_52_56.6300000Z
      • Got exception on reading the contents since the filename contained the path already; fixed it and ran it again - it worked!
  • 19:49 - When your web role shuts down: Un-mount your read-only CloudDrive and make sure you delete the Azure Page Blob snapshots.
  • 21:40 - Conclusion.

Good times!

| More



This page contains a single entry from the blog posted on March 12, 2010 11:05 PM.

The previous post in this blog was Windows Azure CloudDrive - Creating a VHD and Uploading it to Azure Storage (Part 1 of 2, 30 minutes).

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.35