简体   繁体   English

如何使用Java SDK使Cloudfront中的缓存无效

[英]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的正确方法吗?

Invalidation 失效

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.

相关问题 如何通过 Java AWS SDK 使 Cloudfront CDN 提供的文件(要刷新)无效? - How to invalidate a file(to be refreshed) served from Cloudfront CDN via Java AWS SDK? 如何使用 REST API Java 使 Google CDN 缓存失效? - How to invalidate Google CDN cache using REST API Java? Java Grpc:使 dns 缓存无效 - Java Grpc: invalidate dns cache 如何在预定时间使番石榴缓存失效? - How to invalidate guava cache at scheduled time? Android-如何使用okhttp-retrofit从已保存的缓存中使特定的URL无效/删除。 - Android - How can I invalidate/remove specific URL s from saved cache using okhttp-retrofit.? 无法使用 AWS SDK 为 Java 2.x 添加新的备用域名到 CloudFront 资源 - Can't add new alternative domain name to CloudFront resource using AWS SDK for Java 2.x 在不影响性能的情况下,同时使Java中多个工作程序的缓存无效 - concurrently invalidate cache of several workers in java without hurting performances 缓存无效在Shiro中无效 - Cache Invalidate not working in Shiro 如何在使用 angular7 和 springboot 构建的 Web 应用程序中使浏览器缓存无效 - How to invalidate browser cache in a web application built with angular7 and springboot 如何将缓存无效消息广播到所有运行 Web 应用程序的服务器? - How to broadcast cache invalidate messages to all servers running a web app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM