使用VHDUpload上传VHD文件到云存储,并在Azure VM中加挂Azure Drive


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里实验通过)





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值