Sign In
Sign In

Setting Object Headers

Updated on 26 November 2024

This guide explains how to set headers for S3 objects using s3cmd and aws s3api. This is useful for managing caching, adding custom metadata, and optimizing bucket data handling.

Setting Headers via Control Panel

  1. Go to the S3 storage section and click on the bucket.
  2. In the Objects tab, click on the three dots next to the object and select Edit metadata.

36d71125 Cdfd 41b2 9810 540c7c9b4883

  1. Add necessary headers. To set up a custom header, click +Add.

Bacf7b83 9ffc 4c5b Ac07 5dd516c23d0d

Setting Headers via CLI

Another way to configure headers is to use utilities like s3cmd or s3api.

Setting a Header for a Single Object

To set a header for a specific object, use the s3cmd modify command. For example, to add a Cache-Control header to a specific object:

s3cmd modify s3://<bucket_name>/<object_name> --add-header="cache-control:max-age=7000"

In this case, the object will receive the Cache-Control header with the max-age=7000 parameter, instructing browsers or proxy servers to cache the object for 7000 seconds.

Setting Headers for All Objects in a Bucket

To set headers for all objects in a bucket, you can use the --recursive option to apply changes to all files:

s3cmd modify --recursive s3://<bucket_name> --add-header="cache-control:max-age=7001"

This command recursively adds the Cache-Control header with the value max-age=7001 to all objects in the bucket.

Creating a Custom Header

To add custom headers, use the s3cmd modify command with the x-amz-meta prefix:

s3cmd modify s3://<bucket_name>/<object_name> --add-header="x-amz-meta-test-header:test"

This creates a custom header x-amz-meta-test-header with the value test. In the object's metadata, the header will appear without the x-amz-meta- prefix, resulting in:

"Metadata": {
   "test-header": "test"
}

This allows you to store additional data in the object, which can be useful for processing or logging.

Verifying Set Headers

To verify the headers, use the aws s3api head-object command. This ensures that all headers and metadata have been successfully added:

aws s3api head-object --bucket <bucket_name> --key <object_name> --endpoint-url https://s3.hostman.com

The command will return information about the object, including its metadata and headers, allowing you to confirm that the changes were applied correctly.

Was this page helpful?
Updated on 26 November 2024

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start.
Email us
Hostman's Support