Windows Azure入门教学系列 (八):使用Windows Azure Drive

简介: 我们知道,由于云端的特殊性,通常情况下,对文件系统的读写建议使用Blob Storage来代替。这就产生了一个问题:对于一个已经写好的本地应用程序,其中使用了NTFS API对本地文件系统读写的代码是否需要进行完全重写以便迁移到Windows Azure平台上呢?答案是否定的。

我们知道,由于云端的特殊性,通常情况下,对文件系统的读写建议使用Blob Storage来代替。这就产生了一个问题:对于一个已经写好的本地应用程序,其中使用了NTFS API对本地文件系统读写的代码是否需要进行完全重写以便迁移到Windows Azure平台上呢?答案是否定的。Windows Azure平台提供了Drive的功能。

在1.1版本的SDK中提供了CloudDrive类,能够将本地NTFS文件系统Mount到Blob Storage上。我们只要添加一小段代码来表明我们希望Mount Drive到Blob Storage上就能重用大部分已有的对本地文件系统访问的代码。这样,我们已有的程序能够无缝地迁移到Windows Azure上而不需要做大的改动。

在开始本教学之前,请确保你从Windows Azure 平台下载下载并安装了最新的Windows Azure开发工具。本教学使用Visual Studio 2010作为开发工具。

步骤一:创建解决方案和项目

由于我们要在本地模拟环境下测试Windows Azure Drive,首先,请确保Storage Emulator已经启动。我们可以找到管理器的进程手动启动或者让Visual Studio 2010帮助我们启动他。

右击工具栏中Windows Azure模拟器的图标,选择”Show Storage Emulator UI”。弹出如下图所示的窗口:

我们要关注的是Service management中Blob所在的一行。要确保Status为Running。

确认完毕后启动Visual Studio 2010,新建一个Cloud Service项目并为之添加一个Web Role。

步骤二:添加SDK程序集引用

请在项目属性页里确认项目的Target framework的值是.NET Framework 4或.NET Framework 3.5。然后在Web Role项目中添加对C:Program FilesWindows Azure SDKv1.3refMicrosoft.WindowsAzure.CloudDrive.dll的引用。该路径为SDK默认安装路径,如果你不能在这个路径中找到Microsoft.WindowsAzure.CloudDrive.dll请从SDK安装路径中寻找。

步骤三:添加代码

删除并重新创建Default.aspx 页面,然后在Default.aspx.cs中引用命名空间:

双击代码全选
1
2
3
4
5
using Microsoft.WindowsAzure;
 
using Microsoft.WindowsAzure.StorageClient;
 
using System.IO;

然后添加下列代码:

双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public partial class Default : System.Web.UI.Page
 
{
 
     string _driveLetter = WebRole.DriveLetter;
 
  
 
     protected void Page_Load(object sender, EventArgs e)
 
     {
 
         if (!IsPostBack)
 
         {
 
             Bind();
 
         }
 
     }
 
  
 
     void Bind()
 
     {
 
         // 显示被Mount的Drive根目录下的所有文件
 
         DirectoryInfo di = new DirectoryInfo(string.Format("{0}", _driveLetter));
 
         this.GridView1.DataSource = di.GetFiles();
 
         this.GridView1.DataBind();
 
     }
 
  
 
     protected void Button1_Click(object sender, EventArgs e)
 
     {
 
         // 在被Mount的Drive根目录下保存文件
 
         if (this.FileUpload1.HasFile)
 
         {
 
             File.WriteAllBytes(string.Format("{0}{1}", _driveLetter, this.FileUpload1.FileName), this.FileUpload1.FileBytes);
 
             Bind();
 
         }
 
     }
 
}

在Default.aspx中添加下列代码:

双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
< form id = "form1" runat = "server" >
 
  
 
     < asp:FileUpload ID = "FileUpload1" runat = "server" />
 
     < asp:Button ID = "Button1" runat = "server" Text = "Upload" onclick = "Button1_Click" />
 
  
 
     < asp:GridView AllowPaging = "true" PageSize = "20" ID = "GridView1" runat = "server" >
 
     </ asp:GridView >
 
  
 
</ form >

接下来在WebRole.cs中引用命名空间:

双击代码全选
1
using Microsoft.WindowsAzure.StorageClient;

然后添加下列代码:

双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
public class WebRole : RoleEntryPoint
 
{
 
     public static string DriveLetter { get; private set; }
 
     CloudDrive myCloudDrive;
 
  
 
     public override bool OnStart()
 
     {
 
         // 当用配置文件中ConfigurationSettings时必须调用CloudStorageAccount.SetConfigurationSettingPublisher
 
         // 来说明当配置文件在发布后被更改时将采取何种操作
 
         CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
 
         {
 
               
 
             configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
 
  
 
             RoleEnvironment.Changed += (sender, arg) =>
 
             {
 
                 if(arg.Changes.OfType< RoleEnvironmentConfigurationSettingChange >()
 
                     .Any((change) => (change.ConfigurationSettingName == configName)))
 
                 {
 
  
 
                     if(!configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)))
 
                     {
 
                         RoleEnvironment.RequestRecycle();
 
                     }
 
                 }
 
             };
 
         });
 
  
 
         // For information on handling configuration changes
 
         // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
 
  
 
         return base.OnStart();
 
     }
 
  
 
     public override void  Run()
 
     {
 
         CloudStorageAccount storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
 
  
 
         LocalResource localCache = RoleEnvironment.GetLocalResource("InstanceDriveCache");
 
         CloudDrive.InitializeCache(localCache.RootPath, localCache.MaximumSizeInMegabytes);
 
  
 
         // 检查Container是否存在,不存在则创建
 
         CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
 
         blobClient.GetContainerReference("drives").CreateIfNotExist();
 
  
 
         // 创建Cloud Drive
 
         myCloudDrive = storageAccount.CreateCloudDrive(
 
             blobClient
 
             .GetContainerReference("drives")
 
             .GetPageBlobReference("mydrive.vhd")
 
             .Uri.ToString()
 
         );
 
  
 
         try
 
         {
 
             myCloudDrive.Create(64);
 
  
 
         }
 
         catch (CloudDriveException)
 
         {
 
  
 
         }
 
         DriveLetter = myCloudDrive.Mount(0, DriveMountOptions.Force);
 
  
 
         base.Run();
 
     }
 
  
 
     public override void OnStop()
 
     {
 
         myCloudDrive.Unmount();
 
  
 
         base.OnStop();
 
     }
 
}

最后,修改配置文件。在Cloud Service项目的ServiceConfiguration.cscfg中添加下列配置:

双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<? xml version = "1.0" encoding = "utf-8" ?>
 
< ServiceConfiguration serviceName = "WindowsAzureDriveDemonstration" xmlns = "http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" osFamily = "1" osVersion = "*" >
 
   < Role name = "WebRole1" >
 
     < Instances count = "1" />
 
     < ConfigurationSettings >
 
       < Setting name = "Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value = "UseDevelopmentStorage=true" />
 
       < Setting name = "DataConnectionString" value = "UseDevelopmentStorage=true" />
 
     </ ConfigurationSettings >
 
   </ Role >
 
</ ServiceConfiguration >

在ServiceDefinition.csdef中添加下列配置(注意将Sites部分的内容删除或注释掉):

双击代码全选
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<? xml version = "1.0" encoding = "utf-8" ?>
 
< ServiceDefinition name = "WindowsAzureDriveDemonstration" xmlns = "http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition" >
 
   < WebRole name = "WebRole1" >
 
     < Sites >
 
       < Site name = "Web" >
 
         < Bindings >
 
           < Binding name = "Endpoint1" endpointName = "Endpoint1" />
 
         </ Bindings >
 
       </ Site >
 
     </ Sites >
 
     < Endpoints >
 
       < InputEndpoint name = "Endpoint1" protocol = "http" port = "80" />
 
     </ Endpoints >
 
     < Imports >
 
       < Import moduleName = "Diagnostics" />
 
     </ Imports >
 
     < LocalResources >
 
         < LocalStorage name = "InstanceDriveCache" cleanOnRoleRecycle = "false" sizeInMB = "300" />
 
     </ LocalResources >
 
     < ConfigurationSettings >
 
         < Setting name = "DataConnectionString" />
 
     </ ConfigurationSettings >
 
   </ WebRole >
 
</ ServiceDefinition >

步骤四:观察并分析代码

步骤三中的代码中,Default.aspx.cs的代码跟普通的ASP.NET项目代码没什么区别。我们把盘符抽离出来以便迁移到Windows Azure上。Default.aspx.cs中的代码跟Windows Azure唯一相关的一句语句就是string driveLetter = WebRole.DriveLetter。我们如果把WebRole.DriveLetter替换为本机盘符该ASP.NET程序将能够正常运行。

后面的代码示范了如何Mount Drive到Blob Storage。此外我们使用了本地缓存来缓存尚未被传递到Blob Storage的文件。

通过上面的例子可以看到,只需额外添加一小段代码,已有的使用NTFS API的程序能够很方便地迁移到Windows Azure平台上。

步骤五:运行程序

运行程序。上传几个文件。如果一切顺利你将看到下图所示的结果。可以看到文件的DirectoryName为“a:”。这就是被Mount到Blob Storage上的Drive的盘符。

需要注意的是,本例中我们并未修改WebRole的实例数,因此只有1个WebRole(发布到云端后将只有一个虚拟机被使用)。如果有两个实例,并且要让两个实例共享一个”网络硬盘“的话,本例中代码将不能正常工作。因为对于同一个Blob同时只能有一个虚拟机Mount。一个可能的解决方案是额外维护一个Web Service来管理Mount和UnMount Drive。进一步的讨论超出了本教学的范围。有兴趣的读者可以下载并参考Windows Azure Cloud Drive白皮书获取信息。

目录
相关文章
|
4月前
|
安全 Windows
【Azure Cloud Service】在Windows系统中抓取网络包 ( 不需要另外安全抓包工具)
通常,在生产环境中,为了保证系统环境的安全和纯粹,是不建议安装其它软件或排查工具(如果可以安装,也是需要走审批流程)。 本文将介绍一种,不用安装Wireshark / tcpdump 等工具,使用Windows系统自带的 netsh trace 命令来获取网络包的步骤
117 32
|
4月前
|
C# Windows
【Azure App Service】在App Service for Windows上验证能占用的内存最大值
根据以上测验,当使用App Service内存没有达到预期的值,且应用异常日志出现OutOfMemory时,就需要检查Platform的设置是否位64bit。
78 11
|
5月前
|
存储 NoSQL MongoDB
MongoDB入门级别教程全(Windows版,保姆级教程)
一份全面的MongoDB入门级教程,包括在Windows系统上安装MongoDB、使用MongoDB Shell和Compass GUI进行数据库操作,以及MongoDB的基本数据类型和查询技巧。
547 2
MongoDB入门级别教程全(Windows版,保姆级教程)
|
7月前
|
数据库 Windows
超详细步骤解析:从零开始,手把手教你使用 Visual Studio 打造你的第一个 Windows Forms 应用程序,菜鸟也能轻松上手的编程入门指南来了!
【8月更文挑战第31天】创建你的第一个Windows Forms (WinForms) 应用程序是一个激动人心的过程,尤其适合编程新手。本指南将带你逐步完成一个简单WinForms 应用的开发。首先,在Visual Studio 中创建一个“Windows Forms App (.NET)”项目,命名为“我的第一个WinForms 应用”。接着,在空白窗体中添加一个按钮和一个标签控件,并设置按钮文本为“点击我”。然后,为按钮添加点击事件处理程序`button1_Click`,实现点击按钮后更新标签文本为“你好,你刚刚点击了按钮!”。
672 0
|
7月前
|
开发者 iOS开发 C#
Uno Platform 入门超详细指南:从零开始教你打造兼容 Web、Windows、iOS 和 Android 的跨平台应用,轻松掌握 XAML 与 C# 开发技巧,快速上手示例代码助你迈出第一步
【8月更文挑战第31天】Uno Platform 是一个基于 Microsoft .NET 的开源框架,支持使用 C# 和 XAML 构建跨平台应用,适用于 Web(WebAssembly)、Windows、Linux、macOS、iOS 和 Android。它允许开发者共享几乎全部的业务逻辑和 UI 代码,同时保持原生性能。选择 Uno Platform 可以统一开发体验,减少代码重复,降低开发成本。安装时需先配置好 Visual Studio 或 Visual Studio for Mac,并通过 NuGet 或官网下载工具包。
755 0
|
7月前
|
Kubernetes Cloud Native 开发者
探索云原生技术:Kubernetes入门与实践探索Windows操作系统的隐藏功能
【8月更文挑战第31天】在数字化转型的浪潮中,云原生技术成为企业提升敏捷性、效率和可靠性的关键。本文将带你了解云原生的核心组件之一——Kubernetes(K8s),通过浅显易懂的语言和实际代码示例,引导你步入这一强大工具的世界。无论你是初学者还是有经验的开发者,本篇都将为你打开一扇通向高效资源管理与自动化部署的大门。
|
7月前
|
网络安全 API 数据安全/隐私保护
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Windows)
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Windows)
|
7月前
|
Shell PHP Windows
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
|
2天前
|
Unix 虚拟化 Windows
Windows Server 2025 中文版、英文版下载 (2025 年 3 月更新)
Windows Server 2025 中文版、英文版下载 (2025 年 3 月更新)
34 4
Windows Server 2025 中文版、英文版下载 (2025 年 3 月更新)
|
2天前
|
安全 数据安全/隐私保护 虚拟化
Windows Server 2022 中文版、英文版下载 (2025 年 3 月更新)
Windows Server 2022 中文版、英文版下载 (2025 年 3 月更新)
21 4
Windows Server 2022 中文版、英文版下载 (2025 年 3 月更新)