Home > Mobile >  How to get the size of an azure storage account in Azure CLI or Bash Script?
How to get the size of an azure storage account in Azure CLI or Bash Script?

Time:10-27

I want to retrieve the size of an azure storage account without using the portal (metrics). How can I get the metrics of the storage account through azure CLI or bash script? Is there a way to make this through azure CLI or any bash scripts?

CodePudding user response:

Looking at the AZ CLI commands, I believe there's no command currently available that will give you this information directly.

What you will need to do is make use of az rest and invoke Metrics - List REST API and parse the response.

Here's the command you would want to execute:

az rest --uri https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Storage/storageAccounts/<storage-account-name>/providers/Microsoft.Insights/metrics?api-version=2018-01-01&metricnames=UsedCapacity&aggregation=Average

You would get a response like the following:

{
  "cost": 59,
  "interval": "PT1H",
  "namespace": "Microsoft.Storage/storageAccounts",
  "resourceregion": "resource-location",
  "timespan": "2021-10-27T05:12:06Z/2021-10-27T06:12:06Z",
  "value": [
    {
      "displayDescription": "The amount of storage used by the storage account. For standard storage accounts, it's the sum of capacity used by blob, table, file, and queue. For premium storage accounts and Blob storage accounts, it is the same as BlobCapacity or FileCapacity.",
      "errorCode": "Success",
      "id": "/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Storage/storageAccounts/<storage-account-name>/providers/Microsoft.Insights/metrics/UsedCapacity",
      "name": {
        "localizedValue": "Used capacity",
        "value": "UsedCapacity"
      },
      "resourceGroup": "cerebrata",
      "timeseries": [
        {
          "data": [
            {
              "average": 9078827149.0,//This is the value you would want to extract
              "timeStamp": "2021-10-27T05:12:00Z"
            }
          ],
          "metadatavalues": []
        }
      ],
      "type": "Microsoft.Insights/metrics",
      "unit": "Bytes"
    }
  ]
}

CodePudding user response:

To Calculate size of storage account you need to find the size of containers in the storage account then sum the size you get the size of storage account.

Example to get length of container using Azure CLI

#!/bin/bash
export AZURE_STORAGE_ACCOUNT=<storage-account-name>
export AZURE_STORAGE_ACCESS_KEY=<storage-account-key>

# Create a resource group
az group create --name myResourceGroup --location eastus

# Create a container
az storage container create --name mycontainer

# Create sample files to upload as blobs
for i in `seq 1 3`; do
    echo $RANDOM > container_size_sample_file_$i.txt
done

# Upload sample files to container
az storage blob upload-batch \
    --pattern "container_size_sample_file_*.txt" \
    --source . \
    --destination mycontainer

# Calculate total size of container. Use the --query parameter to display only
# blob contentLength and output it in TSV format so only the values are
# returned. Then pipe the results to the paste and bc utilities to total the
# size of the blobs in the container.
bytes=`az storage blob list \
    --container-name mycontainer \
    --query "[*].[properties.contentLength]" \
    --output tsv |
    paste --serial --delimiters=  | bc`

# Display total bytes
echo "Total bytes in container: $bytes"

# Delete the sample files created by this script
rm container_size_sample_file_*.txt

Refer this document for more details :

Example using PowerShell

Get-AzureStorageBlob -Container "ContainerName" | %{ $_.Length } | measure -Sum

For more details refer this SO Thread

  • Related