Share via


計算 Blob 儲存體容器的大小

此指令碼會加總容器中 Blob 的大小,計算出 Azure Blob 儲存體的容器大小。

如果您沒有 Azure 訂用帳戶,請在開始之前先建立 Azure 免費帳戶

重要

此 CLI 指令碼提供的是容器的估計大小,且不應用於計費計算。

使用單一清單呼叫傳回的最大 Blob 數目是 5000 個。 如果您需要傳回 5000 個以上的 Blob,請使用接續權杖來要求額外的結果集。

必要條件

範例指令碼

啟動 Azure Cloud Shell

Azure Cloud Shell 是免費的互動式 Shell,可讓您用來執行本文中的步驟。 它具有預先安裝和設定的共用 Azure 工具,可與您的帳戶搭配使用。

若要開啟 Cloud Shell,只要選取程式碼區塊右上角的 [試試看] 即可。 您也可以移至 https://shell.azure.com ,從另一個瀏覽器索引標籤啟動 Cloud Shell。

當開啟 Cloud Shell 時,請確認已為您的環境選取 Bash。 後續的工作階段將會在 Bash 環境中使用 Azure CLI,請選取 [複製] 以複製程式碼區塊,並將其貼到 Cloud Shell 中,然後按 Enter 鍵加以執行。

登入 Azure

系統會在登入的初始帳戶下自動驗證 Cloud Shell。 使用下列指令碼透過不同的訂閱登入,並將 <Subscription ID> 取代為您的 Azure 訂用帳戶識別碼。 如果您沒有 Azure 訂用帳戶,請在開始之前先建立 Azure 免費帳戶

subscription="<subscriptionId>" # add subscription here

az account set -s $subscription # ...or use 'az login'

如需詳細資訊,請參閱設定使用中訂閱以互動方式登入

執行指令碼

# Calculate container size

# Variables for storage
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
resourceGroup="msdocs-azuresql-rg-$randomIdentifier"
tag="calculate-container-size"
storage="msdocsstorage$randomIdentifier"
container="msdocs-storage-container-$randomIdentifier"

# Create a resource group
echo "Creating $resourceGroup in $location..."
az group create --name $resourceGroup --location "$location" --tags $tag

# Create storage account
echo "Creating $storage..."
az storage account create --name $storage --resource-group $resourceGroup --location "$location" --sku Standard_LRS

# Create a container
echo "Creating $container on $storage..."
key=$(az storage account keys list --account-name $storage --resource-group $resourceGroup -o json --query [0].value | tr -d '"')

az storage container create --name $container --account-key $key --account-name $storage #--public-access container

# Create sample files to upload as blobs
for i in `seq 1 3`; do
    echo $randomIdentifier > 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 $container \
    --account-key $key \
    --account-name $storage

# 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. The bc utility is not supported in Cloud Shell.
bytes=`az storage blob list \
    --container-name $container \
    --account-key $key \
    --account-name $storage \
    --query "[*].[properties.contentLength]" \
    --output tsv | paste -s -d+ | bc`

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

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

清除資源

您可以使用下列命令來移除資源群組及所有與其相關聯的資源,除非您仍持續需要這些資源,否則請使用 az group delete 命令。 某些資源可能需要一些時間才能建立或刪除。

az group delete --name $resourceGroup

範例參考

此指令碼會使用下列命令來計算 Blob 儲存體容器的大小。 下表中的每個項目都會連結至特定命令的文件。

Command 注意
az group create 建立用來存放所有資源的資源群組。
az storage blob upload 將本機檔案上傳至 Azure Blob 儲存體容器。
az storage blob list 列出 Azure Blob 儲存體容器中的 Blob。

下一步

如需 Azure CLI 的詳細資訊,請參閱 Azure CLI 文件

您可以在適用於 Azure Blob 儲存體的 Azure CLI 範例中,找到其他儲存體 CLI 指令碼範例。