If image file paths in DB, where do I store image?
Posted by Low_Oil_7522@reddit | learnprogramming | View on Reddit | 7 comments
Hi!
In the past, I created a 'social media' app on heroku. You could take an image and it would post on a page.
I found that like once a day the app would reset and all of the images would disappear.
Even if storing only the image path in a database is the best practice, I'd still store the image on the heroku app. Here it would still remove all images when it resets.
On my local machine, storing the image in the same directory as the project works fine. Once moved to heroku, those files get removed.
I think this is a pretty normal concept. What are the best practices?
Lumethys@reddit
You use a different persistent server to store files. The modern term is Object Storage. The most popular is AWS S3, but there are many cheaper alternative, like Cloudflare R2 or Blackblaze B2
VoiceOfSoftware@reddit
Depending on how many images you want to store, consider cloudinary.com — they have a very generous free tier, and tons of really cool features. They provide uploader widgets, too, in case you or your users want a nice browser interface for uploading new images
I learned about them from another subreddit, and am super happy with them
rllngstn@reddit
Consider Render. It's very easy to add a volume (persistent storage) to an instance. Much easier than dealing with AWS.
Rain-And-Coffee@reddit
You host the images externally, either on something like S3 (object storage) or you can store them on the Hard disk of your host (different than the container).
In the case of docker you could mount a volume onto the container.
However if your deploying onto multiple server you have to make sure all hosts can reach it. One common way is to use an external mount, something like an NFS share.
On a Platform as a Service (like Heroku) you just have to read and see what services they offer for persistent storage.
Low_Oil_7522@reddit (OP)
I've created an aws account to see what is going on better.
Do I host my application on aws too? Or do I keep the application on heroku and use s3 only for storage?
high_throughput@reddit
Heroku doesn't persists disks. That would get weird when you scale up and down. Use an external storage service like Amazon S3, or shove the images a blobs into your (hopefully already persistent) DB.
Low_Oil_7522@reddit (OP)
Thanks for the input! While I was looking into this previously, Amazon S3 seemed like the best solution.