使用 PowerShell 计算 blob 容器的大小Calculate the size of a blob container with PowerShell

此脚本通过计算容器中 blob 的总大小来计算 Azure Blob 存储中容器的大小。This script calculates the size of a container in Azure Blob storage by totaling the size of the blobs in the container.

本示例需要 Azure PowerShell。This sample requires Azure PowerShell. 运行 Get-Module -ListAvailable Az 即可查找版本。Run Get-Module -ListAvailable Az to find the version. 如果需要进行安装或升级,请参阅安装 Azure PowerShell 模块If you need to install or upgrade, see Install Azure PowerShell module.

运行 Connect-AzAccount -Environment AzureChinaCloud,创建与 Azure 的连接。Run Connect-AzAccount -Environment AzureChinaCloud to create a connection with Azure.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

Important

此 PowerShell 脚本提供容器的估计大小,不应该用于计费计算。This PowerShell script provides an estimated size for the container and should not be used for billing calculations. 有关为计费目的计算容器大小的脚本,请参阅为计费目的计算 Blob 存储容器的大小For a script that calculates container size for billing purposes, see Calculate the size of a Blob storage container for billing purposes.

示例脚本Sample script

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

# these are for the storage account to be used
$resourceGroup = "bloblisttestrg"
$storageAccountName = "contosobloblisttest"
$containerName = "listtestblobs"

# get a reference to the storage account and the context
$storageAccount = Get-AzStorageAccount `
  -ResourceGroupName $resourceGroup `
  -Name $storageAccountName
$ctx = $storageAccount.Context 

# get a list of all of the blobs in the container 
$listOfBLobs = Get-AzStorageBlob -Container $ContainerName -Context $ctx 

# zero out our total
$length = 0

# this loops through the list of blobs and retrieves the length for each blob
#   and adds it to the total
$listOfBlobs | ForEach-Object {$length = $length + $_.Length}

# output the blobs and their sizes and the total 
Write-Host "List of Blobs and their size (length)"
Write-Host " " 
$listOfBlobs | select Name, Length
Write-Host " "
Write-Host "Total Length = " $length

清理部署Clean up deployment

运行以下命令来删除资源组、容器和所有相关资源。Run the following command to remove the resource group, container, and all related resources.

Remove-AzResourceGroup -Name bloblisttestrg

脚本说明Script explanation

此脚本使用以下命令来计算 Blob 存储容器的大小。This script uses the following commands to calculate the size of the Blob storage container. 表中的每一项均链接到命令特定的文档。Each item in the table links to command-specific documentation.

CommandCommand 说明Notes
Get-AzStorageAccountGet-AzStorageAccount 获取资源组或订阅中的指定存储帐户或所有存储帐户。Gets a specified Storage account or all of the Storage accounts in a resource group or the subscription.
Get-AzStorageBlobGet-AzStorageBlob 列出容器中的 Blob。Lists blobs in a container.

后续步骤Next steps

有关为计费目的计算容器大小的脚本,请参阅为计费目的计算 Blob 存储容器的大小For a script that calculates container size for billing purposes, see Calculate the size of a Blob storage container for billing purposes.

有关 Azure PowerShell 模块的详细信息,请参阅 Azure PowerShell 文档For more information on the Azure PowerShell module, see Azure PowerShell documentation.

可以在 Azure 存储的 PowerShell 示例中找到其他存储 PowerShell 脚本示例。Additional storage PowerShell script samples can be found in PowerShell samples for Azure Storage.