删除给定 URI 的 Azure 存储 blob

Delete Azure storage blob given a URI

我正在编写一个删除 Azure 映像及其底层存储 blob 的清理脚本。我可以使用以下命令找到图像的存储 blob:

az image list --query "[?name=='$IMAGE_NAME'] | [].storageProfile.osDisk.blobUri"

(这是 bash,因此 $IMAGE_NAME 被替换为实际的图像名称)。上述命令的输出是一个 JSON URI 列表,每个看起来像这样:

https://storage_account.blob.core.windows.net/container_name/blob_name.vhd

查看 az storage blob delete 的文档,我知道可以使用如下命令删除此 blob:

az storage blob delete --account-name storage_account --container container_name --name blob_name.vhd

所以,显然我可以解析 URI,然后生成此命令。然而,这似乎很奇怪:如果您不能使用它们,那么给 blob 一个 URI 有什么意义?

所以我的问题是:

  1. 是否有直接的 az cli 命令使用其 URI 删除 blob?
  2. 更好的是,有没有办法删除与给定 Azure 图像关联的 blob?

没有内置的 CLI 命令可以直接用 blob url 删除。有一个解决方法可以使用 az rest to call the Delete Blob REST API.

access_token = $(az account get-access-token --resource https://storage.azure.com/ --query accessToken -o tsv)
now = $(env LANG=en_US TZ=GMT date '+%a, %d %b %Y %T %Z')
headers = "Authorization=Bearer "+$access_token+" x-ms-date="+$now+" x-ms-version=2020-08-04"
az rest --method delete --uri $blob_url --headers $headers