aws-samples / image-optimization

Simple, performant and cost efficient solution for optimizing images using Amazon CloudFront, Amazon S3 and AWS Lambda
MIT No Attribution
195 stars 116 forks source link

Using Next image with presigned urls for certain path patterns #43

Closed RahulBirCodes closed 4 months ago

RahulBirCodes commented 4 months ago

Hi!

So in my cloudfront distro, I set cache behaviors so that certain paths to resources in my bucket are protected; however, Im having the following problem now: I can only sign the urls after the query params are added but the next image component adds the query param to the final url I send to the image in the loader. I looked into presigned cookies however for my use case i need to have fine control over individual resource access and each cookie only allows me to provide one resource in its policy. Now Im thinking that I need to create a custom image component that determines the quality and width and then sends a request to my server to generate a signed url with those params to use. Only thing is, I would give up all the default behavior of the Next image component and would likely have to rewrite most of those features myself in this implementation. Is there any other way i could go about this?

achrafsouk commented 4 months ago

If i understood you correctly, can you check the usage of wildcard in the signing policy? It allows you to use the same signature with all variants of an image (independently from the the query string parameters)

https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-creating-signed-url-custom-policy.html

RahulBirCodes commented 4 months ago

Yeah that's what I ended up using, missed that in the docs the first time. Thanks!