[英]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.