Skip to content

Latest commit

 

History

History
49 lines (36 loc) · 1.12 KB

InvalidateCloudfront.md

File metadata and controls

49 lines (36 loc) · 1.12 KB

Deleting files from S3 and CloudFront | Invalidate CloudFront Cache

https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-cloudfront/index.html

Install and import the CloudFront client

npm i @aws-sdk/client-cloudfront
import { CloudFrontClient, CreateInvalidationCommand } from "@aws-sdk/client-cloudfront"

Create a CloudFront object

const cloudfrontDistributionId = process.env.CLOUDFRONT_DISTRIBUTION_ID

const cloudfront = new CloudFrontClient({
  credentials: {
    accessKeyId: accessKey,
    secretAccessKey: secretAccessKey,
  }
});

Create an invalidation command

const cfCommand = new CreateInvalidationCommand({
  DistributionId: cloudfrontDistributionId,
  InvalidationBatch: {
    CallerReference: post.imageName,
    Paths: {
      Quantity: 1,
      Items: [
        "/" + post.imageName
      ]
    }
  }
})

const response = await cloudfront.send(cfCommand)