使用 PowerShell 计算 Blob 容器的大小

此脚本会计算存储帐户中所有 Azure Blob 存储容器的大小。

本示例需要 Azure PowerShell。 运行 Get-Module -ListAvailable Az 即可查找版本。 如果需要进行安装或升级,请参阅安装 Azure PowerShell 模块

运行 Connect-AzAccount -Environment AzureChinaCloud cmdlet 以连接到世纪互联运营的 Azure。

如果没有 Azure 试用版订阅,请在开始前创建一个试用版订阅

重要

此 PowerShell 脚本会提供帐户中的容器的估计大小,但不应该用于费用计算。 有关为计费目的计算容器大小的脚本,请参阅为计费目的计算 Blob 存储容器的大小

示例脚本

<#
.SYNOPSIS
Calculates the total size of blobs in all containers in a specified Azure storage account.

.DESCRIPTION
Before running this script, ensure you have:
- A storage account created
- At least one container in the storage account
- Uploaded some blobs into the container

.EXAMPLE
.\Get-AzureStorageAccountBlobSize.ps1 -StorageAccountName mystorageaccount -ResourceGroupName myResourceGroup

.NOTES
This script incurs transactional costs for Azure requests.
#>

[CmdletBinding()]
param (
    [ValidateNotNullOrEmpty()]
    [string]$ResourceGroupName = '<name-of-your-resource-group>',
    
    [ValidateNotNullOrEmpty()]
    [string]$StorageAccountName = '<name-of-your-storage-account>'
)

$containerstats = @()

# Get a reference to the storage account and the context.
$storageAccount = Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName
$Ctx = $storageAccount.Context

$container_continuation_token = $null
do {
  $containers = Get-AzStorageContainer -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
      $version_usage = 0
      $version_count = 
      $snapshot_count = 0 
      $snapshot_usage = 0
      $blob_continuation_token = $null
      
      do {
        $blobs = Get-AzStorageBlob -Context $Ctx -IncludeDeleted -IncludeVersion -Container $container -ConcurrentTaskCount 100 -MaxCount 5000 -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 ($blobs[$b].SnapshotTime -ne $null) {
              $snapshot_count++
              $snapshot_usage+= $blobs[$b].Length
            }
            
            if ($blobs[$b].VersionId -ne $null) {
              $version_count++
              $version_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 
        TotalBlobUsageinGB = $total_usage/1GB
        SoftDeletedBlobCount = $soft_delete_count
        SoftDeletedBlobUsageinGB = $soft_delete_usage/1GB
    SnapshotCount = $snapshot_count
    SnapshotUsageinGB = $snapshot_usage/1GB
    VersionCount = $version_count
    VersionUsageinGB = $version_usage/1GB
      }
    }
  }
  
  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

清理部署

运行以下命令来删除资源组、容器和所有相关资源。

Remove-AzResourceGroup -Name bloblisttestrg

脚本说明

此脚本使用以下命令来计算 Blob 存储容器的大小。 表中的每一项均链接到命令特定的文档。

Command 说明
Get-AzStorageAccount 获取资源组或订阅中的指定存储帐户或所有存储帐户。
Get-AzStorageContainer 列出存储容器。
Get-AzStorageBlob 列出容器中的 Blob。

后续步骤

有关为计费目的计算容器大小的脚本,请参阅为计费目的计算 Blob 存储容器的大小

有关 Azure PowerShell 模块的详细信息,请参阅 Azure PowerShell 文档

有关 PowerShell 脚本示例的详细信息,可查看适用于 Azure 存储的 PowerShell 示例