Question:
I have an Amazon S3 bucket with about 300K objects in it and need to set the Cache-control header on all of them. Unfortunately it seems like the only way to do this, besides one at a time, is by copying the objects to themselves and setting the cache control header that way:
Is the documentation for the Amazon S3 CLI copy command but I have been unsuccessful setting the cache control header using it. Does anyone have an example command that would work for this. I am trying to set cache-control to max-age=1814400
Some background material:
Answer:
By default, aws-cli only copies a file’s current metadata, EVEN IF YOU SPECIFY NEW METADATA.
To use the metadata that is specified on the command line, you need to add the ‘–metadata-directive REPLACE’ flag. Here are some examples.
For a single file
1 2 3 |
aws s3 cp s3://mybucket/file.txt s3://mybucket/file.txt --metadata-directive REPLACE \ --expires 2100-01-01T00:00:00Z --acl public-read --cache-control max-age=2592000,public |
For an entire bucket:
1 2 3 |
aws s3 cp s3://mybucket/ s3://mybucket/ --recursive --metadata-directive REPLACE \ --expires 2100-01-01T00:00:00Z --acl public-read --cache-control max-age=2592000,public |
A little gotcha I found, if you only want to apply it to a specific file type, you need to exclude all the files, then include the ones you want.
Only jpgs and pngs
1 2 3 4 |
aws s3 cp s3://mybucket/ s3://mybucket/ --exclude "*" --include "*.jpg" --include "*.png" \ --recursive --metadata-directive REPLACE --expires 2100-01-01T00:00:00Z --acl public-read \ --cache-control max-age=2592000,public |
Here are some links to the manual if you need more info: