如何在 java 中获取和更新 azure 生命周期规则

How to get and update azure lifecycle rules in java

我在以下代码的帮助下为 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 存储帐户的现有生命周期规则?

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

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

我只能获得最后的 modified/created 保单。如果我们删除“last()”,它会给出 Observable 值,所以。

2) 如何获取现有策略,迭代它们,并在需要时使用 java 更新其中之一???

How to avoid this deletion of existing lifecycle rules for azure storage account using java?

关于这个问题,我们可以用classcom.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);

How to get the existing policies, iterate them, and update one of it if required using 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


更新

关于如何更新现有规则,请参考飞翼代码

        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();