1.上傳VHD檔案到Azure Blob Storage
VHDUpload程式的原始碼在Windows Azure Training Kit目錄Labs\ExploringWindowsAzureStorageVS2010\Source\Assets\VHDUpload,先編譯成VHDUpload.exe。然後上傳
Uploads a virtual hard disk (VHD) file to Windows Azure page blob service.
usage: VHDUPLOAD vhdFilePath blobUri accountName accountKey
vhdFilePath - path to virtual hard disk (VHD) file
blobUri - destination page blob relative URI (i.e. container/blobname)
accountName - storage account name (use devstorage for storage emulator)
accountKey - storage account primary key (omit for storage smulator)
VHDUpload上傳過程如下所示:
2. 自寫命令列程式Mount Drive
主要代碼如下(請修改成相應的儲存體帳戶和儲存體金鑰,並編譯成EXE):
class Program { const string sMainStorageName = "StorageAccountName"; const string sMainStorageKey = "StorageKey"; static void Main(string[] args) { string sVHDPathName = string.Empty; string sOperation = string.Empty; bool IfUnmount = false; if (args.Count() == 0) { Console.WriteLine("Syntax: MountAzureDrive ContainerName/Disk.VHD"); return; } try { sVHDPathName = args[0]; if (args.Length > 1) { sOperation = args[1]; if (args[1] == "/u") IfUnmount = true; } var cloudDriveBlobPath = string.Format("http://{0}.blob.core.windows.net/{1}", sMainStorageName, sVHDPathName); StorageCredentialsAccountAndKey credentials = new StorageCredentialsAccountAndKey(sMainStorageName, sMainStorageKey); // LocalResource localCache = RoleEnvironment.GetLocalResource(sMainLocalStorageName); // Char[] backSlash = { '\\' }; //String localCachePath = localCache.RootPath.TrimEnd(backSlash); //CloudDrive.InitializeCache(localCachePath, localCache.MaximumSizeInMegabytes); //string cachePath = @"C:\Resources\LocalStorage"; //for VM role //Console.WriteLine("Local Cache initialized.....{0}", cachePath); //try //{ // CloudDrive.InitializeCache(cachePath, 10); //} //catch (Exception e) //{ // Console.WriteLine(e.Message); //} if (IfUnmount) { Console.WriteLine("UnMount Drive " + cloudDriveBlobPath); CloudDrive mountDrive = new CloudDrive(new Uri(cloudDriveBlobPath), credentials); Console.WriteLine("Calling Cloud Drive UnMount API....", 0); mountDrive.Unmount(); Console.WriteLine("Finished Cloud Drive UnMount ."); } else { Console.WriteLine("Mount Drive " + cloudDriveBlobPath); CloudDrive mountDrive = new CloudDrive(new Uri(cloudDriveBlobPath), credentials); Console.WriteLine("Calling Cloud Drive Mount API....", 0); string driveLetter = mountDrive.Mount(0, DriveMountOptions.FixFileSystemErrors | DriveMountOptions.Force); Console.WriteLine("Finished Cloud Drive Mounting at Drive :" + driveLetter, 0); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
3. 在虛擬機器裡執行Mount動作
使用上述命令列:
的E盤是Mount之後的效果。(VM Role裡實驗通過)