繁体   English   中英

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

[英]Retrieve Azure storage account key using Azure CLI

在发布管道中,我使用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"

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

我创建了一个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"

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

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

这可行。 您可能需要将-u和-p放在变量中。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM