Tobias T
06/02/2022, 4:00 PMTobias T
06/03/2022, 9:49 AMTobias T
06/03/2022, 9:57 AMconst imageBucket = new Bucket(stack, "ImageBucket", {
cdk: {
bucket: {
publicReadAccess: true,
},
},
});
const distribution = new cloudfront.Distribution(stack, "ImageDistribution", {
defaultBehavior: {
origin: new origins.S3Origin(imageBucket.cdk.bucket),
viewerProtocolPolicy: cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
cachePolicy: cloudfront.CachePolicy.CACHING_OPTIMIZED,
},
});
This seems to be working..Tanner Bindrup
06/03/2022, 3:57 PMcachePolicy
. Not sure if it's interesting to you or not, but we use cloudfront distributions in front of our buckets in order to let us turn publicReadAccess
off and still have the items in the bucket be accessible. I think you just need to make sure you have a CORS policy set on the bucket:
import * as S3 from "aws-cdk-lib/aws-s3";
const bucket = new Bucket(stack, "*****-assets", {
name: `assets-*****-${stage}`,
cdk: {
bucket: {
cors: [
{
allowedHeaders: ["*"],
allowedMethods: [S3.HttpMethods.PUT, S3.HttpMethods.GET],
allowedOrigins: [
`<https://app>${isProd ? "" : "." + stage}.*****.com`,
`<https://admin>${isProd ? "" : "." + stage}.*****.com`,
"<http://localhost:3000>",
"<http://localhost:3001>",
"<https://console.serverless-stack.com/>"
],
exposedHeaders: []
}
]
}
}
});
Frank
Frank