如何在文件系统中存储图像 [英] How to store images in your filesystem

查看:229
本文介绍了如何在文件系统中存储图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有一个图像(最大6MB)存储在一个InnoDB表中的BLOB。
随着数据大小的增长,每晚备份的速度越来越慢,阻碍了正常的性能。



因此,二进制数据需要转到文件系统。 (指向文件的指针将保存在DB中)



数据具有类似于以下关系的树:

   - 主要网站
- user_0
- album_0
- album_1
- album_n
- user_1
- user_n
etc ...

现在我想通过目录结构均匀分布数据。



我想我可以尝试 MD5('userId,albumId,imageId'); 并分割结果字符串以获取我的目录路径:

 
/ var / imageStorage / f / 347e / 013b / c042 / 51cf / 985f7ad0daa987d .jpeg

这将允许我将第一个字符映射到服务器,并将目录结构均匀分布在多个服务器上。



但是 不会按照每个用户组织图片,可能会在多个服务器上传播1张相册的图片。



我的问题是:

以平衡的方式将图像数据存储在文件系统中,同时将用户/专辑数据保存在一起的最好方法是什么?



我是在正确的方向思考吗?

更新:

我会去的是 md5(user_id)在最高级别拆分的字符串切片。
然后将所有用户数据放在同一个桶中。这将确保数据的均匀分布,同时保持用户数据存储在一起。

 
/ var
- imageStorage
- f / 347e / 013b
- f347e013bc04251cf985f7ad0daa987d
- 0
- album1_10
- picture_1.jpeg
- 1
- album1_1
- picture_2 .jpeg
- picture_3.jpeg
- album1_11
- picture_n.jpeg
- n
- album1_n

我想我会使用albumId从后面分开(我喜欢这个想法!),以保持每个目录的专辑数量更小(虽然大多数用户不需要)。



谢谢!

解决方案

  UserID = 6435624 
Path = / images / 24/56/6435624

对于备份,您可以使用MySQL复制并备份从
数据库,以避免在备份时出现问题/ p>

Currently, I've got images (max. 6MB) stored as BLOB in a InnoDB table. As the size of the data is growing, the nightly backup is growing slower and slower hindering normal performance.

So, the binary data needs to go to the file system. (pointers to the files will be kept in the DB.)

The data has a tree like relation:

- main site
  - user_0
    - album_0
    - album_1
    - album_n
  - user_1
  - user_n
etc...

Now I want the data to be distributed evenly trough the directory structure. How should I accomplish this?

I guess I could try MD5('userId, albumId, imageId'); and slice up the resulting string to get my directory path:

  /var/imageStorage/f/347e/013b/c042/51cf/985f7ad0daa987d.jpeg

This would allow me to map the first character to a server and evenly distribute the directory structure over multiple servers.

This would however not keep images organised per user, likely spreading the images for 1 album over multiple servers.

My question is:
What is the best way to store the image data in the file system in a balanced way, while keeping user/album data together ?

Am I thinking in the right direction? or is this the wrong way of doing things altogether?

Update:
I will go for the md5(user_id) string slicing for the split up on highest level. And then put all user data in that same bucket. This will ensure an even distribution of data while keeping user data stored close together.

  /var
   - imageStorage
     - f/347e/013b
       - f347e013bc04251cf985f7ad0daa987d
         - 0
           - album1_10
             - picture_1.jpeg
         - 1
           - album1_1
             - picture_2.jpeg
             - picture_3.jpeg
           - album1_11
             - picture_n.jpeg
         - n
           - album1_n

I think I will use albumId splitted up from behind (I like that idea!) as to keep the number of albums per directory smaller (although it won't be necessary for most users).

Thanks!

解决方案

Just split your userid from behind. e.g.

UserID = 6435624 
Path = /images/24/56/6435624

As for the backup you could use MySQL Replication and backup the slave database to avoid problems (e.g. locks) while backuping.

这篇关于如何在文件系统中存储图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆