I wrote a post a couple of weeks ago about getting started with the new Windows Azure Tools that are available for Eclipse, but I glossed over some of the Windows Azure features that you can configure with the tools. In the next few weeks, I hope to drill into some of those features. Today, I’ll start by taking a look at Windows Azure Drives. Specifically, I’ll explain how to programmatically determine the driver letter for a Windows Azure Drive. (I’ll assume you’ve read the getting started post I wrote a couple of weeks ago.)
What are Windows Azure Drives?
Simply put, a Windows Azure drive acts as a local NTFS volume that is mounted on the server’s file system and that is accessible to code running in a role. The data written to a Windows Azure drive is stored in a page blob defined within the Windows Azure Blob service, and cached on the local file system. Because data written to the drive is stored in a page blob, the data is maintained even if the role instance is recycled. For this reason, a Windows Azure drive can be used to run an application that must maintain state. (What's a "page" blob? Understanding Block Blobs and Page Blobs)
How to Determine a Drive Letter?
To figure out how I could use Windows Azure Drives, I created a project in Eclipse and specified two drives for my web role:
When I wrote some basic code to read and write from these drives, I made the mistake of assuming that the drive Label was the same as the drive letter (it is not). As I investigated further, I found that there is no way to know the letter for a drive during application development. The drive letter is determined when an application is deployed, which makes reading from and writing to these drives a bit more complicated than usual. Fortunately, programmatically determining the drive letter is not that difficult.
Drive information is stored in the X_Drives environment variable in this format, Z=[b:\,w], where Z is the drive label, b:\ is the drive letter, and w is the drive access (write or read). Multiple drives are separated by a semicolon. If drives have been successfully mounted, you can see the drive information displayed when you call phpinfo():
This makes it relatively easy to figure out what drive letter is associated with which drive label. First we get the X_Drives environment variable, then explode the string on semicolons:
$X_Drives = getenv("X_DRIVES");
if ( is_null($X_Drives) || !is_string($X_Drives) || empty($X_Drives))
printf("<h2>X_DRIVES Not Defined</h2>");
$XDrives = explode(";", $X_Drives);
Now it is simply a matter of looping through each element of the $XDrives variable and teasing apart the drive letter from the drive label and the drive access description. The following code does that. It also demonstrates that these drives can be written to and read from just as you’d expect:
foreach( $XDrives as $XDrive )
$XDrive = explode("=", $XDrive);
$XDriveLabel = $XDrive;
$XDriveAttribs = $XDrive;
$XDriveAttribs = trim($XDriveAttribs, "");
$XDriveAttribs = explode(",", $XDriveAttribs);
$XDriveLetter = $XDriveAttribs;
$XDriveAccess = $XDriveAttribs;
if($XDriveLabel == 'Z')
$file = $XDriveLetter."Test.txt";
$handle = fopen($file, 'x+');
$data = "Brian\n";
$data = "Swan\n";
echo "Data Written<br/>";
$f = $XDriveLetter."Test.txt";
$h = fopen($f,'r');
$c = fread($h, filesize($f));
That’s it for now. Expect to see more posts about the Windows Azure features that are surfaced in the new Eclipse tools.