Udostępnij za pomocą


Obliczanie rozmiaru kontenerów blob przy użyciu PowerShell

Ten skrypt oblicza rozmiar wszystkich kontenerów usługi Azure Blob Storage na koncie magazynowym.

Ten przykładowy skrypt wymaga programu Azure PowerShell. Uruchom polecenie Get-Module -ListAvailable Az, aby dowiedzieć się, jaka wersja jest używana. Jeśli konieczna będzie instalacja lub uaktualnienie, zobacz Instalowanie modułu Azure PowerShell.

Uruchom polecenie cmdlet Connect-AzAccount, aby nawiązać połączenie z platformą Azure.

Jeśli nie masz jeszcze konta platformy Azure, przed rozpoczęciem utwórz bezpłatne konto.

Ważne

Ten skrypt programu PowerShell zapewnia szacowany rozmiar kontenerów na koncie i nie powinien być używany do obliczeń rozliczeniowych. Aby uzyskać skrypt, który oblicza rozmiar kontenera na potrzeby rozliczeń, zobacz Obliczanie rozmiaru kontenera Blob na potrzeby rozliczeń.

Przykładowy skrypt

<#
.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

Oczyszczanie wdrożenia

Uruchom następujące polecenie, aby usunąć grupę zasobów, kontener i wszystkie powiązane zasoby.

Remove-AzResourceGroup -Name bloblisttestrg

Objaśnienia dla skryptu

Ten skrypt zawiera następujące polecenia, służące do obliczenia rozmiaru kontenera w usłudze Blob Storage. Każda pozycja w tabeli prowadzi do dokumentacji odpowiednich dla polecenia.

Polecenie Uwagi
Pobierz-AzStorageAccount Pobiera konkretne konto Storage lub wszystkie konta Storage w grupie zasobów lub w subskrypcji.
Get-AzStorageContainer Wyświetla listę kontenerów magazynu.
Get-AzStorageBlob Wyświetla listę obiektów blob w kontenerze.

Następne kroki

Aby uzyskać skrypt, który oblicza rozmiar kontenera na potrzeby rozliczeń, zobacz Obliczanie rozmiaru kontenera Blob na potrzeby rozliczeń.

Aby uzyskać więcej informacji na temat modułu Azure PowerShell, zobacz dokumentację programu Azure PowerShell.

Więcej przykładów skryptów programu PowerShell można znaleźć w przykładach programu PowerShell dla usługi Azure Storage.