Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup custom response header in AWS S3

I want to take my application to offline mode, and I want to register a service worker. My files are hosted in AWS S3 and I've created worker.js in the root of my bucket. I may need to return custom header "Service-Worker-Allowed" in response from AWS S3 for worker.js Currently custom headers are requiring to have x-amz-meta headers, which I can not use. Are there anyway to add that header by using Cloudfront or Cloudfare? Or any other suggestions?

like image 214
user2757853 Avatar asked Aug 15 '16 05:08

user2757853


3 Answers

You can not set custom response headers with s3 or cloudfront.

You have 2 possible workarounds when using AWS.

  1. Proxy your requests. I.e. using nginx/haproxy running on ec2
  2. Using lambda@edge.

Option 2, lambda@edge is a simpler solution. It is designed to run a lambda function from a cloudfront edge node. You can use lambda@edge to run custom javascript code. You can configure your code to run before a http request is sent to cloudfront or after a http response is received from cloudfront. This allows you to set custom request or response headers. See https://medium.com/@tom.cook/edge-lambda-cloudfront-custom-headers-3d134a2c18a2 for a good write-up on how to do this.

like image 119
Learner Avatar answered Oct 26 '22 04:10

Learner


In AWS S3, click on object and go to its settings, there Metadata section where you can add custom headers. Simply click on add metadata.

This can also be done programmatically while uploading data. All api supports additional metadata.

like image 41
hridayesh Avatar answered Oct 26 '22 03:10

hridayesh


It's not possible at the moment. S3 only allows you to add header by prefix x-amz-meta and some standard header for caching.

like image 29
Reza Mousavi Avatar answered Oct 26 '22 04:10

Reza Mousavi