3
I’m modeling a database, but I’m in doubt how to deal with this part of saving the image on Amazon S3 and easily referencing in the database.
I intend to save the image URL (hosted on Amazon S3) in a field called "image_url" of the bd.
One of my doubts is:
How do I generate multiple image size versions (Sm, Md, lg, original), and be able to save their url to the database? Should I create a column for each version of the image? Ex: image_url_sm, image_url_md, image_url_lg, image_url_original. In my opinion this seems a bad practice of database.
Here is my current table:
First of all, thank you for your reply. I didn’t quite understand this GUID scheme, but from what I understood of the table, you would only save the image name, then use the default URL of Bucket and just change the file name, and so you could access any of the versions? (original, Sm, Md, lg)
– Thiago Yoithi
And how would you know the format of the image? Jpg, Png.. I really liked this idea, but maybe n would be more interesting to save two columns, one with the URI (path and file name without file extension), and another column only with the file extension?
– Thiago Yoithi
The GUID (this random string) is guaranteed to be different for each record. I don’t know where your files come from, so I figured some of them might have the same name. Generating a GUID (UUID) for each record is one way to solve this problem. As for the file extension, if you can standardize and convert all images to JPEG or PNG it would be better. If not possible, then you can save the name of the original version
xxxxx.original.jpg
and manipulate the name to get the other sizes.– sergiopereira
Oh yes, I understand, but the printscreen of the table I put up does not solve? In this case I would change the "image_url" to "image_key", which is not even your example. It would not work?
– Thiago Yoithi
Ah, Obs: The file name generation I use a uniqid() PHP function that creates a name based on the current timestamp, so it will never repeat.
– Thiago Yoithi
As long as you are sure that the files will never have the same name, then you can leave the GUID aside and use the file name itself. Like you said, renaming the field to
image_key
.– sergiopereira
Got it, thank you so much for the tips! I will tag as an answer. I will follow your suggestions in my app.
– Thiago Yoithi