如何在 Java 中获取和更新 Azure 生命周期规则

Posted

技术标签:

【中文标题】如何在 Java 中获取和更新 Azure 生命周期规则【英文标题】:How to get and update azure lifecycle rules in java 【发布时间】:2021-04-22 10:55:09 【问题描述】:

我在以下代码的帮助下创建了 Azure 存储帐户的生命周期策略。

StorageManager manager = StorageManager.authenticate(credential, subId); //credentilas and subscription iD
   manager.managementPolicies()
          .define("testprefix")
          .withExistingStorageAccount(resourceGroup, storageAcc)
          .defineRule("testprefixrulenew")
          .withLifecycleRuleType()
          .withBlobTypeToFilterFor(BlobTypes.BLOCK_BLOB)
          .withPrefixToFilterFor("prefix1")
          .withDeleteActionOnBaseBlob(1)
          .attach()
          .create();

当我运行此程序时,存储帐户的所有现有生命周期规则都将被删除,只有新创建的规则存在。

1) 如何避免删除使用 java 的 azure 存储帐户的现有生命周期规则?

另外,当我尝试使用以下代码 sn-p 获取现有生命周期策略时。

ManagementPolicyInner managementPolicy1 =
        manager2
            .inner()
            .managementPolicies()
            .getAsync(resourceGroup, storageAcc)
            .toBlocking()
            .last();

我只能获得最后修改/创建的策略。如果我们删除“last()”,它会给出 Observable 值,所以。

2) 如何获取现有策略,对其进行迭代,并在需要时使用 java 更新其中一个???

【问题讨论】:

【参考方案1】:

如何避免删除 azure 的现有生命周期规则 使用 java 的存储帐户?

关于这个问题,我们可以使用com.azure.resourcemanager.storage.implementation.StorageManagementClientImpl类来实现它。更多详情请参考here

例如

String clientId="<the service principal client id>";
        String clientSecret="<the service principal client secret>"";
        String tenant="";
        String subId="";
        AzureProfile profile = new AzureProfile(tenant,subId,AzureEnvironment.AZURE);
        TokenCredential credential = new ClientSecretCredentialBuilder()
                .clientId(clientId)
                .clientSecret(clientSecret)
                .authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
                .tenantId(tenant)
                .build();

        StorageManagementClientImpl storageManagementClient = new StorageManagementClientBuilder()
                .pipeline(HttpPipelineProvider.buildHttpPipeline(credential,profile))
                .endpoint(profile.getEnvironment().getResourceManagerEndpoint())
                .subscriptionId(profile.getSubscriptionId())
                .buildClient();

        ManagementPolicyInner policyInner= storageManagementClient.getManagementPolicies()
                .get("andywin7","andyprivate",ManagementPolicyName.DEFAULT);

        ManagementPolicyDefinition definition = new ManagementPolicyDefinition()
                .withActions( new ManagementPolicyAction().withBaseBlob(
                        new ManagementPolicyBaseBlob().withTierToCool(
                                new DateAfterModification().withDaysAfterModificationGreaterThan(180))))
                .withFilters(new ManagementPolicyFilter().withBlobTypes(Arrays.asList("blockBlob")));

        ManagementPolicyRule newRule = new ManagementPolicyRule()
                .withName("changeTier")
                .withEnabled(true)
                .withType(RuleType.LIFECYCLE)
                .withDefinition(definition);
        policyInner.policy().rules().add(newRule);
        Response<ManagementPolicyInner>  res =storageManagementClient.getManagementPolicies().createOrUpdateWithResponse(
                "andywin7","andyprivate",ManagementPolicyName.DEFAULT,
                policyInner.policy(), Context.NONE);

如何获取现有策略、迭代它们并更新其中之一 如果需要使用 java?

请参考以下代码 列表

String clientId="<the service principal client id>";
        String clientSecret="<the service principal client secret>"";
        String tenant="";
        String subId="";
        AzureProfile profile = new AzureProfile(tenant,subId,AzureEnvironment.AZURE);
        TokenCredential credential = new ClientSecretCredentialBuilder()
                .clientId(clientId)
                .clientSecret(clientSecret)
                .authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
                .tenantId(tenant)
                .build();

        StorageManager manager = StorageManager.authenticate(credential,profile);
        ManagementPolicy managementPolicy = manager.managementPolicies()
                .getAsync("andywin7","andyprivate")
                .block();
        ObjectMapper mapper = new ObjectMapper();

        for ( ManagementPolicyRule rule : managementPolicy.policy().rules())

            System.out.println(mapper.writeValueAsString(rule));
        

更新

String clientId="<the service principal client id>";
        String clientSecret="<the service principal client secret>"";
        String tenant="";
        String subId="";
        AzureProfile profile = new AzureProfile(tenant,subId,AzureEnvironment.AZURE);
        TokenCredential credential = new ClientSecretCredentialBuilder()
                .clientId(clientId)
                .clientSecret(clientSecret)
                .authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
                .tenantId(tenant)
                .build();

        StorageManager manager = StorageManager.authenticate(credential,profile);
        ManagementPolicy managementPolicy = manager.managementPolicies()
                .getAsync("andywin7","andyprivate")
                .block();
        managementPolicy.update()
                .updateRule("<the rule name you want to update>")
                     ... the action you want to update
                .parent()
                .withoutRule("<the rule name you do not want to update>")
                .apply();

更多详情请参考here


更新

现有规则如何更新,请参考fowwing代码

        StorageManager manager = StorageManager.authenticate(credential,profile);
        ManagementPolicy managementPolicy = manager.managementPolicies()
                .getAsync("andywin7","andyprivate")
                .block();
        ObjectMapper mapper = new ObjectMapper();

        for ( ManagementPolicyRule rule : managementPolicy.policy().rules())
               System.out.println(mapper.writeValueAsString(rule));

               if(rule.name().equals("deleteAppend"))
                  int i= managementPolicy.policy().rules().indexOf(rule);
                   ManagementPolicyRule newRule= managementPolicy.policy().rules().get(i);
                   managementPolicy.policy().rules().remove(rule);
                   newRule.definition()
                           .actions()
                           .withBaseBlob(new ManagementPolicyBaseBlob().withDelete(new DateAfterModification().withDaysAfterModificationGreaterThan(18) ));
                   managementPolicy.policy().rules().add(newRule);
               


        

        managementPolicy.update()
                .withPolicy(managementPolicy.policy())
                .apply();

【讨论】:

你能告诉我你使用的是哪个 gradle 或 maven 依赖吗??? 非常感谢@Jim Xu 的解决方案。我使用了不正确的依赖项,因此 block() 出现错误,现在已解决。但面对另一个问题,在更新现有政策时,我已经给出了这个 - .updateRule("rule1")withoutRule("Rule2") 这里,Rule1 得到了更新,但 Rule2 和其他 Rule3 被删除了。是 update() 的行为,还是我做错了什么?? @DeveloperSSP208 你好。我犯了一个错误。 withoutRule 方法将删除规则。关于如何更新,请参考我的更新。 嗨@Jim Xu,在创建或更新生命周期规则时,如果不存在生命周期规则,manager.managementPolicies() .getAsync("andywin7","andyprivate")storageManagementClient.getManagementPolicies() .get("andywin7","andyprivate",ManagementPolicyName.DEFAULT) 将给出 "ManagementPolicyNotFound" 异常对于存储帐户,您能否建议如何处理? @DeveloperSSP208 我认为您可以使用trycatch 来捕获异常。

以上是关于如何在 Java 中获取和更新 Azure 生命周期规则的主要内容,如果未能解决你的问题,请参考以下文章

Azure Functions with java - 如何获取写入函数的日志?

如何在Azure中使用Java获取VM的串行日志

如何获取 Azure APIM 健康检查和详细信息

如何在不创建或附加 Azure Java SDK 的情况下获取 AppendBlobItem

java - 如何从Java日历中获取指定周的星期六日期? [关闭]

如何在 Azure blob 下载中获取 blob 下载进度