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?
How to get the size of an azure storage account in Azure CLI or Bash Script?
3.3k Views Asked by Sashin Sahasra At
3
There are 3 best solutions below
2
On
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

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 restand invokeMetrics - ListREST API and parse the response.Here's the command you would want to execute:
You would get a response like the following: