简体   繁体   中英

Cloudfront missing cache control

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

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:

  1. Log in to AWS Management Console
  2. Go into S3 bucket
  3. Select all files clicking on the checkbox near "Name"
  4. Click on "Actions" and then select "Edit metadata"
  5. "Add metadata" --> Specify Type as System defined, Key as Cache-Control and Value as max-age=31536000
  6. Press "Edit Metadata" button

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM