This month, we announced the Beta Release of Windows Azure Drive (a.k.a. XDrive), a technology that allows you to mount a VHD (Virtual Hard Drive) into a Windows Azure instance and access it as if it was a locally attached NTFS drive.
There are many applications for this technology, and in this post I would like to illustrate one of them: migrating an existing on-premise application, that uses regular Windows file access APIs, into the cloud, with no impact on the existing I/O code, thanks to Windows Azure Drive. There will really be two parts in this post:
This is the first part, where the most interesting part has actually been contributed by my colleague Stéphane Crozatier from the ISV DPE Team here in France, who programmed the Page Blob Upload Tool that is the key to making your data available as an XDrive in the cloud. Thanks Stéphane!
First, let’s have a look at my application. It is a very simple Windows application that allows you to browse a directory of PDF files using thumbnails. The idea is to port this application to the cloud, so that you can browse the PDF files, hosted in Windows Azure, from a Web browser.
This application has two important characteristics, beyond its ugliness, in our case:
Of course, we could port our System.IO stuff to use Windows Azure Blobs instead; in my case, it would be quite easy, because the code is very simple! But this may not be the case of your existing applications. Furthermore, the case of the Ghostscript DLL is more complicated: I really don’t want to port this library, written in C, to Windows Azure Storage! It will be much simpler for me to just simulate a disk drive using XDrive, and I can keep my DLL as it is. Besides, if you have existing third-party closed source libraries, you may not have a choice.
I could start with an empty XDrive, creating it directly from my Azure application, and there are some examples of this in the Windows Azure Drive White Paper we have published. But my problem is different: my existing data (my directories of PDF files) and my application are linked: they work together, and I want to migrate them both into the cloud.
So, I am going to create a local VHD, where I will create the directory structure I need and copy my data, and I will upload this VHD to Windows Azure as a Page Blob, so it can be later mounted is an XDrive.
Creating a new VHD and mounting it is easy in Windows 7. If you want to use the VHD as an XDrive, you need to pay attention to the following:
Here are the steps using the Windows 7 Disk Management Tool.
Just type “format” in the start menu and select “Create and format hard disk partitions”:
Select “Create VHD” from the “Action” menu:
Specify the path, size and format:
Click OK, and the VHD will be created and attached automatically:
Now you need to initialize it, like any other disk; right-click on the disk an select “Initialize Disk”:
Select “MBR” (the default) and click OK:
Now, right-click on the partition and select “New Simple Volume…”:
Just follow the defaults in the wizard that follows. You will assign a drive letter to the new drive, then format it as NTFS. Give it a volume name if you want and make sure to select “Perform a quick format”.
You now have a brand new Virtual Hard Drive, attached locally like a regular hard drive:
I will now copy my data into this virtual hard drive; I will create three directories for the various PDF files I want to browse, and copy a number of PDF files in each directory:
Once I am done, I can detach the VHD from the Disk Management tool, and my VHD is ready to be uploaded:
Now, the really fun part begins! All we need to do is upload the VHD in Windows Azure Storage as a Blob, right? So why not use any of the nice graphical tools at our disposal, like the Azure Storage Explorer from CodePlex, Cerebrata Cloud Storage Studio, or CloudBerry Explorer for Azure Blob Storage? Easy: the VHD must be uploaded as a Page Blob, and none of the graphical tools can handle Page Blob at this time! So, we need to build our own upload tool.
Here I must thank my colleague Stéphane Crozatier from the ISV DPE Team here in France, who did all the hard work of figuring out how to do this. All the code is his!
Basically, the first thing we need to do is to create a Page Blob using the StorageClient API from the Windows Azure SDK:
CloudBlobClient blobStorage = account.CreateCloudBlobClient(); CloudPageBlob blob = blobStorage.GetPageBlobReference(blobUri); blob.Properties.ContentType = "binary/octet-stream"; blob.Create(blobSize);
Then, we need to upload the whole file page by page, using the Put Page operation, which is surfaced as the CloudPageBlob.WritePages method in the Storage Client API. Because WritePages requires a Stream as input, we will use a MemoryStream to hold the pages of data we read from the file.
int pageSize = 1024 * 1024; int numPages = (int)(file.Length / pageSize); byte[] page = new byte[pageSize]; using (MemoryStream stmPage = new MemoryStream(page)) { // process all pages until last for (int i = 0; i < numPages; i++) { // write page to storage file.Read(page, 0, pageSize); stmPage.Seek(0, SeekOrigin.Begin); blob.WritePages(stmPage, i * pageSize); } // process last page if (blobSize > numPages * pageSize) { int remain = (int)(blobSize - (numPages * pageSize)); page = new byte[remain]; // allocate a clean buffer, padding with zero using (MemoryStream stmLast = new MemoryStream(page)) { // write page to storage file.Read(page, 0, remain); stmLast.Seek(0, SeekOrigin.Begin); blob.WritePages(stmLast, numPages * pageSize); } } }
Here is the complete version of Stéphane’s Page Blob Upload tool:
It is a Visual Studio 2010 solution, ready to use. Here is how to use it:
To be continued…