Adding custom header to S3

0

Hi there,

I'm using an S3 bucket to server static resources through CloudFront and I'm struggling figuring out how to add custom headers with canonical tags for specific objects inside this bucket. What I'm trying to implement is this rule that I previously applied to our Apache webservers via .htaccess file:

<Files "some-example-file.pdf">
Header add Link "<https://example.com/friendly-URL/>; rel=\"canonical\""
</Files>

Any way to achieve that using Lambda@Edge?

Thanks!

1 個回答
1

Yes, this could be done via Lambda@Edge. There are a few different ways that you could approach this, but I think the most straightforward would be as follows:

  • Add user-defined metadata to the S3 Objects for which you want to return the rel=canonical header. eg. x-amz-meta-canonical: https://example.com/friendly-URL
  • Create a Lambda@Edge function on the Origin Response trigger. This means your function will only be invoked in the event of a cache miss, and the modified version of the object will be stored in cache for future requests.
  • The function should look for the x-amz-meta-canonical header, read the value, then write a new rel="canonical" header in the correct format. You could choose to remove the x-amz-meta-canonical header if you wish

Documentation:

An alternative approach would be to keep a mapping of the source object and the value of the rel=canonical header (if any) separate to the objects themselves, and simply look this up in your Lambda@Edge function. There are various methods for doing this, as outlined in this blog post: https://aws.amazon.com/blogs/networking-and-content-delivery/leveraging-external-data-in-lambdaedge/

However, I think this adds unnecessary complexity, and it is probably easier to store the mapping in the object metadata.

AWS
專家
Paul_L
已回答 2 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南