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년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인