Click here to Skip to main content
Click here to Skip to main content

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

, , 12 Feb 2012 Ms-PL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Authors

MeraMBA.com
Software Developer HCL Technologies Limited
India India
Rahul has worked on several technologies in Microsoft's technology stack. He has been in field of software developemnt for two years. He started with ASP.net and graduated to Windows application develeopment. His technology experience includes C#, LINQ, T-SQL,T-SQL, XML, PL\SQL, C, C++, VB, WebServices..
 
Apart from all these he is working on some of the most recent technology offerings such as Windows Azure, SQL Azure, SQL Azure Sync framework, MS Dallas, Powershell etc.
 
His goal of knowledge is to possess knowledge that is sky high and earth deep.

maq_rohit a.k.a asthanarht
Technical Lead
United States United States
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.
 

1. 20 Apr 2014: Best Mobile Article of March 2014 - First Prize
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 4 Pinmemberhebsiboy29-Apr-13 2:14 
GeneralMy vote of 5 PinmvpKanasz Robert28-Sep-12 5:36 
GeneralRe: My vote of 5 Pinmembermaq_rohit28-Sep-12 9:17 
QuestionI need more information... PinmemberDewey12-Feb-12 12:47 
AnswerRe: I need more information... Pinmembermaq_rohit12-Feb-12 17:52 
GeneralRe: I need more information... PinmemberDewey13-Feb-12 8:09 
GeneralRe: I need more information... Pinmembermaq_rohit13-Feb-12 17:40 
GeneralRe: I need more information... PinmemberDewey13-Feb-12 22:55 
GeneralMy vote of 5 PinmemberNicolas Dorier12-Feb-12 11:42 
GeneralRe: My vote of 5 Pinmembermaq_rohit12-Feb-12 17:46 

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.

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 12 Feb 2012
Article Copyright 2012 by Member 5513529, maq_rohit a.k.a asthanarht
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid