deanmoses / tacocat-gallery-sam

Back end for Tacocat photo gallery implemented on Amazon AWS Serverless Application Model (SAM)
0 stars 0 forks source link

Download derived image with original filename, instead of "1024" #31

Closed deanmoses closed 11 months ago

deanmoses commented 11 months ago

When you save a derived image from the browser, it saves with the filename "1024".

The Issue

This happens because the URL ends in "1024":

https://img.pix.tacocat.com/i/2001/12-31/image.jpg/VERSION/jpeg/1024

The Solution

Change to some URL format that instead ends in "image.jpg".

The Challenge

S3 stores the derived images under a path like /2001/12-31/image.jpg/VERSION/jpeg/1024. When an image is deleted, the system cleans up by deleting everything under the path /2001/12-31/image.jpg.

In order to continue to be able to delete by path, I would need to keep the S3 file structure and transform the path somewhere between the CDN and S3. The most appropriate technology is CloudFront Functions, which is designed to do exactly this sort of thing. Do not use Lambda @ Edge, which are much higher latency and also overkill for this situation.

My concern: I'm unsure how much latency a CloudFront Function would add to the request -- I don't believe the "sub-millisecond" claim AWS makes but also I don't believe the 100's of ms I see in another blog.

I guess the thing to do is measure current performance, add a CloudFront Function, measure again. The CloudFront Function doesn't even have to do the rewrite, I just need some code to execute.

URL Format Options

I'm going with Option 2 because its cons turned out to be invalid. It's just the better option.

Option 1

/i/jpeg/1024/VERSION/2001/12-31/image.jpg

Option 2

/i/2001/12-31/image.jpg?version=VERSION&format=jpeg&size=1024&crop=2,3,4,5

Error handling

Should I change the S3 bucket path?

Probably not