Skip to content

Instantly share code, notes, and snippets.

@ajith-k
Last active January 30, 2019 05:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajith-k/80a2dc84986d6bb83be76be6bc672043 to your computer and use it in GitHub Desktop.
Save ajith-k/80a2dc84986d6bb83be76be6bc672043 to your computer and use it in GitHub Desktop.
Get the list of blobs and sum up the sizes per container
###
## DISCLAIMER : This is a sample and is provided as is with no warranties express or implied.
##
###
[CmdletBinding(DefaultParametersetName="SharedKey")]
param(
[Parameter(Mandatory=$true, HelpMessage="Storage Account Name")]
[String] $storage_account_name,
[Parameter(Mandatory=$true, HelpMessage="Any one of the two shared access keys", ParameterSetName="SharedKey", Position=1)]
[String] $storage_shared_key,
[Parameter(Mandatory=$true, HelpMessage="SAS Token : the GET parameters", ParameterSetName="SASToken", Position=1)]
[String] $storage_sas_token
)
$containerstats = @()
If ($PsCmdlet.ParameterSetName -eq "SharedKey")
{
$Ctx = New-AzureStorageContext -StorageAccountName $storage_account_name -StorageAccountKey $storage_shared_key
}
Else
{
$Ctx = New-AzureStorageContext -StorageAccountName $storage_account_name -SasToken $storage_sas_token
}
 
$container_continuation_token = $null
do {
$containers = Get-AzureStorageContainer -Context $Ctx -MaxCount 5000 -ContinuationToken $container_continuation_token
$container_continuation_token = $null;
if ($containers -ne $null)
{
$container_continuation_token = $containers[$containers.Count - 1].ContinuationToken
for ([int] $c = 0; $c -lt $containers.Count; $c++)
{
$container = $containers[$c].Name
Write-Verbose "Processing container : $container"
$total_usage = 0
$total_blob_count = 0
$soft_delete_usage = 0
$soft_delete_count = 0
$blob_continuation_token = $null
do {
$blobs = Get-AzureStorageBlob -Context $Ctx -Container $container -MaxCount 5000 -IncludeDeleted -ContinuationToken $blob_continuation_token
$blob_continuation_token = $null;
if ($blobs -ne $null)
{
$blob_continuation_token = $blobs[$blobs.Count - 1].ContinuationToken
for ([int] $b = 0; $b -lt $blobs.Count; $b++)
{
$total_blob_count++
$total_usage += $blobs[$b].Length
if ($blobs[$b].IsDeleted)
{
$soft_delete_count++
$soft_delete_usage += $blobs[$b].Length
}
}
If ($blob_continuation_token -ne $null)
{
Write-Verbose "Blob listing continuation token = {0}".Replace("{0}",$blob_continuation_token.NextMarker)
}
}
} while ($blob_continuation_token -ne $null)
Write-Verbose "Calculated size of $container = $total_usage with soft_delete usage of $soft_delete_usage"
$containerstats += [PSCustomObject] @{
Name = $container
TotalBlobCount = $total_blob_count
TotalBlobUsage = $total_usage
SoftDeletedBlobCount = $soft_delete_count
SoftDeletedBlobUsage = $soft_delete_usage
}
}
}
If ($container_continuation_token -ne $null)
{
Write-Verbose "Container listing continuation token = {0}".Replace("{0}",$container_continuation_token.NextMarker)
}
} while ($container_continuation_token -ne $null)
Write-Host "Total container stats"
$containerstats | Format-Table -AutoSize
@ajith-k
Copy link
Author

ajith-k commented Dec 19, 2018

Account for soft-deleted blobs.

@ajith-k
Copy link
Author

ajith-k commented Dec 21, 2018

See this other gist for a sample script that will remove soft delete data :- https://gist.github.com/ajith-k/b68c8550bc77a13f0436c260100499df

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment