How To Set S3 Headers

Hi guys. I’ve been trying to set the headers e.g. Cache-Control, Expires, etc when using s3_sync and have been unable to do so. If someone could possibly post a config that’s implementing it I’d greatly appreciate it.

Thanks!

There are a couple of gotchas when using the S3 sync command in the CLI.

–metadata (map) A map of metadata to store with the objects in S3. This will be applied to every object which is part of this request. In a sync, this means that files which haven’t changed won’t receive the new metadata. When copying between two s3 locations, the metadata-directive argument will default to ‘REPLACE’ unless otherwise specified.

This is the command that I run that seems to be ok:

aws s3 sync . s3://bucketname --metadata-directive REPLACE --expires 2034-01-01T00:00:00Z --cache-control 'max-age=315360000,public' --acl public-read --exact-timestamps --delete

If you also run cloudfront you can invalidate using the CLI as well. As Middleman generates a static site, the HTML gets cached too. I hash my asset filenames so I only need to invalidate the HTML (because invalidation can be expensive on AWS)

aws cloudfront create-invalidation --distribution-id 00000000000000 --paths '/**/*.html'

I hope this helps.