如何在服务器上存储数百万张图像?
可能的重复:
存储大量图像
您好,
我希望能够扩展到我的 LAMP 服务器上使用 PHP 保存了数百万张用户个人资料图片。
我目前将所有图像存储在一个文件夹中,这是一个很大的禁忌,因此我想将它们分散到许多文件夹和子文件夹中(例如 aa/bb/ 等...)。
最好和最有效的方法是什么,特别是如果我不想调用数据库来获取该用户的个人资料图片的文件名/路径?
我正在考虑对用户名进行哈希处理,并利用该哈希值的前 4 个字母来生成/定位该用户的个人资料图片的路径,这样我就不必从数据库中额外访问任何内容,因为我将始终有用户的用户名。因此,例如,如果用户用户名哈希的前 4 个字符是“aabb”,我会将该用户的个人资料图片存储在 aa/bb/username/profile.jpg 下,理论上这应该允许我扩展到数百万用户,而无需必须向数据库添加任何内容,同时将所有图片均匀地分布在 aa/bb/ 文件夹结构中。
有什么想法/意见吗?
谢谢!
Possible Duplicate:
Storing a large number of images
Hello,
I want to be able to scale to millions of user profile pics on my LAMP Server using PHP.
I currently store all images in one folder, which is a big no-no, so I want to spread them out into many folders and sub-folders (e.g. aa/bb/ etc...).
What is the best and most efficient way of doing that, especially if I do not want to have to call the DB to get the filename/path for that user's profile pic?
I'm thinking of maybe doing a hash of the username and utilizing the first 4 letters of that hash to generate/locate the path for that user's profile pic, that way I wouldn't have to access anything additionally from the DB since I will always have the user's username. So, for example, if the first 4 characters of the user's username hash were "aabb", I would store that user's profile pic under aa/bb/username/profile.jpg , which should theoretically allow me to scale to millions of users without having to add anything to the DB, while spreading all the pics evenly throughout the aa/bb/ folder structure.
Any ideas/input?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
取决于您的用户是如何组织的。
我猜他们都有一个唯一的 ID。如果您知道,您可以存储诸如
0xx/007.png
、8xx/824.png
和547xx/54723.png
之类的文件。这会将主文件夹中的项目数量减少 100 倍,每个文件夹最多包含 100 个项目。如果您的用户名中只允许选择字符,您可以直接使用它,但我通常不建议这样做。如果您不知道自己在做什么,可能会很危险。文件名如
ma/master_of_desaster.png
、ki/king_cool.png
和so/some_other_infantile_name.png
使用哈希是个好主意。如果它与安全无关(似乎不是),您可以通过使用短校验和算法而不是复杂但安全的哈希算法来减少 CPU 开销。想想 CRC32。像
[CRC32sum]/[USER_ID].png
这样的文件名
Depends on how your users are organized.
I guess they all have a unique ID. If you know it you can store files like
0xx/007.png
,8xx/824.png
and547xx/54723.png
. This cuts the number of items in the main folder by factor 100 and every folder contains 100 items at max.If you have only selected chars in your usernames allowed you can directly use it but I would not generally recommend that. It could get dangerous if you don't know what you're doing. Filenames like
ma/master_of_desaster.png
,ki/king_cool.png
andso/some_other_infantile_name.png
Using hashes is a great idea. If it's not about security (seems it's not) you can reduce CPU overhead by using a short checksum algorithm instead of a complex but secure hash algorithm. Just think of CRC32. Filenames like
[CRC32sum]/[USER_ID].png