Next.js Discord

Discord Forum

Storing and hosting images

Unanswered
Asiatic Lion posted this in #help-forum
Open in Discord
Original message was deleted.

36 Replies

Original message was deleted
images aren't saved in a database but in a storage bucket
you upload an image to your bucket and get a url in return which you save in database and use accordingly
Original message was deleted
Silver Fox
I store them in a file system.
Original message was deleted
I save them in a storage bucket, I will just change api keys from dev env to prod
File system is not writable in serverless hosts such as vercel, so that’s one thing to keep in mind if you don’t want to learn it the hard way like https://nextjs-forum.com/post/1151080393815113728
Original message was deleted
Silver Fox
yes you should prob use firebase or something.
For image hosting I use cloudflare r2
But any object storage service is good: s3, r2, google clould (firebase) are just some examples
Original message was deleted
take image, upload it, pass the returned url to your server in the post request where you save your todo
add a field in db called image
set it to url
and use it
Original message was deleted
you will need a new route where you will upload images
make sure to add proper auth here, this route can be abused if left open
Depending on your situation you may want a route to serve images too
The thing is that the URL you get from these services might be totally public, you can't secure them with auth
So it's common to have an API endpoint that checks auth, then download the image from cloudinary/AWS S3/whatver
So the actual URL of the image stays private
it's unsettling the first time you write something like this but most often it's actually a dozen lines of code
Original message was deleted
cause cloudinary and stuff expect to be called by a backend, not a browser
but not always it depends
having image upload/image serving go through your API allow you to control the process better
duh kill this thing
it's suffering
"Correct, you don't need to create a separate route specifically for images." depends on how you process the form those stuff are up to you
it's common to separate processing files from the rest because they are a tad specific
eg if you use JSON or GraphQL for form data it's not a good fit for files
so instead you upload to get a tmp url
then add the URL to your JSON
and make it a final URL when the data are validated
but that's up to you
Original message was deleted
I've coded it a few times
when you make all the possible mistakes you eventually learn the right approach ':)
no miracle
but there is not a single way to go just a few possible patterns, you have to pick what you need among the response provided here
you got it 👍
Original message was deleted
good news is that you are describing 90% of web apps in the world
auth, file upload are tricky parts but you'll reuse this knowledge a lot and improve over time
what did you do in the end?