Best practices for storing images in Amazon S3 and saving in Mysql

Asked

Viewed 1,190 times

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:

inserir a descrição da imagem aqui

1 answer

2


I would particularly try not to keep any of this in the database. I would create a GUID for each record and make the URL of each image be derived from that GUID. Example of a record:

id: 123
name: "Eletrônicos"
image_key: "fdb3a9ba-b5e6-4d56-b703-27adb55c6f92"
etc...

In S3 I would create and save each version of the image:

  • meubucket/Categories/images/fdb3a9ba-b5e6-4d56-b703-27adb55c6f92.original.png
  • meubucket/Categories/images/fdb3a9ba-b5e6-4d56-b703-27adb55c6f92.sm.png
  • meubucket/Categories/images/fdb3a9ba-b5e6-4d56-b703-27adb55c6f92.md.png
  • meubucket/Categories/images/fdb3a9ba-b5e6-4d56-b703-27adb55c6f92.lg.png

The initial part of the URL will be http://meubucket.s3-website-us-east-1.amazonaws.com/categories/images and you can put this value in some configuration file if you need to change afterwards or you have more than one environment of this running application (eg Production x Integration).

If you are sure that the id registration never changes, you can even use it in place of the GUID, but find it more peaceful using the GUID.

  • 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)

  • 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?

  • 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.

  • 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?

  • 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.

  • 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.

  • Got it, thank you so much for the tips! I will tag as an answer. I will follow your suggestions in my app.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.