In an app that will be deployed on heroku....
I need to allow users to upload thumbnail images.
A heroku-deployed app of course has no persistent local file storage.
The typical thing to do here, googling around, seems to be storing the files in Amazon S3, or possibly other AWS-hosted cloud storage.
But what if I just stick the images in a postgres blob column?
What are the downsides of doing this? The upsides are, don't have to pay for other storage, don't have to deal with an additional external system with more opportunities for bugs and outages. But there must be some good reasons nobody seems to do this, what are they?
Storing the files in the database can make the database much larger. Even if say a daily full backup would have sufficed, with a larger database size, you may no longer be able to do that.
It's possible to store the content of the files in the database itself, or we could store the content somewhere else and index it with the database. In this article, we're going to illustrate both of these methods with a basic Image Archive Application.
Since the dawn of time, database vendors have called out to developers, “Store everything inside the database. You know you want to. Here, we'll make it easy for you by adding data types like binary and features like filestream.”
Finally, you shouldn't store credit card information in your database unless you absolutely need to. This includes credit card owner names, numbers, CVV numbers, and expiration dates.
A database and S3 are two different storage mechanisms. How are they different?
About S3
Amazon S3 is a highly specialized file storage system. It minimizes what you can do to basic read/write/delete file operations and optimizes caching for serving data of the entire file stored.
About Postgres
Postgres is a SQL relational database with massive flexibility for storing and indexing data for a variety of operations. You can very well cram binary image data into a row within Postgres.
Comparing Cost/Scalability
Why would you choose S3 over Postgres? Cost and scalability. Postgres is an expensive, highly skilled generalist. On Heroku, running a Postgres database could cost you hundreds or thousands of dollars/month based on the amount of data and scale of traffic.
Amazon S3 is an inexpensive and highly scalable solution that perfectly matches your needs (write an image, serve up the image in the context of a web page).
TL;DR: Amazon S3 is highly optimized for files like images, highly scalable, and relatively inexpensive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With