计算 Blob 存储容器的大小Calculate the size of a Blob storage container

此脚本通过计算容器中 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 CLITo run this sample, install the latest version of the Azure CLI. 若要开始,请运行 az login 以创建与 Azure 的连接。To start, run az login to create a connection with Azure.

适用于 Azure CLI 的示例是针对 bash shell 编写的。Samples for the Azure CLI are written for the bash shell. 若要在 Windows PowerShell 或命令提示符中运行此示例,可能需要更改脚本的元素。To run this sample in Windows PowerShell or Command Prompt, you may need to change elements of the script.

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

Important

此 CLI 脚本提供容器的估计大小,不应用于计费计算。This CLI script provides an estimated size for the container and should not be used for billing calculations.

示例脚本Sample script

#!/bin/bash
export AZURE_STORAGE_ACCOUNT=<storage-account-name>
export AZURE_STORAGE_ACCESS_KEY=<storage-account-key>

# Create a resource group
az group create --name myResourceGroup --location chinaeast

# Create a container
az storage container create --name mycontainer

# Create sample files to upload as blobs
for i in `seq 1 3`; do
    echo $RANDOM > container_size_sample_file_$i.txt
done

# Upload sample files to container
az storage blob upload-batch \
    --pattern "container_size_sample_file_*.txt" \
    --source . \
    --destination mycontainer

# Calculate total size of container. Use the --query parameter to display only
# blob contentLength and output it in TSV format so only the values are
# returned. Then pipe the results to the paste and bc utilities to total the
# size of the blobs in the container.
bytes=`az storage blob list \
    --container-name mycontainer \
    --query "[*].[properties.contentLength]" \
    --output tsv |
    paste --serial --delimiters=+ | bc`

# Display total bytes
echo "Total bytes in container: $bytes"

# Delete the sample files created by this script
rm container_size_sample_file_*.txt

清理部署Clean up deployment

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

az group delete --name myResourceGroup

脚本说明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
az group createaz group create 创建用于存储所有资源的资源组。Creates a resource group in which all resources are stored.
az storage blob uploadaz storage blob upload 将本地文件上传到 Azure Blob 存储容器。Uploads local files to an Azure Blob storage container.
az storage blob listaz storage blob list 列出 Azure Blob 存储容器中的 blob。Lists the blobs in an Azure Blob storage container.

后续步骤Next steps

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

有关其他存储 CLI 脚本示例,可参阅 Azure Blob 存储的 Azure CLI 示例Additional storage CLI script samples can be found in the Azure CLI samples for Azure Blob storage.