azure - 使用 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
命令中使用两个键之一?
解决方案
对于您的问题,如果您只需要两个键中的一个,例如第一个键。您可以使用键作为值设置变量,如下所示:
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%,因此在您的命令中使用它似乎是一种错误的方式。
推荐阅读
- html - 无法更改底部导航栏的背景颜色
- android - 如何检查firestore中的条目列表
- swift - 如何使用自定义初始化程序扩展 Picker?
- javascript - javascript模数执行不清楚
- python - 如何使用 Azure Databricks 运行具有多个文件和模块的 ML 程序?
- makefile - 在 z/OS makefile 中使用条件
- javascript - 如何从 React.js 中的函数调用 API?
- oracle - What is the difference between BEGIN followed by IF and IF followed by BEGIN
- python - Problem installing PyQtChart module in python 3.7
- visual-studio-code - 特定文件名的 vscode 设置