Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to store images in database on Heroku?

We have a RoR 3.2 site on Heroku using their production tier PostgreSQL database.

Our site users occasionally upload images of 0.5 - 2MB in size. For different reasons we want to store those images in the database, rather than as files on S3 or similar.

If our site was image heavy, i.e. a Facebook clone, surely storing images in the db would not be a good thing.

Anyway, it is not, so here are some of the reasons for wanting to store a users images in the database:

  • Control over everything: S3 downtime and third party gems cannot break image handling/access to images.
  • Easy delete: If a user cancels his account, we can easily delete all associated images with that account through our rails models (relation dependencies).
  • Easier user data export: When a user wants to leave our site and take his data with him, we can more easily export his images rather than having to export from S3
  • Cost: Not a weighty argument, but since we're already paying for the database it will be cheaper than using S3 which costs (a small amount of) money.
  • Backups: Not a weighty argument, but a backup of our db would include everything associated with our site (except the RoR code, of course).

What is the easiest way to store images in the Heroku PostgreSQL database? Example code or link to a walk-through would be most welcome.

Thanks :)

like image 904
rassom Avatar asked Aug 25 '12 14:08

rassom


People also ask

Can we store images on Heroku?

No problem. Simple File Upload is the easiest way to allow users to add files to your app. We handle all of the tooling surrounding file uploading for you. No cloud storage account needed.

Can Heroku store database?

Heroku Postgres delivers the world's most advanced open source database as a trusted, secure, and scalable service that is optimized for developers.


1 Answers

I encourage you to not use your database to store images of that size.

S3 can be backed up very effectively, and is extremely hardened. From a data loss perspective, it's effectively safer then a development postgres database plan with Heroku.

The cost aspect is also not really best practice in this example. Although you're right they only enforce a row limit, I think it would be frowned upon to store large images (0.5 - 2mb) in the development database plan.

Finally, the speed of a transfer to S3 from EC2 (where Heroku is hosted) is very fast due to Amazon's internal networking.

Knowing that, I'd urge you to rethink your desire to store images of that size in a database. See this question for a more in depth analysis on the pros and cons.

There's also a dev center article about using S3 and Heroku.

like image 197
Jack P. Avatar answered Sep 20 '22 20:09

Jack P.