簡體   English   中英

如何在 Azure CLI 或 Bash 腳本中獲取 Azure 存儲帳戶的大小?

[英]How to get the size of an azure storage account in Azure CLI or Bash Script?

我想在不使用門戶(指標)的情況下檢索 Azure 存儲帳戶的大小。 如何通過 azure CLI 或 bash 腳本獲取存儲帳戶的指標? 有沒有辦法通過 azure CLI 或任何 bash 腳本來做到這一點?

查看 AZ CLI 命令,我相信目前沒有可用的命令可以直接為您提供此信息。

您需要做的是利用az rest並調用Metrics - List REST API 並解析響應。

這是您要執行的命令:

az rest --uri https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Storage/storageAccounts/<storage-account-name>/providers/Microsoft.Insights/metrics?api-version=2018-01-01&metricnames=UsedCapacity&aggregation=Average

你會得到如下響應:

{
  "cost": 59,
  "interval": "PT1H",
  "namespace": "Microsoft.Storage/storageAccounts",
  "resourceregion": "resource-location",
  "timespan": "2021-10-27T05:12:06Z/2021-10-27T06:12:06Z",
  "value": [
    {
      "displayDescription": "The amount of storage used by the storage account. For standard storage accounts, it's the sum of capacity used by blob, table, file, and queue. For premium storage accounts and Blob storage accounts, it is the same as BlobCapacity or FileCapacity.",
      "errorCode": "Success",
      "id": "/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Storage/storageAccounts/<storage-account-name>/providers/Microsoft.Insights/metrics/UsedCapacity",
      "name": {
        "localizedValue": "Used capacity",
        "value": "UsedCapacity"
      },
      "resourceGroup": "cerebrata",
      "timeseries": [
        {
          "data": [
            {
              "average": 9078827149.0,//This is the value you would want to extract
              "timeStamp": "2021-10-27T05:12:00Z"
            }
          ],
          "metadatavalues": []
        }
      ],
      "type": "Microsoft.Insights/metrics",
      "unit": "Bytes"
    }
  ]
}

要計算存儲帳戶的大小,您需要找到存儲帳戶中容器的大小,然后將得到的存儲帳戶大小相加。

使用 Azure CLI 獲取容器長度的示例

#!/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 eastus

# 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

有關更多詳細信息,請參閱此文檔

使用 PowerShell 的示例

Get-AzureStorageBlob -Container "ContainerName" | %{ $_.Length } | measure -Sum

有關更多詳細信息,請參閱此SO Thread

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM