Reputation: 470
After uploading all my files to a AWS S3 bucket, I have realized that the cache control header is missing. I have set up my cloudfront distribution in this way:
Afterwards I have invalidated all my files and waited until it is completed. However I don't get any Cache-Control header:
HTTP/1.1 200 OK
x-amz-id-2: fnvFKqf8aeykSWBnQAPlj6FE7JvZSPXHdyOosjce6PyMTr7FXQsSaUflKlJuMv+RBmvaErjsLUY=
x-amz-request-id: 890141931E821C87
Date: Tue, 09 Mar 2021 16:27:03 GMT
Last-Modified: Tue, 09 Mar 2021 16:25:04 GMT
ETag: "444ec6f04c020fe4dbd43b17e1c9baba"
Cache-Control: max-age=15552000
x-amz-version-id: q8R8DSCtnLai4D70p1csuwXiEZXaFQtv
Accept-Ranges: bytes
Content-Type: image/jpeg
Content-Length: 206070
Server: AmazonS3
Upvotes: 0
Views: 2014
Reputation: 23
The response from @retroriff works but if you have some CI/CD during the deployment will need to add it there. So another way I solve this problem was by using a CloudFront function on the response header in the behavior of the distribution.
Here some example of cache-control function: https://docs.aws.amazon.com/pt_br/AmazonCloudFront/latest/DeveloperGuide/example-function-add-cache-control-header.html
Upvotes: 0
Reputation: 470
If you want 'cache-control' header in the Response Headers of CloudFront, then you will have to specify this metadata for each object stored in S3 bucket, because CloudFront does not return this header specifically.
In order to bulk edit metadata for all the existing files using AWS Console, you can follow the below given steps:
To add Metadata from AWS CLI, please run the below given command:
aws s3 cp s3://bucket-name/ s3://bucket-name/ --cache-control max-age=31536000 --recursive
By default S3 does not add cache-control headers to all the objects which are uploaded to a S3 bucket, but while uploading a file, you can specify the metadata to add cache-control header. You can run the below given AWS CLI command for the same:
aws s3 cp object-name s3://bucket-name/object-name --cache-control max-age=31536000
If you want to add some other user-defined metadata as well, then you can run the below given command:
aws s3 cp object-name s3://bucket-name/object-name --cache-control max-age=31536000 --metadata="test=1,example=2"
Upvotes: 2