How To Work With CloudDrive - Your Azure Hard Drive In Azure (831,7 kB)


I often wonder why Cloud Drive is not as popular as other storage mechanisms, although it has so many nifty uses. The need out of which CloudDrive was born was migration of Windows applications to Azure while still using File APIs for R\W data. But as always imagination is the limit.

Today I would build a "Hello World" CloudDrive application with minimum LOC and with a to-the-point activity. Through a small activity we would be creating a CloudDrive and creating a VHD from the files we collect in emulator. I would also give you some tips on how not to get your hands burned if you are going to use this feature anytime soon. Source code of the application is attached with the article.


1. Creating a CloudDrive


  1. Create a new Cloud Project with a WebRole (I have used MVC template but you can use the template as well).
  1. Add a button to your page\view and generate event handler for the button. Inside the event handler code, create a cloud drive and load it with files. Follow the (liberally added) comments:
////Account where we are going to back up our drive. In production this should be WAZ storage account.
var account = CloudStorageAccount.DevelopmentStorageAccount;

////Blob client to give us access to various blob services.
var blobClient = account.CreateCloudBlobClient();

////Container named drives where our VHD is going to stay.
CloudBlobContainer container = new CloudBlobContainer("drives", blobClient);

////We need a page blob since VHDs are page blobs.
CloudPageBlob pageBlob = container.GetPageBlobReference("TestDrive.vhd");

////Delete the page blob if it already exists.

////Create a 20MB page blob to accommodate our VHD.
pageBlob.Create(20 * 1024 * 1024);

////Un-mount any previously mounted drive.
foreach (var drive in CloudDrive.GetMountedDrives())
    var mountedDrive = account.CreateCloudDrive(drive.Value.PathAndQuery);

////Create the Windows Azure drive and its associated page blob
CloudDrive myDrive = account.CreateCloudDrive(pageBlob.Uri.AbsoluteUri);

////Create the CloudDrive if not already present.

////Mount the drive and initialize the application with the path to the date store on the Azure drive
var drivePath = myDrive.Mount(0, DriveMountOptions.None);

////Do some I\O operations with File APIs. Let's create a folder and add some files.
Directory.CreateDirectory(Path.Combine(drivePath, "Data").ToString());
var fStream = System.IO.File.Create(Path.Combine(drivePath, "Data","First.txt").ToString());
System.IO.File.WriteAllText(Path.Combine(drivePath, "Data","First.txt").ToString(), "First File Data");
var sStream = System.IO.File.Create(Path.Combine(drivePath, "Data","Second.txt").ToString());
System.IO.File.WriteAllText(Path.Combine(drivePath, "Data","Second.txt").ToString(), "Second File Data");

////Let's now output to page what we have done till now by saving it in our model.
data = new DriveData();
data.LocalDrivePath = myDrive.LocalPath;

////Use File API to read data from drive.
string localPath = myDrive.LocalPath;
if (Directory.Exists(localPath))
////Get the folder that we created in VHD. Read all data.
    var folder = Directory.GetDirectories(localPath).First();
    data.FolderName = folder;
    var files = Directory.GetFiles(folder);
    data.File1Name = files[0];
    data.File1Content = System.IO.File.ReadAllText(files[0]);
    data.File2Name = files[1];
    data.File2Content = System.IO.File.ReadAllText(files[1]);

////Un-mount Drive
return View(data);




  1. To see what is in your drive open your storage emulator and navigate to File —> Open Azure Drive —> Navigate through directory.




  1. In local emulator you cannot mount a drive without first creating it. In case you want to do so, you need to copy paste files into storage emulator location where it reads data from.
  2. If you are in cloud and want to mount a VHD in a page blob without first creating it, then directly call theMount() method with the page blob URI.
  3. You can cache content of drive to your local storage by keeping a Snapshot of drive and keep it persistent across role recycles to save on storage costs and increase speed of operations.
  4. Do not forget to unmount drive once you are done using it as it saves space.

Now that you are done using the drive, you can package the emulator folder as a VHD and upload it to a page blob. Thus, you can refer to VHD without first creating one unlike as we did in the sample. To upload the VHD to page blob you could either code one for yourself or use a GUI tool such as Cerebrata cloud storage studio. The next step shows how you can create VHD from a given folder.

2. Creating a VHD (The longest stage).



  1. On Start Menu, type Disk Management and select the Disk Manager.
  2. Select Action —> Create VHD and save the VHD to a location and give it a name and a size (>= 16MB).
  3. Initialize the VHD you just created by right clicking on Disk1 —> Initialize —> OK



  4. Format your VHD by right clicking on the new partition —> New Simple Volume —> Next —> Next (Let the volume size remain as it is) —> Assign Drive a Letter (V) —> Format as NTFS (ONLY) —> Finish.





  1. Add the folder "Data" from emulator location to this new drive.
  2. You have your VHD ready, but to copy it you need to detach it. Go back to Disk Management and right click on Disk2 icon —> Detach —> OK(Delete VHD checkbox should be unchecked, obviously)


3. Upload VHD to PageBlob

  1. Use any of GUI tools, PWS scripts, Custom code to upload this VHD to your storage account and use it in your application.

Topic: How To Work With CloudDrive - Your Azure Hard Drive In Azure

No comments found.

New comment