Skip to content

Instantly share code, notes, and snippets.

@carsonip
Created February 8, 2018 09:56
Show Gist options
  • Save carsonip/89350974e8f58eac68af8409bc03a58f to your computer and use it in GitHub Desktop.
Save carsonip/89350974e8f58eac68af8409bc03a58f to your computer and use it in GitHub Desktop.
Get Azure Blob Storage container size without System.OutOfMemoryException
# this script will show how to get the total size of the blobs in a container
# before running this, you need to create a storage account, create a container,
# and upload some blobs into the container
# note: this retrieves all of the blobs in the container in one command
# if you are going to run this against a container with a lot of blobs
# (more than a couple hundred), use continuation tokens to retrieve
# the list of blobs. We will be adding a sample showing that scenario in the future.
# these are for the storage account to be used
param(
[Parameter(Mandatory = $true)]
[string]$resourceGroup,
[Parameter(Mandatory = $true)]
[string]$storageAccountName,
[Parameter(Mandatory = $true)]
[string]$containerName,
# Numbers of blobs to be processed in a batch.
[Parameter(Mandatory = $false)]
[int]$BatchCount = 10000
)
# get a reference to the storage account and the context
$storageAccount = Get-AzureRmStorageAccount `
-ResourceGroupName $resourceGroup `
-Name $storageAccountName
$ctx = $storageAccount.Context
# zero out our total
[long]$length = 0
$Token = $Null
[long]$blobCount = 0
do
{
$Blobs = Get-AzureStorageBlob -Context $ctx -Container $ContainerName -MaxCount $BatchCount -ContinuationToken $Token
$Blobs | ForEach-Object {$length = $length + $_.Length}
$blobCount = $blobCount + $Blobs.Count
$Token = $Blobs[$Blobs.Count - 1].ContinuationToken;
Write-Host "Count = " $blobCount " Length = " $length
}
while ($Token -ne $Null)
Write-Host "Total Blob Count = " $blobCount
Write-Host "Total Length = " $length
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment