How can I modify HTTP Headers for Objects in my Bucket?
Is there a way to change HTTP Response headers for images being served from object storage to specify how long the image should be cached by the browser?
You can do so using s3cmd to modify the metadata of either the entire bucket, or on a per object basis.
To make the change for all of the objects inside of the bucket you can do so by running the following command:
s3cmd --recursive modify --add-header=”Cache-Control: max-age=$TOTAL_SECONDS” s3://$BUCKET_NAME
To make the change on one file you can do so with the following command:
s3cmd modify --add-header=”Cache-Control: max-age=$TOTAL_SECONDS” s3://$BUCKET_NAME/$FILE_NAME
You can even use globbing to target files that have a particular extension to add the header:
s3cmd modify --add-header=”Cache-Control: max-age=$TOTAL_SECONDS” ‘s3://$BUCKET_NAME/*.png’
(Please note the single quotes surrounding the location of the bucket.)
$TOTAL_SECONDS, $BUCKET_NAME and $FILE_NAME are the number of seconds the browser should cache the file, the name of the bucket, and the name of the file respectively.