如何通过Azure CLI获取Blob文件列表并显示文件大小?
解决Azure CLI同时列出Blob文件名与大小的问题
你之前的命令失败是因为误用了VM资源的属性字段(storageProfile.osDisk.diskSizeGb),而Blob存储的大小字段是properties.contentLength。
正确的命令
使用以下命令即可同时获取文件名和对应的文件大小:
az storage blob directory list --account-name ${a} --account-key ${k} -c ${container} -d path/${yyyymmdd} --query "[].{Name:name, Size:properties.contentLength}" --output table
补充说明
properties.contentLength是Blob存储返回结果中表示文件大小的字段,单位为字节。- 如果需要将大小转换为更易读的格式(如MB/GB),可以结合JMESPath表达式计算,例如:
该命令会将大小转换为MB并保留两位小数。az storage blob directory list --account-name ${a} --account-key ${k} -c ${container} -d path/${yyyymmdd} --query "[].{Name:name, Size_MB:round(properties.contentLength/1024/1024, 2)}" --output table
验证字段的方法
如果不确定返回结果中的字段名,可以先去掉--query参数执行命令,查看完整的返回结构,定位对应属性:
az storage blob directory list --account-name ${a} --account-key ${k} -c ${container} -d path/${yyyymmdd}
内容的提问来源于stack exchange,提问作者Miss_Orchid
相关产品推荐
相关产品推荐

