无法使用 Java 2.x 的 AWS SDK 将新的备用域名添加到 CloudFront 资源

Can't add new alternative domain name to CloudFront resource using AWS SDK for Java 2.x

我在尝试使用适用于 Java v2.x

的 AWS 开发工具包向现有 CloudFront 资源添加新的备用域名 (CNAME) 时遇到困难

这是我目前使用的代码片段:

// First I get the actual resource from AWS
GetDistributionResponse distributionInformation = cloudFrontclient
        .getDistribution(GetDistributionRequest.builder().id(input.getDistributionId())
        .build());

// Then I extract the part I want to edit
DistributionConfig config = distributionInformation.distribution().distributionConfig();

// so far so good, I'm able to see my data as intended 

// The next thing is to try adding the new alias, and of course I can't as that array is Unmodifiable! 
// Meaning that I'm  always getting an: java.lang.UnsupportedOperationException
config.aliases().items().add(input.getAlternativeDomain()); 

// If the previous line worked or I find an alternative solution I'm planning to make the following update request
UpdateDistributionRequest updateDistributionRequest = UpdateDistributionRequest
                .builder()
                .distributionConfig(config)
                .build();

cloudFrontclient.updateDistribution(updateDistributionRequest);

我有点迷路了,我不确定这应该如何工作。

如果能得到任何帮助,我将不胜感激

提前致谢

我确认属于 DistributionConfig 的方法 - 甚至注释 - 当您使用从 distributionConfig[=27] 返回的对象时似乎是只读的=]

    Distribution disObject = response.distribution();
    DistributionConfig config = disObject.distributionConfig();

解决方案是使用 builder 方法创建一个新的 DistributionConfig 对象(见下文)。添加新值,然后也读入未更改的值。否则抛出 Java 异常。

这里我添加一条新评论作为修改Distribution的例子

public static void main(String[] args) {

        CloudFrontClient cloudFrontClient = CloudFrontClient.builder()
                .region(Region.AWS_GLOBAL)
                .build();

        try {

            // Lets get the Distribution to modify
            GetDistributionRequest disRequest = GetDistributionRequest.builder()
                    .id("E90U7J6Pxxxxx")
                    .build();

            GetDistributionResponse response = cloudFrontClient.getDistribution(disRequest);
            Distribution disObject = response.distribution();
            DistributionConfig config = disObject.distributionConfig();

            // Create a new  DistributionConfig object and add new values to comment and aliases
            DistributionConfig config1 = DistributionConfig.builder()
                    .aliases(config.aliases()) // You can pass in new values here
                    .comment("New Comment")
                    .cacheBehaviors(config.cacheBehaviors())
                    .priceClass(config.priceClass())
                    .defaultCacheBehavior(config.defaultCacheBehavior())
                    .enabled(config.enabled())
                    .callerReference(config.callerReference())
                    .logging(config.logging())
                    .originGroups(config.originGroups())
                    .origins(config.origins())
                    .restrictions(config.restrictions())
                    .defaultRootObject(config.defaultRootObject())
                    .webACLId(config.webACLId())
                    .httpVersion(config.httpVersion())
                    .viewerCertificate(config.viewerCertificate())
                    .customErrorResponses(config.customErrorResponses())
                    .build();


                UpdateDistributionRequest updateDistributionRequest = UpdateDistributionRequest.builder()
                    .distributionConfig(config1)
                    .id(disObject.id())
                    .ifMatch(response.eTag())
                     .build();

            cloudFrontClient.updateDistribution(updateDistributionRequest);


        } catch (CloudFrontException e){
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
    }
}

这有效,您可以看到新评论:

替代方法...

    DistributionConfig.Builder newConfigBuilder = configResponse.distributionConfig().toBuilder()
    
    //Do what you want with newConfigBuilder
    
    ...
    
    ...
    
    
    DistributionConfig newConfig = newConfigBuilder.build();

https://sdk.amazonaws.com/java/api/latest/