如何使用 CloudDrive - Azure 中的您的 Azure 硬盘






4.96/5 (11投票s)
如何使用 CloudDrive,您在 Azure 中的 azure 硬盘
我经常想知道为什么 Cloud Drive 并没有像其他存储机制那样受欢迎,尽管它有很多巧妙的用途。CloudDrive 诞生的需求是将 Windows 应用程序迁移到 Azure,同时仍然使用文件 API 进行 R\W 数据操作。但和往常一样,想象力是无限的。
今天,我将使用最少的代码行构建一个“Hello World” CloudDrive 应用程序,并进行一个重点突出的活动。通过一个小活动,我们将创建一个 CloudDrive 并从我们在模拟器中收集的文件创建 VHD。我还会给您一些关于如果您很快要使用此功能的提示,以避免烫手。应用程序的源代码附在文章中。
步骤
1. 创建 CloudDrive
- 使用 WebRole 创建一个新的云项目(我使用了 MVC 模板,但您也可以使用 ASP.net 模板)。
- 在您的页面/视图中添加一个按钮,并为该按钮生成事件处理程序。在事件处理程序代码中,创建一个云驱动器并使用文件加载它。按照(大量添加的)注释
////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);
输出
- 要查看驱动器中的内容,请打开您的存储模拟器并导航到文件 —> 打开 Azure 驱动器 —> 浏览目录。
注释
- 在本地模拟器中,您必须先创建驱动器才能挂载它。如果您想这样做,您需要将文件复制粘贴到存储模拟器位置,它从中读取数据。
- 如果您在云中并想在未先创建页 Blob 的情况下挂载 VHD,则直接使用页 Blob URI 调用
Mount()
方法。 - 您可以通过为驱动器保留快照并使其在角色回收过程中保持持久性来缓存驱动器的内容到您的本地存储,以节省存储成本并提高操作速度。
- 完成使用驱动器后,请不要忘记卸载驱动器,因为它会节省空间。
现在您已经完成了驱动器的使用,您可以将模拟器文件夹打包为 VHD 并将其上传到页 Blob。因此,您可以引用 VHD 而无需像我们在示例中所做的那样先创建一个。要将 VHD 上传到页 Blob,您可以自己编写代码,或者使用 GUI 工具,例如 Cerebrata 云存储工作室。下一步展示了如何从给定文件夹创建 VHD。
2. 创建 VHD(最长的阶段)。
- 在“开始”菜单上,键入“磁盘管理”并选择“磁盘管理器”。
- 选择“操作” - > 创建 VHD 并将 VHD 保存到某个位置,并为其命名和设置大小(>= 16MB)。
- 通过右键单击 Disk1 -> 初始化 -> 确定来初始化您刚刚创建的 VHD。
- 通过右键单击新分区 -> 新建简单卷 -> 下一步 -> 下一步(让卷大小保持不变)-> 分配驱动器号 (V) -> 格式化为 NTFS (仅限) -> 完成 来格式化您的 VHD。
- 将模拟器位置中的“数据”文件夹添加到此新驱动器。
- 您的 VHD 已准备就绪,但要复制它,您需要将其分离。返回到“磁盘管理”并右键单击 Disk2 图标 -> 分离 -> 确定(应取消选中“删除 VHD”复选框,显然)
3. 将 VHD 上传到 PageBlob
- 使用任何 GUI 工具、PWS 脚本、自定义代码将此 VHD 上传到您的存储帐户并在您的应用程序中使用它。