首页 > 解决方案 > 使用 Azure CLI 检索 Azure 存储帐户密钥

问题描述

在我的发布管道中,我使用 Azure CLI 将构建文件传输到 Azure 存储 blob:

call az storage blob upload-batch --source "$(System.DefaultWorkingDirectory)/_ClientWeb-Build-CI/ShellArtifact/out/build" --destination "$web" --account-key "****QxjclDGftOY/agnqUDzwNe/gOIAzsQ==" --account-name "*****estx"

这有效,但我想account-key动态检索。

当我使用:

az storage account keys list -g CustomersV2 -n ****estx

我得到一个包含 2 个对象的数组,它们都持有一个键值:

[
    {
    "keyName": "key1",
    "permissions": "Full",
    "value": "f/eybpcl*****************Vm9uT1PwFC1D82QxjclDGftOY/agnqUDzwNe/gOIAzsQ=="
    },
    {
    "keyName": "key2",
    "permissions": "Full",
    "value": "bNM**********L6OxAemK1U2oudW5WGRQW++/bzD6jVw=="
    }
]

如何在upload-batch命令中使用两个键之一?

标签: azureazure-devops

解决方案


对于您的问题,如果您只需要两个键中的一个,例如第一个键。您可以使用键作为值设置变量,如下所示:

key=$(az storage account keys list -g CustomersV2 -n ****estx --query [0].value -o tsv)

key然后在另一个命令中使用该变量,如下所示:

call az storage blob upload-batch --source "$(System.DefaultWorkingDirectory)/_ClientWeb-Build-CI/ShellArtifact/out/build" --destination "$web" --account-key $key --account-name "*****estx"

或者您可以直接将获取密钥的命令放在另一个命令中,如下所示:

call az storage blob upload-batch --source "$(System.DefaultWorkingDirectory)/_ClientWeb-Build-CI/ShellArtifact/out/build" --destination "$web" --account-key $(az storage account keys list -g CustomersV2 -n ****estx --query [0].value -o tsv) --account-name "*****estx"

更新

根据您所说,您似乎在 windows 命令提示符下运行命令,它与 Linux shell 和 PowerShell 不同。您不能使用命令输出的值设置环境变量。你可以这样做:

az storage account keys list -g CustomersV2 -n ****estx --query [0].value -o tsv > key.txt
set /P key=<key.txt
az storage blob upload-batch --source "$(System.DefaultWorkingDirectory)/_ClientWeb-Build-CI/ShellArtifact/out/build" --destination "$web" --account-key %key% --account-name "*****estx"

"$web"而且您似乎可以将环境变量引用为 %variable_name%,因此在您的命令中使用它似乎是一种错误的方式。


推荐阅读