Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to store user's avatars

I have a website where users each have their own profile page. Here they can upload a single image that acts as their avatar. This is the only image users can upload to the server across the whole site. There is no archive so it can be overwritten if a user wishes to update their avatar.

I have never had to do anything like this before so I would like to open it up and ask for a suitable, scalable option for this website.

My initial thought is to give each user's image a random name, a string 6-12 characters long. This way you couldn't build a script that just pulls every user's profile pic from a directory (eg 001.png, 002.png etc). My second thought is that there should be only be a certain amount of images per directory to make sure they can be retrieved quickly by the server.

There may well be other things I'm missing here, I'm not sure on exact details hence why I'm asking.

like image 587
jskidd3 Avatar asked Dec 16 '13 22:12

jskidd3


People also ask

What are user avatars?

Avatars are images associated with user accounts. They can be displayed, for example, on a user's public profile. Avatars serves as graphical representations of users and are often used to personalize user contributions on websites.


2 Answers

I would recommend storing the images on something like Amazon S3. Depending on how many pictures you're storing, serving images can really take a tow on your web server. S3 is scalable and with multi-zone deployments through CloudFront (Amazon's version of a CDN), you can really speed up this part of your service.

like image 57
Lloyd Banks Avatar answered Sep 29 '22 02:09

Lloyd Banks


It's good idea to not overload single directory. Very often you can see that images are stored in hierarchy of folders according to theirs first few letters. An example of this is

b5dcv5.jpg -> /b/5/b5dcv5.jpg
bsgb0g.jpg -> /b/s/bsgb0g.jpg
a5dcbt.jpg -> /a/5/a5dcbt.jpg

and so on. I thing you got the principle. Advantage of this is to have access to and image in O(log N) when filenames are uniformly distributed instead of O(N) as it would be in single folder solution.

like image 28
tomas789 Avatar answered Sep 29 '22 02:09

tomas789