无法在 Azure 上的存储管理策略中添加新规则

问题描述

我正在使用 az cli 添加一个 storage management-policy删除容器中修改时间超过 7 天的 blob。
这是 policy.json 文件

  "rules": [
    {
      "name": "expirationRule1","enabled": true,"type": "Lifecycle","deFinition": {
        "filters": {
          "blobTypes": [ "blockBlob" ],"prefixMatch": [ "container1" ]
        },"actions": {
          "baseBlob": {
            "delete": { "daysAfterModificationGreaterThan": 7 }
          }
        }
      }
    }
  ]
}

我使用以下命令创建此生命周期管理策略:

az storage account management-policy create --account-name <my_acc_name> --policy <policy_file> --resource-group <my_res_group>

这一步成功了。现在我想在不同的容器上添加一个策略。 policy.json 保持不变,prefixMatch 更改为 container2name 更改为 expirationRule2。现在,当我使用上述相同的命令应用此新策略时,我看不到应用的旧策略,而只能看到新策略。
以下是步骤:

$az storage account management-policy create --account-name resacc1 --resource-group resgrp1 --policy /tmp/azure_lifecycle.json

{
  "id": "<some_id_here>","lastModifiedTime": "2021-05-10T10:10:32.261245+00:00","name": "DefaultManagementPolicy","policy": {
    "rules": [
      {
        "deFinition": {
          "actions": {
            "baseBlob": {
              "delete": {
                "daysAfterLastAccesstimeGreaterThan": null,"daysAfterModificationGreaterThan": 7.0
              },"enableAutoTierToHotFromCool": null,"tierToArchive": null,"tierToCool": null
            },"snapshot": null,"version": null
          },"filters": {
            "blobIndexMatch": null,"blobTypes": [
              "blockBlob"
            ],"prefixMatch": [
              "container1"                                << container1 is prefixMatch
            ]
          }
        },"name": "expirationRule1","type": "Lifecycle"
      }
    ]
  },"resourceGroup": "resgrp1","type": "Microsoft.Storage/storageAccounts/managementPolicies"
}

现在我为 container2 添加新策略:

$ az storage account management-policy create --account-name resacc1 --resource-group resgrp1 --policy /tmp/azure_lifecycle.json
{
  "id": "<some_id_here>","lastModifiedTime": "2021-05-10T10:11:54.622184+00:00","prefixMatch": [
              "container2"                                    << container2 in prefixMatch
            ]
          }
        },"name": "expirationRule2","type": "Microsoft.Storage/storageAccounts/managementPolicies"
}

现在应用 2 条规则后,当我执行 show 命令时,它只显示单个策略应用于存储帐户。

$ az storage account management-policy show --account-name resacc1 --resource-group resgrp1
{
  "id": "<some_id_here>","prefixMatch": [
              "container2"
            ]
          }
        },"type": "Microsoft.Storage/storageAccounts/managementPolicies"
}

有人可以帮助我了解如何将新规则附加到现有策略或一起创建新策略,以便我将这两个规则应用于我的存储帐户中的容器。

解决方法

查看 AZ CLI 文档,您唯一可用的选项是 creating a new policyupdating an existing policy (i.e. replacing a policy completely)。没有可用于向现有策略添加规则的命令。

您看到这种行为的原因是因为您正在更新覆盖先前政策内容的整个政策。

您需要做的是修改您的 policy.json 文件并包含这两个规则,然后更新存储帐户上的策略。或者您可以使用 az storage account management-policy show 获取现有政策,解析政策 JSON,添加新规则,然后使用 az storage account management-policy update 更新政策。