【文章版权归作者和CNBLOG所有,转载请务必注明原始出处】

猫尾巴的狐狸

导航

Windows azure 上将Legcay IO操作轻松切换到Blob

您准备迁移到azure平台上的程序有大量的类似于File.()…的操作?

不用着急,MS发布的 Microsoft.WindowsAzure.CloudDrive 提供的 API,即可方便的切换到Blob中。

假设您有一个FileUpload的控件,您可能有这样的代码:

File.WriteAllBytes(string.Format("{0}\\{1}", Server.MapPath("~\Data"), fileName), this.FileUpload1.FileBytes);

切换到 Blob?  使用 CloudDrive 提供的API,只需做少量的修改即可。

引用 Microsoft.WindowsAzure.CloudDrive

1. 修改 WebRole

public class WebRole : RoleEntryPoint
    {
        public static string DriveLetter { get; private set; }
        CloudDrive myCloudDrive;

        public override bool OnStart()
        {
            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();
                        }
                    }
                };
            });
            RoleEnvironment.Changing += RoleEnvironmentChanging;
            return base.OnStart();
        }

        private void RoleEnvironmentChanging(object sender, RoleEnvironmentChangingEventArgs e)
        {
            // If a configuration setting is changing
            if (e.Changes.Any(change => change is RoleEnvironmentConfigurationSettingChange))
            {
                // Set e.Cancel to true to restart this role instance
                e.Cancel = true;
            }
        }

        public override void OnStop()
        {
            myCloudDrive.Unmount();
            base.OnStop();
        }

        public override void Run()
        {
            string containerName = "drives";
            string blobName = "mydrive.vhd";
            CloudStorageAccount storageAccount = CloudStorageAccount.FromConfigurationSetting("DiagnosticsConnectionString");
            LocalResource localCache = RoleEnvironment.GetLocalResource("InstanceDriveCache");
            CloudDrive.InitializeCache(localCache.RootPath, localCache.MaximumSizeInMegabytes);
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            blobClient.GetContainerReference(containerName).CreateIfNotExist();
            // create Cloud Drive 
            myCloudDrive = storageAccount.CreateCloudDrive(blobClient.GetContainerReference(containerName).GetPageBlobReference(blobName).Uri.ToString());
            try
            {
                myCloudDrive.Create(32);
            }
            catch (CloudDriveException ex)
            {
            }
            DriveLetter = myCloudDrive.Mount(0, DriveMountOptions.Force);
            base.Run();
        }
    }
2. ServiceDefinition 中增加了缓存的设置[可选]

<ServiceDefinition name="CloudWebDemo" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
  <WebRole name="WebRole1">
    <InputEndpoints>
      <InputEndpoint name="HttpIn" protocol="http" port="80" />
    </InputEndpoints>
    <ConfigurationSettings>
      <Setting name="DiagnosticsConnectionString" />
    </ConfigurationSettings>
    <LocalResources>
      <LocalStorage name="InstanceDriveCache" cleanOnRoleRecycle="false" sizeInMB="100" />
    </LocalResources>

  </WebRole>
</ServiceDefinition>


3. 修改文件操作的代码

 File.WriteAllBytes(string.Format("{0}\\{1}",  WebRole.DriveLetter, fileName), this.FileUpload1.FileBytes);

posted on 2010-05-03 17:45  GinoHuo  阅读(302)  评论(0编辑  收藏  举报