计算 Blob 存储容器的大小

此脚本通过计算容器中 blob 的总大小来计算 Azure Blob 存储中容器的大小。

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

运行 Connect-AzureRmAccount -Environment AzureChinaCloud,创建与 Azure 的连接。

如果没有 Azure 订阅,可在开始前创建一个试用帐户

Important

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

示例脚本

# 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. We will be adding a sample showing that scenario in the future.

# 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-AzureRmStorageAccount `
  -ResourceGroupName $resourceGroup `
  -Name $storageAccountName
$ctx = $storageAccount.Context 

# get a list of all of the blobs in the container 
$listOfBLobs = Get-AzureStorageBlob -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

清理部署

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

Remove-AzureRmResourceGroup -Name bloblisttestrg

脚本说明

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

命令 注释
Get-AzureRmStorageAccount 获取资源组或订阅中的指定存储帐户或所有存储帐户。
Get-AzureStorageBlob 列出容器中的 Blob。

后续步骤

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

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

可以在 Azure 存储的 PowerShell 示例中找到其他存储 PowerShell 脚本示例。