如何在 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 and invoke 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

请参阅此 document 了解更多详情:

使用 PowerShell 的示例

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

有关详细信息,请参阅此