Check space used in azure storage accounts in my subscription

前端 未结 6 541
面向向阳花
面向向阳花 2021-01-17 07:36

How can I check how much space I used in each of my azure storage accounts in my subscription resource group wise.

I am not able to find a way to check space used in

6条回答
  •  孤城傲影
    2021-01-17 08:19

    Here is a .net core script I use to list storage account usage using the average metrics value of the last hour.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Threading;
    using Microsoft.Azure.Management.CosmosDB.Fluent.Models;
    using Microsoft.Azure.Management.Fluent;
    using Microsoft.Azure.Management.Monitor;
    using Microsoft.Azure.Management.Monitor.Models;
    using Microsoft.Rest.Azure.Authentication;
    
    namespace storagelist
    {
        class Program
        {
            static async System.Threading.Tasks.Task Main(string[] args)
            {
                // to generate my.azureauth file run the follow command:
                // az ad sp create-for-rbac --sdk-auth > my.azureauth
                var azure = Azure.Authenticate("my.azureauth").WithDefaultSubscription();
    
                var accounts = azure.StorageAccounts.List();
                // can get values from my.azureauth
                var tenantId = "";
                var clientId = "";
                var clientSecret = "";
                var serviceCreds = await ApplicationTokenProvider.LoginSilentAsync(tenantId, clientId, clientSecret);
                MonitorManagementClient readOnlyClient = new MonitorManagementClient(serviceCreds);
    
                var oneHour = System.TimeSpan.FromHours(1);
                var startDate = DateTime.Now.AddHours(-oneHour.Hours).ToUniversalTime().ToString("o");
                string endDate = DateTime.Now.ToUniversalTime().ToString("o");
                string timeSpan = startDate + "/" + endDate;
    
                List fileContents = new List();
    
                foreach (var storage in accounts)
                {
                    var response = await readOnlyClient.Metrics.ListAsync(
                    resourceUri: storage.Id,
                    timespan: timeSpan,
                    interval: oneHour,
                    metricnames: "UsedCapacity",
    
                    aggregation: "Average",
                    resultType: ResultType.Data,
                    cancellationToken: CancellationToken.None);
    
                    foreach (var metric in response.Value)
                    {
                        foreach (var series in metric.Timeseries)
                        {
                            foreach (var point in series.Data)
                            {
                                if (point.Average.HasValue)
                                {
                                    fileContents.Add($"{storage.Id}, {point.Average.Value}");
                                    break;
                                }
                            }
                            break;
                        }
                        break;
                    }
                }
    
                await File.WriteAllLinesAsync("./storage.csv", fileContents);
            }
        }
    }
    

提交回复
热议问题