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

大憨熊 提交于 2020-01-14 10:49:28

问题


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. How to do it via Java SDK ?


回答1:


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.




回答2:


From AWS Documentation:

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. 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. If you exceed the limit, CloudFront returns an error message.

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.

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




回答3:


With the new AWS Java SDK 2.x, I have successfully invalidated some paths with this:

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




回答4:


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.

Please refer this link, http://jets3t.s3.amazonaws.com/toolkit/code-samples.html#cloudfront-invalidation



来源:https://stackoverflow.com/questions/28527188/how-to-invalidate-a-fileto-be-refreshed-served-from-cloudfront-cdn-via-java-aw

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!