Azure Lei Zhang的博客

weibo: LeiZhang的微博/QQ: 185165016/QQ群:319036205/邮箱:leizhang1984@outlook.com/TeL:139-161-22926

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

  《Windows Azure Platform 系列文章目录

  

  对于A系列和D系列的虚拟机来说,使用的是普通存储。

  普通存储的存储资源,是按照每GB每月计费的。Microsoft Azure普通存储资源是用多少算多少的,且按照平均使用计算。

    http://blogs.msdn.com/b/windowsazurestorage/archive/2010/07/09/understanding-windows-azure-storage-billing-bandwidth-transactions-and-capacity.aspx?PageIndex=2#comments

 

  举个例子:虚拟机操作系统的文件,对于Windows平台,C盘系统盘容量127GB,操作系统实际使用30GB。只收取实际使用的30GB存储。

 

  但是我们通过Azure管理界面,只能知道VHD的容量大小。并不知道实际使用容量。

  笔者在这里提供一个Sample Code,可以计算某个VHD的实际使用容量。

  我们需要准备:

  1.Azure China 存储账号

  2.Azure China 存储账号密码

  3.VHD所在的Container Name

  4.VHD Name

  

  创建Visual Studio Windows Form项目,Nuget增加Azure Storage引用。图略。

  将配置信息保存在App.config文件里:

<appSettings>
    <add key="AccountName" value="leivms"/>
    <add key="AccountKey" value="storagekey"/>
    <add key="ContainerName" value="vhds"/>
    <add key="VHDName" value="Lei2012CHNVM-Lei2012CHNVM01-2016-01-25.vhd"/>
  </appSettings>

 

  Sample Code如下:

        private string accountname;
        private string accountkey;
        private string containername;
        private void Form1_Load(object sender, EventArgs e)
        {
            Calculate();
        }

        private void Calculate()
        {
            var container = GetContainer();
            var cloudpageblob = container.GetPageBlobReference(ConfigurationManager.AppSettings["VHDName"].ToString());

            //OutPut VHD Actual Size
            string actualSize = GetFormattedDiskSize(GetActualDiskSize(cloudpageblob));
        }

        private CloudBlobContainer GetContainer()
        {
            //Get config from Web.Config
            accountname = ConfigurationManager.AppSettings["AccountName"].ToString();
            accountkey = ConfigurationManager.AppSettings["AccountKey"].ToString();
            containername = ConfigurationManager.AppSettings["ContainerName"].ToString();

            string connectionString = GenerateConnectionString();

            //Get Azure Storage Connection String 
            var account = CloudStorageAccount.Parse(connectionString);
            var client = account.CreateCloudBlobClient();
            //Get BlobContainer Object
            return client.GetContainerReference(containername);
        }

        private string GenerateConnectionString()
        {
            StringBuilder sbuilder = new StringBuilder();
            sbuilder.Append(@"BlobEndpoint=https://");
            sbuilder.Append(accountname);
            sbuilder.Append(".blob.core.chinacloudapi.cn/");

            sbuilder.Append(@";QueueEndpoint=https://");
            sbuilder.Append(accountname);
            sbuilder.Append(".queue.core.chinacloudapi.cn/");

            sbuilder.Append(@";TableEndpoint=https://");
            sbuilder.Append(accountname);
            sbuilder.Append(".table.core.chinacloudapi.cn/");

            sbuilder.Append(";AccountName=");
            sbuilder.Append(accountname);

            sbuilder.Append(";AccountKey=");
            sbuilder.Append(accountkey);

            return sbuilder.ToString();
        }


        private long GetActualDiskSize(CloudPageBlob pageBlob)
        {
            pageBlob.FetchAttributes();
            return 124 + pageBlob.Name.Length * 2
               + pageBlob.Metadata.Sum(m => m.Key.Length + m.Value.Length + 3)
               + pageBlob.GetPageRanges().Sum(r => 12 + (r.EndOffset - r.StartOffset));
        }

        [DllImport("Shlwapi.dll", CharSet = CharSet.Auto)]
        public static extern long StrFormatByteSize(long fileSize, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder buffer, int bufferSize);

        public static string GetFormattedDiskSize(long size)
        {
            var sb = new StringBuilder(11);
            StrFormatByteSize(size, sb, sb.Capacity);
            return sb.ToString();
        }

 

  运行结果:

  1.上图中,Lei2012CHNVM-Lei2012CHNVM01-2016-01-25.vhd这个Page Blob容量大小为127GB

  

  

  2.运行代码后,这个vhd的实际使用容量为10GB

  

  

 

 

 

  参考资料:

     http://fabriccontroller.net/calculating-how-much-space-a-windows-azure-disk-is-really-using/

 

posted on 2016-02-03 15:39  Lei Zhang的博客  阅读(1152)  评论(0编辑  收藏  举报