【发布时间】:2021-12-12 08:49:45
【问题描述】:
我想在不使用门户的情况下检索 Azure 存储帐户的大小(指标)。如何通过 azure CLI 或 bash 脚本获取存储帐户的指标?有没有办法通过 azure CLI 或任何 bash 脚本做到这一点?
【问题讨论】:
标签: azure azure-blob-storage azure-storage azure-cli azure-metrics-advisor
我想在不使用门户的情况下检索 Azure 存储帐户的大小(指标)。如何通过 azure CLI 或 bash 脚本获取存储帐户的指标?有没有办法通过 azure CLI 或任何 bash 脚本做到这一点?
【问题讨论】:
标签: azure azure-blob-storage azure-storage azure-cli azure-metrics-advisor
查看 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"
}
]
}
【讨论】:
Min 和Max,可能还有Sum(但这不太可能有用)。
要计算存储帐户的大小,您需要找到存储帐户中容器的大小,然后将大小相加得到存储帐户的大小。
使用 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
更多详情请参考SO Thread
【讨论】: