I am developing a Node.js application that uses S3 and CloudFront to store and distribute images, such as profile pictures. After uploading an image to S3, I save its key (name) in the database. To access these images via CloudFront, I dynamically construct URLs by combining CloudFront’s domain with the S3 key when the API responds.
However, my API has multiple routes that involve images, and I often find myself repeatedly creating these URLs for each response. This is particularly cumbersome when dealing with nested responses containing images, like posts with associated user profile pictures.
I’m seeking advice on how to streamline this process for better scalability and maintainability. Should I store only the filename in the database? And is it acceptable to repeatedly map objects to construct URLs?