Windows Azure Storage (23) 計算Azure VHD實際使用容量

來源:互聯網
上載者:User

標籤:

  《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

  

  將配置資訊儲存在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

  

  

  3.我們開啟這台Windows虛擬機器,顯示C盤容量為9.58G

  

 

 

 

  參考資料:

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

 

Windows Azure Storage (23) 計算Azure VHD實際使用容量

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.