Click here to Skip to main content
Licence Ms-PL
First Posted 8 Feb 2012
Views 10,994
Downloads 150
Bookmarked 26 times

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

By | 12 Feb 2012 | Article
How to work with CloudDrive, Your azure hard drive in Azure

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.

Steps

1. Creating a CloudDrive

image001.png

  1. Create a new Cloud Project with a WebRole (I have used MVC template but you can use the ASP.net 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);
container.CreateIfNotExist();

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

////Delete the page blob if it already exists.
pageBlob.DeleteIfExists();




////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);
    mountedDrive.Unmount();
}

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

////Create the CloudDrive if not already present.
myDrive.CreateIfNotExist(25);

////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());
fStream.Close();
fStream.Dispose();
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());
sStream.Close();
sStream.Dispose();
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
myDrive.Unmount();
return View(data);

Output

image002.png

image003.png

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

image004.png

image005.png

Notes

  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 the Mount() 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. image006.png

  3. Select Action —> Create VHD and save the VHD to a location and give it a name and a size (>= 16MB).
  4. image007.png

  5. Initialize the VHD you just created by right clicking on Disk1 —> Initialize —> OK

    image008.png

    image009.png

  6. 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.

image010.png

image011.png

image012.png

  1. Add the folder "Data" from emulator location to this new drive.
  2. image013.png

  3. 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)

image014.png

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.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

About the Author

maq_rohit

Technical Lead

India India

Member

Follow on Twitter Follow on Twitter
Rohit is a Lead Engineer from NCR. He started Embedded Programing in his college days and now he is a Software Developer by Profession. Mainly he interested in cutting edge technology offer by Microsoft (i.e Azure,MVC). He Loves coding and his passion is always been towards Microsoft Technologies. Apart from coding his other hobbies include reading books and hang out with friends is his most favorite past time hobby.
 
Technical Skills:
C#,Ado.Net,Asp.Net,Sql Server,JavaScript,XML,Web services.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionI need more information... PinmemberDewey12:47 12 Feb '12  
AnswerRe: I need more information... Pinmembermaq_rohit17:52 12 Feb '12  
GeneralRe: I need more information... PinmemberDewey8:09 13 Feb '12  
GeneralRe: I need more information... Pinmembermaq_rohit17:40 13 Feb '12  
GeneralRe: I need more information... PinmemberDewey22:55 13 Feb '12  
GeneralMy vote of 5 PinmemberNicolas Dorier11:42 12 Feb '12  
GeneralRe: My vote of 5 Pinmembermaq_rohit17:46 12 Feb '12  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 12 Feb 2012
Article Copyright 2012 by maq_rohit
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid