C
C#2mo ago
Fady

✅ Typically how is an image stored?

Im trying to set up an ASP.NET Project where i have a profile section. Users can then choose to upload a Profile Picture of themselves. I want to store this profile picture in my MSSQL Database so they stick around. How would you typically do it? Im trying to find info on what the "Best approach" would typically be. As it feels like a lot of storage space for a simple cause.
68 Replies
leowest
leowest2mo ago
typically they are stored in the filesystem with a path in the database filesystem could be azure, cdn etc
Fady
Fady2mo ago
I see 😮 The info i found was basically i should convert the image to byte arrays and then unpack it when i take it out again; Its a small part of a simple school project
Fady
Fady2mo ago
No description
Fady
Fady2mo ago
like this
Angius
Angius2mo ago
That's bad info Or rather, it will work, yes, but it's a generally dogshit solution You give up the ability to cache those images, to process them in any way Fetches from the database take longer
Fady
Fady2mo ago
Oh i see; Do you have any guides or documentation on using Azure to store the images? I cannot seem to find anything
Angius
Angius2mo ago
If it's just a school project I'd just use the server's filesystem tbh Never used Azure myself, I don't fancy myself going bankrupt at 30 lmao
Fady
Fady2mo ago
Hmmmm i dont think i've ever used that before 😮
Angius
Angius2mo ago
Just File.WriteAllBytesAsync() the files
Fady
Fady2mo ago
Hahahaha makes sense; we have access to a Student subscription but we do need it for next semester Thanks again guys; i'll do some research 😄
Angius
Angius2mo ago
You'd probably use the BLOB client library
Fady
Fady2mo ago
I'll look into that 😮
Zendist
Zendist2mo ago
That's good advice. Azure blob storage sounds like the right approach for you, if you have a free subscription to play with. You don't always have access to persistent storage, that's why blob or cloud storage services can be useful. Then you have a blob URL in your DB instead of a file path.
leowest
leowest2mo ago
@Fady yeah sorry I was busy to provide the full info that is what I mean with azure above, thx Z there are also events where u just use the filesystem, but given the technology these days, using CDN is the way to go
Fady
Fady2mo ago
No problem @leowest Well to me it comes down to money; Does using CDN cost any money?
leowest
leowest2mo ago
it depends on what u already have it could reduce costs could raise it depends on some variables u see not something I can tell without having more information but u can always go and look up azure blob, how much it costs cloudflare etc research 😉
Fady
Fady2mo ago
So we are likely looking at a ~30-50 users at most. Its a group project that will be presented infront of our school and people will test it; and each user can only upload one image lol
leowest
leowest2mo ago
im not sure what 30~50 users at most represents here, I dont know if it needs to have an actual website if all u need to do is present for ur school could be as simple as running the whole project in a notebook with visual studio so it could be a completely no cost type of thing or if ur school offers vm or something u could run it there or if u use your school credentials u could get free vps etc github offers some cool educational stuff
leowest
leowest2mo ago
GitHub Education
GitHub Student Developer Pack
The best developer tools, free for students. Get your GitHub Student Developer Pack now.
leowest
leowest2mo ago
gives u free credit for azure, digital ocean and more
Fady
Fady2mo ago
So basically we have set up a full website; We have set up the Backend as well as the Database in Azure the Frontend is up through Github Pages All the Profile Picture will do is show up in the Profile; all i want it to do is stick around
leowest
leowest2mo ago
sure but who is paying for it or how it goes down is the school providing everything?
Angius
Angius2mo ago
Price depends on what you use... a lot Some CDNs will have paid egress, so you pay for all the data sent Some only want money for storage and the amount of fetches, but egress is free If you can cache the file served, that reduces both fetches and egress, so you can even get under the free tier Assuming there is a free tier
Fady
Fady2mo ago
Microsoft is offering $100 for every student; So the school pays nothing really as long as we're deemed eligible 😮 I definitely get that; Basically i'm looking at perhaps 100 images called at the very most; As it'll be visible to students for a day; so we'll likely ~50 students trying it and likely uploading their own profile pictures; and then we'll get some traffic over the next couple days (So not being used a lot)
Angius
Angius2mo ago
With proper caching and a cheap-ish CDN it should be fine Should be fine even without a CDN tbh, just serving from the server
Fady
Fady2mo ago
I actually ended up setting up the Blobs with some pointers from a wonderful individual from this Discord channel The Backend as well as the DB is already up on Azure and the Frontend is up on Github Pages; So i'm trying to get away from anything local haha I'm sure Azure Blobs wont set me back too much
Angius
Angius2mo ago
Make sure images are served with a long cache duration And use cache busting to make any changes the user makes still visible immediately
leowest
leowest2mo ago
should be more than enough for a demo
Fady
Fady2mo ago
how do i manage that? Through Azure itself? or within my code? to me it sounds like a frontend thing
Angius
Angius2mo ago
Whatever serves the files The cache header has to be set
Fady
Fady2mo ago
hmm; serves how? Its up on Azure and my implementation is in backedn? Afaik cache etc is a frontend thing? (sorry for the weird questions; I'm meant to touch on Azure as a whole in ~4-5 months from now)
Angius
Angius2mo ago
Whatever sends the file to the browser It sends that file alongside some headers Some of those headers control how the image is cached
Fady
Fady2mo ago
Many thanks 😄 I'll test this out later today
Pobiega
Pobiega2mo ago
That doesn't seem like what you want. That's TTL for the entire blob, meaning the picture will get deleted when it expires And as far as I understand the question here, you will be storing the actual image in the blob, not a cached version that can easily be refreshed?
Zendist
Zendist2mo ago
Huh? Where do you get that? It's setting the Cache-Control header for GET calls.
Pobiega
Pobiega2mo ago
Oh sorry, didn't see the subheader "manage expiration of blob content" is a curious place to put cache headers
Fady
Fady2mo ago
So what i've done is; I've set up so the Image gets stored using Azure Blob Storage; And i get a SAS URL that gets stored in the Database. I haven't thought about Cache or anything like that Which is definitely something to do
Pobiega
Pobiega2mo ago
You wouldn't put a cache here The blob.storage handles that for you
Fady
Fady2mo ago
Oh; So what do i need to do with it next? Azure as a whole is a new concept to me haha; This takes it a step further
Pobiega
Pobiega2mo ago
The browser will see the URL, check the cache lifetime that was set by the blob storage (configured by you as per the link above)
Fady
Fady2mo ago
Oooooooh okay Is there any standard Cache Lifetime that people tend to follow?
Pobiega
Pobiega2mo ago
Think of the blob storage as an external storage place. That's it If you make it so an updated profile image won't reuse the same URL, you could set the maximum
Fady
Fady2mo ago
Yeah haha; that much i gathered; I didnt have access to the Azure subscription account of where we have everything; As it belongs to a classmate. So i tested everyting using Azurite
Pobiega
Pobiega2mo ago
That seems to be coming up quite often here lol It's problematic that the person who set it all up isn't the one doing the work
Zendist
Zendist2mo ago
If the SAS token keeps changing (i.e. you generate a new one for each request), the browser cache will not be used. Just FYI.
Pobiega
Pobiega2mo ago
Since you are limited to student accounts, which I guess don't allow adding other users Good point. Do you need SAS tokens here?
Fady
Fady2mo ago
Yeah; its definitely a problem. Took this upon myself but we have to work around eachother. Without the SAS URL that i'm generating, the User cannot really see the image i felt? As they need proper authentication; or at least thats the error message i received in the browser
Pobiega
Pobiega2mo ago
Iirc you can remove the need for a token, so the images are available without a token
Zendist
Zendist2mo ago
If it's ok to have public anonymous access to the pictures (definitely not OK in the real world 😄), you can skip the SAS token. Otherwise, the app can generate a container token per user that lasts for some time and keep reusing it. You can allow anonymous access. But again, it depends on the requirements of your stakeholders. In the real world, you definitely do not want to allow public anonymous access to user pictures.
Fady
Fady2mo ago
How would that work in practice? 😮 All i really want is for the image to be readable by the user; as it'll be put as an html img tag in our frontend Hmm; Any chance i could demo what i've done to you guys? I think its easier that way
Pobiega
Pobiega2mo ago
Think of the SAS token as a proof that the URL was generated from your app And it has a duration encoded in it Meaning it becomes invalid after a while So I can't hotlink your images on my blog
Fady
Fady2mo ago
I see 😮
Zendist
Zendist2mo ago
As mentioned, I would generate a token for the entire container (probably named "user_images"?), per user. Then when the user goes to a profile, you use the token tacked on to the picture and can reuse that same token for all pictures for that one user. The reason that we use per-user, is to detect malicious users and can shut down individuals instead of forcing all users to re-cache if we need to kick someone out.
Fady
Fady2mo ago
So the thing is, every User can only have ONE picture at a time anyway As its just a Profile Picture, i limited it mainly because its not really central in our app
Pobiega
Pobiega2mo ago
Sure but what about the profile pic of other users?
Fady
Fady2mo ago
How do you mean? Like if i upload a Profile Picture; Im the only person that can see it and if you upload one of your own; I cannot access it or even see it as it is right now
Pobiega
Pobiega2mo ago
Then why bother having profile pics at all? :p
Fady
Fady2mo ago
Hahaha; We felt like it "Looked nicer" 😛
Pobiega
Pobiega2mo ago
Alr, well zendists approach still works here User logs in, gets a token, uses that to fetch images during their session
Fady
Fady2mo ago
Also; I want to show up on the presentation on Tuesday and be like "So i used Azure Blob Storage, and SAS Tokens" :PatrickChad:
Zendist
Zendist2mo ago
😄
Pobiega
Pobiega2mo ago
Good excuse:p
Fady
Fady2mo ago
Hahaha 😄
Zendist
Zendist2mo ago
If you can only see your own photo, I would probably base64-encode a thumbnail of the image into the DB and just cache it in-memory on first request, given how few users you have 😄. But good opportunity to learn about blobs and doing it right with SAS etc.
Fady
Fady2mo ago
Yeah definitely; we are going to work more with Azure this fall (we havent even touched on the subject yet); But i intend on using it in a more practical way when the time comes 😄 Thanks so much guys 😄 I've learned a ton
Angius
Angius2mo ago
With cache busting, a year or so
Want results from more Discord servers?
Add your server
More Posts