【发布时间】:2019-03-21 18:52:30
【问题描述】:
我正在使用 Microsoft Azure CLI,但找不到列出存储帐户的 blob 对象的方法。
我在 Azure Web 门户中显示了该列表,但我找不到任何可以使用 az storage 命令的方法。
我尝试过az storage blob list,但它需要一个我不知道如何找到它的容器名称(使用 az cli)。
有人有想法吗?
【问题讨论】:
我正在使用 Microsoft Azure CLI,但找不到列出存储帐户的 blob 对象的方法。
我在 Azure Web 门户中显示了该列表,但我找不到任何可以使用 az storage 命令的方法。
我尝试过az storage blob list,但它需要一个我不知道如何找到它的容器名称(使用 az cli)。
有人有想法吗?
【问题讨论】:
更新:在 cli 中获取帐户密钥:
请尝试下面的代码,它可以列出存储帐户所有容器中的所有 blob。
请注意,“=”周围没有空格。
# list storage account names
az storage account list --query "[].{name:name}" --output tsv)"
# update with your storage account name
storage_account_name="your_storage_account_name"
key="$(az storage account keys list -n ${storage_account_name} --query "[0].{value:value}" --output tsv)"
containers="$(az storage container list --account-name ${storage_account_name} --account-key $key --query "[].{name:name}" --output tsv)"
for c in $containers
do
echo "==== Blobs in Container $c ===="
az storage blob list --container-name $c \
--account-name ${storage_account_name} \
--account-key $key \
--query "[].{name:name}" --output tsv
done
测试结果如下:
【讨论】:
az cli 列出我的帐户存储密钥时出现以下错误:The client 'XXX@XXX.onmicrosoft.com' with object id '091bb6b2-XXX-46da-ac31-f350XXXXXX' does not have authorization to perform action 'Microsoft.Storage/storageAccounts/listKeys/action' over scope ...。我知道我无权这样做,但在 Web 门户中,我的用户帐户能够列出任何存储帐户的 blob 对象(所有资源 => az cli 无法获取此列表(没有帐户存储密钥)?感谢 Ivan Yang 的回答!
建议的编辑队列已满,因此我想将编辑后的代码粘贴回去:
get_blobs.sh
accounts="$(az storage account list --query "[].{name:name}" --output tsv)"
for storage_account_name in $accounts
do
echo "==== Storage Account ${storage_account_name} ===="
key="$(az storage account keys list -n ${storage_account_name} --query "[0].{value:value}" --output tsv)"
containers="$(az storage container list --account-name ${storage_account_name} --account-key $key --query "[].{name:name}" --output tsv)"
for c in $containers
do
echo "==== Blobs in Container $c ===="
az storage blob list --container-name $c \
--account-name ${storage_account_name} \
--account-key $key \
--query "[].{name:name}" --output tsv
done
done
我在 Azure Cloud CLI 中运行我的:
^r 行结尾错误,请在文件上运行 dos2unix get_blobs.sh 以解决该问题。chmod u+x get_blobs.sh 允许它执行。./get_blobs.sh >> output.txt 运行并输出到文本文件。【讨论】:
这是一个列出所有 blob 容器的简单脚本。作为奖励,您也可以列出所有文件共享!
# List all the storage accounts under a subscription
for actname in $( az storage account list --query "[].name" --output tsv );
do
# Get the storage key
key1=`az storage account keys list --account-name $actname --query "[0].value" --output tsv 2>/dev/zero`
# Exclude the listing when you do not have permission to look into the storage account - like databricks managed storage for example
if [ $? -ne 0 ] ; then
continue
fi
echo -e "\n === Here are the storage containers for your storage account $actname === \n"
# List of containers and file shares for the account
az storage container list --account-name $actname --account-key $key1 --query "[].name" --output tsv
echo -e "\n --- Here are the storage file shares for your storage account $actname ---\n"
az storage share list --account-name $actname --account-key $key1 --query "[].name" --output tsv
done
【讨论】:
如果我理解正确,解决方案可以分为 3 部分
az 存储帐户列表 --query "[].{name:name}" --output tsv
az 存储帐户密钥列表 --account-name WHATEVER_ACCOUNTNAME --query "[0].value"
3- 最后得到你想要的帐户的容器列表(这里要注意的是,“key”应该从第二个命令的输出中正确复制)
az 存储容器列表 --account-name WHATEVER_ACCOUNTNAME --account-key YOUR-VERY-VERY-LONG-KEY --query "[].{name:name}" --output tsv
注意-首先不要忘记登录 azure 帐户 :)。
az 登录
【讨论】: