共用方式為


使用 PowerShell 計算 Blob 容器的大小

此指令碼會計算記憶體帳戶中所有 Azure Blob 儲存體容器的大小。

此範例需要 Azure PowerShell。 執行 Get-Module -ListAvailable Az 以尋找版本。 如果您需要安裝或升級,請參閱安裝 Azure PowerShell 模組

執行 Connect-AzAccount Cmdlet 以連線到 Azure。

如果您沒有 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 文件

您可以在 Azure 儲存體的 PowerShell 範例 中找到其他的 PowerShell 指令碼範例。