Skip to content

Instantly share code, notes, and snippets.

@rcabr
Created May 18, 2023 14:23
Show Gist options
  • Save rcabr/c3703954d476d34e60108829f837d675 to your computer and use it in GitHub Desktop.
Save rcabr/c3703954d476d34e60108829f837d675 to your computer and use it in GitHub Desktop.
Get total size of all blobs in Azure Storage (with HNS enabled)
# For an Azure Storage account with Hierarchical Namespace enabled,
# this script totals up all the blobs in a container and outputs the total size in GB
# Set these variables to the storage account and container you want to get the size of
$ENV:AZURE_STORAGE_ACCOUNT = "[STORAGE ACCOUNT NAME]"
$containerName = "[CONTAINER NAME]"
# housekeeping
$ENV:AZURE_STORAGE_AUTH_MODE = "login"
$nextMarker = $null
$chunkSize = 5000
$totalSize = 0
# use az cli to get a list of blobs in a container and their sizes, using the next marker to get all of the blobs
do {
if ($null -ne $nextMarker) {
$resultLines = az storage fs file list -f $containerName --marker $nextMarker --num-results $chunkSize --query "[?!isDirectory].{name: name, size: contentLength, marker: nextMarker}" --show-next-marker
} else {
$resultLines = az storage fs file list -f $containerName --num-results $chunkSize --query "[?!isDirectory].{name: name, size: contentLength, marker: nextMarker}" --show-next-marker
}
$resultJson = $resultLines -join '' | ConvertFrom-Json
$sizes = $resultJson | ForEach-Object { $_.size }
$totalSize += ($sizes | Measure-Object -Sum).Sum / 1073741824
Write-Output("> In progress size: {0} GB" -f $totalSize)
$nextMarker = $resultJson[-1].marker
} while ($null -ne $nextMarker);
Write-Output("Total size: {0} GB" -f $totalSize)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment