Skip to content
Advertisement

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

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement