使用 Azure CLI 检索 Azure 存储帐户密钥

Pet*_*sma 4 azure azure-devops

在我的发布管道中,我使用 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"
Run Code Online (Sandbox Code Playgroud)

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

当我使用:

az storage account keys list -g CustomersV2 -n ****estx
Run Code Online (Sandbox Code Playgroud)

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

[
    {
    "keyName": "key1",
    "permissions": "Full",
    "value": "f/eybpcl*****************Vm9uT1PwFC1D82QxjclDGftOY/agnqUDzwNe/gOIAzsQ=="
    },
    {
    "keyName": "key2",
    "permissions": "Full",
    "value": "bNM**********L6OxAemK1U2oudW5WGRQW++/bzD6jVw=="
    }
]
Run Code Online (Sandbox Code Playgroud)

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

Cha*_* Xu 8

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

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

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

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

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

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"
Run Code Online (Sandbox Code Playgroud)

更新

根据你说的,你好像是在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"
Run Code Online (Sandbox Code Playgroud)

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


Pet*_*sma 7

我创建了一个 Azure Powershell 任务(版本 4),它执行以下操作:

az login -u **** -p ****
Write-Host "##vso[task.setvariable variable=storageKey;]az storage account keys list -g ***** -n ***** --query [0].value -o tsv"
$key = az storage account keys list -g ***** -n **** --query [0].value -o tsv
Write-Host "##vso[task.setvariable variable=something;]$key"
Run Code Online (Sandbox Code Playgroud)

something然后我可以在 Azure CLI 任务中使用该变量:

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

这有效。不过,您可能需要将 -u 和 -p 放入变量中。

@Charles 非常感谢这句话(az storage account keys list -g **** -n ****estx --query [0].value -o tsv)