[英]How to invalidate cache in Cloudfront using Java SDK
I am trying to invalidate cache in AWS cloudfront using the JAVA SDK but I am finding it a nightmare to find the relevant information. 我正在尝试使用JAVA SDK使AWS cloudfront中的缓存无效,但我发现找到相关信息是一场噩梦。 I already created the project and I am trying to figure out how to use
com.amazonaws.services.cloudfront.AmazonCloudFrontClient
to connect to cloudfront and call the invalidate api. 我已经创建了该项目,我正在尝试弄清楚如何使用
com.amazonaws.services.cloudfront.AmazonCloudFrontClient
连接到cloudfront并调用invalidate api。
com.amazonaws.services.cloudfront.AmazonCloudFrontClient
I found an answer to a question similar to mine back in 2016 that recommended the following approach: 我在2016年找到了类似于我的问题的答案,建议采用以下方法:
AWSCredentials awsCredentials = new DefaultAWSCredentialsProviderChain().getCredentials();
AmazonCloudFrontClient client = new AmazonCloudFrontClient(awsCredentials);
Paths invalidation_paths = new Paths().withItems("/path/to/invalidate/foo.jpg", "/path/file2.txt").withQuantity(2);
InvalidationBatch invalidation_batch = new InvalidationBatch(invalidation_paths, "unique_id_like_a_date");
CreateInvalidationRequest invalidation = new CreateInvalidationRequest("distributionID", invalidation_batch);
CreateInvalidationResult ret = client.createInvalidation(invalidation);
However some of these classes are now deprecated and/or non-existent anymore. 但是,这些类中的一些现在已经被弃用和/或不再存在。
Can someone please help with the correct way to invoke invalidation API in Cloudfront via JAVA? 有人可以帮助您通过JAVA在Cloudfront中调用失效API的正确方法吗?
To invalidate files, you can specify either the path for individual files or a path that ends with the * wildcard, which might apply to one file or to many, as shown in the following examples: 要使文件无效,您可以指定单个文件的路径或以*通配符结尾的路径,该路径可能适用于一个文件或多个文件,如以下示例所示:
/images/image1.jpg
/images/image*
/images/*
Note 注意
If you use the AWS command line interface (CLI) for invalidating files and you specify a path that includes the * wildcard, you must use quotes (") around the path. 如果使用AWS命令行界面(CLI)使文件无效并指定包含*通配符的路径,则必须在路径周围使用引号(“)。
For example: 例如:
aws cloudfront create-invalidation --distribution-id $CDN_DISTRIBUTION_ID --paths "/*"
I successfully invalidated the cache of certain paths with AWS Java SDK 2.x with this: 我使用以下方法成功地使用AWS Java SDK 2.x使某些路径的缓存无效:
Paths invalidationPaths = Paths.builder()
.items("/thing.txt", "/foo/bar/*")
.quantity(2)
.build();
InvalidationBatch invalidationBatch = InvalidationBatch.builder()
.paths(invalidationPaths)
.callerReference("arcones")
.build();
CreateInvalidationRequest createInvalidationRequest = CreateInvalidationRequest.builder()
.distributionId(distributionID)
.invalidationBatch(invalidationBatch)
.build();
cloudFront.createInvalidation(createInvalidationRequest);
Take in mind that the invalidation is asynchronous, so it will be issued to your CloudFront distribution when you run this and will take a while to be processed (you can notice that the invalidation has finished when the status becomes Completed
). 请记住,失效是异步的,因此当您运行此失效时,它将发布到您的CloudFront分配,并且需要一段时间才能处理(您可以注意到状态变为已完成时失效已
Completed
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.