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 Risposta
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
ESPERTO
Paul_L
con risposta 2 anni fa

Accesso non effettuato. Accedi per postare una risposta.

Una buona risposta soddisfa chiaramente la domanda, fornisce un feedback costruttivo e incoraggia la crescita professionale del richiedente.

Linee guida per rispondere alle domande