I’m currently developing a Node.js application that utilizes S3 and CloudFront for storing and distributing images, such as profile pictures. Upon storing an image in S3, I save its key (name) in the database. When the API responds, I dynamically construct URLs to access these images via CloudFront by combining CloudFront’s domain with the S3 key.
However, as my API has multiple routes involving images, I find myself repetitively creating these URLs for each response. This becomes particularly cumbersome when dealing with nested responses containing images, such as posts with associated user profile pictures.
I’m seeking advice on how to streamline this process to ensure scalability and maintainability. Is it advisable to store only the filename in the database? And is it acceptable to repeatedly map objects to construct URLs?