简体   繁体   English

如何通过 Java AWS SDK 使 Cloudfront CDN 提供的文件(要刷新)无效?

[英]How to invalidate a file(to be refreshed) served from Cloudfront CDN via Java AWS SDK?

I am using Java SDK to uploaded images to S3, How do I invalidate a file in CloudFront so that it is refetched from s3 origin.我正在使用 Java SDK 将图像上传到 S3,如何使 CloudFront 中的文件无效,以便从 s3 源重新获取它。 How to do it via Java SDK ?如何通过 Java SDK 做到这一点?

import com.amazonaws.services.cloudfront;
import com.amazonaws.services.cloudfront.model.CreateInvalidationRequest;
import com.amazonaws.services.cloudfront.model.Paths;
import com.amazonaws.services.cloudfront.model.InvalidationBatch;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;

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

Note you can only have three concurrent invalidations;请注意,您只能有三个并发失效; an invalidation seems to take 10-30 minutes.失效似乎需要 10-30 分钟。

From AWS Documentation:从 AWS 文档:

You can have invalidation requests for up to 3,000 object URLs per distribution in progress at one time, and each invalidation request can include up to 3,000 object URLs.您一次最多可以对每个分发中的 3,000 个对象 URL 进行失效请求,并且每个失效请求最多可以包含 3,000 个对象 URL。 You can make as many invalidation requests as you want as long as you don't exceed this limit.只要不超过此限制,您就可以根据需要发出任意数量的失效请求。 For example, you can create 30 invalidations that invalidate 100 objects each, but as long as all 30 invalidations are still in progress, you can't create any more invalidations.例如,您可以创建 30 个失效,每个失效 100 个对象,但只要所有 30 个失效仍在进行中,您就不能创建更多失效。 If you exceed the limit, CloudFront returns an error message.如果超出限制,CloudFront 将返回错误消息。

It usually takes 10 to 15 minutes for CloudFront to complete your invalidation request, depending on the number of object URLs that you included in the request. CloudFront 通常需要 10 到 15 分钟来完成您的失效请求,具体取决于您在请求中包含的对象 URL 的数量。

http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Invalidation.html http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Invalidation.html

With the new AWS Java SDK 2.x , I have successfully invalidated some paths 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时失效已Completed )。

By Using Object Invalidation, we achieve this.通过使用对象失效,我们实现了这一点。

Object Invalidation - Invalidate objects in a distribution to force CloudFront to fetch the latest object data from the S3 origin.对象无效 - 使分发中的对象无效以强制 CloudFront 从 S3 源获取最新的对象数据。

Please refer this link, http://jets3t.s3.amazonaws.com/toolkit/code-samples.html#cloudfront-invalidation请参考此链接, http://jets3t.s3.amazonaws.com/toolkit/code-samples.html#cloudfront-invalidation

New update:新更新:

import com.amazonaws.services.cloudfront;
import com.amazonaws.services.cloudfront.model.CreateInvalidationRequest;
import com.amazonaws.services.cloudfront.model.Paths;
import com.amazonaws.services.cloudfront.model.InvalidationBatch;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;

AWSCredentials awsCredentials = new DefaultAWSCredentialsProviderChain().getCredentials();

AmazonCloudFrontClient client =

                (AmazonCloudFrontClient) AmazonCloudFrontClientBuilder.standard()
                        .withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).build();


Paths invalidation_paths = new Paths().withItems("/path/to/invalidate/foo.jpg", "/path/file2.txt").withQuantity(2);
// unique_id_like_a_date = "20201001090000".
InvalidationBatch invalidation_batch = new InvalidationBatch(invalidation_paths, "unique_id_like_a_date");
CreateInvalidationRequest invalidation = new CreateInvalidationRequest("distributionID", invalidation_batch);
CreateInvalidationResult ret = client.createInvalidation(invalidation);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Java SDK使Cloudfront中的缓存无效 - How to invalidate cache in Cloudfront using Java SDK 使用Java AWS S3客户端SDK和Cloudfront CDN端点从AWS S3下载映像 - Downloading an image from AWS S3 using java AWS S3 client sdk and cloudfront CDN end point 使用AWS Java SDK拒绝访问由链接到私有S3存储桶的Amazon CloudFront的安全签名URL提供的图像 - Access denied to images served by secure signed URLs of Amazon CloudFront linked to private S3 bucket using AWS Java SDK 如何使用 REST API Java 使 Google CDN 缓存失效? - How to invalidate Google CDN cache using REST API Java? 通过 AWS 从 S3 读取文本文件时出错 Java SDK - Errors in reading text file from S3 via AWS Java SDK 如何使用 IAM 角色通过 aws sdk (java) 从 ECS 容器调用 s3 存储桶 - How to call s3 bucket from ECS container via aws sdk (java) by using IAM role 如何通过AWS Java SDK Client从Auto Scale Group提取数据 - How to Extract Data From Auto Scale Group via AWS Java SDK Client 如何通过SDK Java在AWS API Gateway中启用CORS - How to enable CORS in AWS API Gateway via SDK Java Java AWS开发工具包-如何使用Java API中的IAM配置文件上传文件 - Java AWS SDK - How to upload file using IAM profile from Java API 通过AWS Java SDK设置EMR - Setup EMR via AWS Java SDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM