#Storing and hosting images

37 messages · Page 1 of 1 (latest)

drifting idolBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

wise abyss
#

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

hearty fable
#

I store them in a file system.

wise abyss
#

I save them in a storage bucket, I will just change api keys from dev env to prod

hushed fable
hearty fable
#

yes you should prob use firebase or something.

hushed fable
#

For image hosting I use cloudflare r2

#

But any object storage service is good: s3, r2, google clould (firebase) are just some examples

wise abyss
#

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

#

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

little cedar
#

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

#

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

#

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 👍

little cedar
#

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

little cedar
#

what did you do in the end?