download free 30 days trial version buy bucket explorer
Documentation  Download  Purchase  Support  FAQs   Forum   ScreenShots & Demos

How to Set Cache Control Header for Amazon S3 Object?

Cache Control Metadata (Custom HTTP Header) drives the Amazon S3 Object behavior when the S3 Object (File) is requested through a browser. Cache Control can lower the number of access requests to S3 Objects, hence can reduce Amazon S3 usage cost. With the use of this custom header, you can access the content which already exists, from the browser cache. If you add S3 Custom Headers Cache-control and Expiration Time, the S3 objects are loaded from cache without requesting from S3 for better performance and reduced cost.  Cache control will access the object from cache up to a specified time limit set by you. By default, it is 24 hours for every object.

Every Metadata setting contains a Key-Value pair. Cache control metadata key is “Cache-Control” and Value is “max-age=<time for which you want your object to be accessed from cache in seconds>”

You can set Cache Control Custom Header for Amazon S3 Objects  by sending HTTP PUT Request to Amazon S3 Server with appropriate headers in two ways:

  1. Set Cache Control Metadata using Amazon S3 REST API PUT Object Request - If you are a programmer, you can write your own software program to use Amazon S3 REST or SOAP APIs to set Custom Headers with PUT Object Request. This website only refers to Amazon S3 REST APIs, please refer to AWS documentation website for details on how to use SOAP APIs.
  2. Set Cache Control Metadata using Bucket Explorer User Interface - If you like to set custom HTTP Headers like Cache Control using mouse clicks instead of writing a software program, you can use Bucket Explorer's user interface for that.

With this Custom HTTP Header, you can specify the caching behavior that must be followed with the request/response chain and to prevent caches from interfering with the request or response.

General categories for cache-control directives:
  • Restrictions on cacheable files which may only be imposed by the origin server.
  • Restrictions on what can be stored by a cache which can be imposed by the origin server or user agent.
  • Modifications on the basic expiration mechanism imposed by either the origin server or user agent.
  • Control on revalidation and reload imposed by a user agent.
  • Control on transformation of entities.
  • Extensions on the caching system

 

Set Cache Control Header using Amazon S3 API PUT Object Request

With this method, you can set Cache Control Header for Amazon S3 Object at the time of uploading the Object, using PUT Object Request in your program. You will need to mention cache-control in the Request header to specify the caching behavior.

 

Syntax:


PUT / ObjectName HTTP/1.1
Host: BucketName .s3.amazonaws.com
Date: date
x-amz-meta-Cache-Control : max-age= <value in seconds>
Authorization: signatureValue
 

 

Set Cache Control Header for S3 file using Bucket Explorer:

You can set Metadata on Amazon S3 Objects like Cache-Control and others using Bucket Explorer. Bucket Explorer provides three different ways to set Metadata.

 

Follow the steps below to set Cache-Control as Metadata on S3 using Bucket Explorer:

Case A: Set Cache-Control while uploading using Upload with Custom Header:
  1. Run Bucket Explorer and login with your credentials.
  2. After listing all Buckets, select any S3 Bucket.
  3. Select files from the native file system (from left tree panel).
  4. Right click on files and select “Upload with Custom Header”.
  5. Now, custom header window will appear.
  6. Click on Add button to add custom header. Enter Key: “Cache-Control” with Value: “max-age = (time for which you want your object to be accessed from cache in seconds)” e.g. value = “max-age = 1296000”
  7. Add custom header and now, file is uploaded with custom header.
Case B: Set Cache-Control for each file being uploaded using Bucket Default:
By using this method, you can set the Cache-Control by default on every object uploaded in the Bucket.
  1. Run Bucket Explorer and login with your credentials.
  2. After listing all Buckets, select any S3 Bucket.
  3. Click on Bucket Default tool button that exists on Bucket table.
  4. Select set option and add default custom header. Enter Key: “Cache-Control” with Value: “max-age = (time for which you want your object to be accessed from cache in seconds)”
  5. Set as Bucket Default.
  6. Select any file from local system and click on Upload button.
  7. Cache-Control metadata will be set on each newly uploaded file.
Case C: Set Cache-Control for already uploaded file on S3 using Update Metadata:
  1. Run Bucket Explorer and login with your credentials.
  2. After listing all Buckets, select any S3 Bucket.
  3. It will list all objects of selected S3 Bucket.
  4. Select any file and right click on the objects and select “Update Metadata” option.
  5. Add Key and Value in Metadata attributes. Enter Key: “Cache-Control” with Value: “max-age = (time for which you want your object to be accessed from cache in seconds)”
  6. Click on Save button. It will update metadata as Cache-Control on all selected S3 objects.

Example to set max-age: For time limit of 15 days = 3600 * 24 * 15 = 1296000 sec. Set Key = “Cache-Control” value = “max-age=1296000”

Note: If object is HTML file, set Key: “Cache-Control” and value: max-age = (time for which you want your object to be accessed from cache in seconds), must-revalidate “i.e. Key: “Cache-Control” value: max-age = “2592000, must re-validate” for 30 days. “must re-validate” string must be added after the time in second as value.