如何组织和操作文件系统中的图片?
虽然看起来可能不是这样,但关于这个主题有一些疑问。 我不是问我是否应该使用数据库或文件系统(它是文件系统),而是问在将与 ASP.NET 应用程序一起使用的文件系统中组织图片的最佳方式是什么? 我说的是几万甚至几十万张图片。
稍微详细说明一个问题。
- 存储库应位于何处,在 IIS 或应用程序 VD 中创建另一个 VD,还是 IIS 之外的某个位置?
- 如何组织文件路径,将它们全部放在一个文件夹中或创建子文件夹。如果是这样,如何命名所有这些文件夹?
- 如何处理同一张图片的多种尺寸?在哪里找到它们?
- 如何将文件夹和图像与数据库同步?
- 你还想到了什么,你认为这很重要……
我自己已经做了一些事情,但我想问是否有人有这方面的经验。也许按照你的方式可以做得更好。
我把这样的东西
我将图片存储在IIS文件夹中,因为将它们存储在iis之外使它们无法在Web中使用,因为路径。 Web 中不能使用文件路径,它必须是虚拟文件路径。
http://images/1.jpg 而不是 C:\images\1.jpg
root 我有每个图片尺寸的子文件夹
根- +400x300 +600x400 +1024x768
根据数据库中的 ID 命名图像。如果数据库中的ImageID是45678909,则图片的名称是45678909.jpg,它的位置是
VD\图像\400x300\45\67\89\45678909.jpg
VD\images\600x400\45\67\89\45678909.jpg
我将图片名称两两切片,并创建子文件夹。重点是文件夹中的文件不要超过 100 个。这是好主意还是愚蠢的主意?你会如何组织这个?对于个人来说,这个拥有大量文件夹的组织并不容易使用。
- 我保存在数据库表中的图像名称及其相对路径,以及描述和插入时间等。路径:45\67\89\45678909.jpg
有什么建议或改进吗?
谢谢 :)
Although it may not seem like that but there is a little questions about this subject.
I am not asking if I should use DB or filesystem, the file system it is, but I am asking what is the best way of organizing pictures in file system that will be used with asp.net application?
I am talking about tens or even hundreds of thousands of picture.
To elaborate a question a bit.
- Where should repository be located, create another VD in IIS or in application VD, or somewhere outside IIS?
- How to organize file patsh, put them all in one folder or create subfolders. If so how to name all those folders?
- How to handle multiple sizes of the same image? where to locate them?
- How to sync folders and images with database?
- What else crosses your mind, and you think it's important..
I've done something myself allready, but I am asking if someone had some experience with this. Maybe it could be done better yours way.
I put things like this
I stored pictures in IIS folder, because storing them outside of iis make them unable to use in web, because of paths. You cannot use file path in web, it must be virtual file path.
http://images/1.jpg istead of C:\images\1.jpg
in the root I had subfolder for each picture size
root-
+400x300
+600x400
+1024x768Named images by id from database. If the ImageID from database is 45678909, the name of picture is 45678909.jpg an it's location is
VD\images\400x300\45\67\89\45678909.jpg
VD\images\600x400\45\67\89\45678909.jpg
I sliced the picture name two by two, and create subfolders. the point was not to have more than 100 files in folder. Was this good idea or stupid one? How would you organize this? For person, this organization with tons of folder is not easy to use.
- Name of the image with its relative path I saved in database table with description and time of insert etc. path: 45\67\89\45678909.jpg
Any suggestions or improvements?
Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的最后一个解决方案对我来说似乎是一个非常好的解决方案。如果一个文件夹中的文件太多(例如超过 1000 个),文件访问速度可能会变慢,因此坚持 100 个比较好。
在同步方面,您只需要一个定期运行的脚本来查询数据库中应该存在的所有图像,并删除多余的图像。如果您的磁盘空间并不紧张,那么您就不需要这个。
有时您可能希望将图像放在另一台服务器上以获得更好的可扩展性。我唯一可以做的就是为您的图像设置一个子域,例如 images.yourdomain.com。即使您将其指向同一服务器,如果需要,也可以很容易地将图像转移到更快的文件服务器。
Your last solution seems a perfectly good solution to me. File access can slow down with too many files in one folder, more than 1000 say, so sticking to 100 is good.
In terms of syncing, you'd just need a script that runs periodically that queries the database for all the images that should be there, and deletes the redundant one. If you're not tight on disk space, then you don't need this.
There may come a time when you'd want the images on another server for better scalability. The only thing I might do is set up a subdomain for your images, e.g. images.yourdomain.com. Even if you point it at the same server, it's very easy to shift your images to a faster file server should you need to.
1)我不懂ASP或IIS,所以我不能在这里帮助你。我将图像存储在 Amazon S3 上,但我不知道这是否与您的情况相关。
2)尝试根据模型、ID 来组织照片。因此,如果图像是用户头像图像,则您的路径将类似于 avatars/ID/image。
3) 如果您创建多个不同尺寸的图像,请尝试为每个尺寸指定一个名称。例如,您有“原始”图像、“缩略图”图像 (90x90) 和“预览”图像 (400*300)。这样您就可以存储不同的尺寸,例如 avatars/ID/image_SIZE 或 avatars/ID/SIZE/image。
4)如果您遵循我提到的方式,那么您只需要在数据库中存储原始图像的信息,然后您可以在运行时从中派生所有其他图像。
所有这些要点都来自 Ruby on Rails 的令人惊叹的 PaperClip 插件。查看它以获取有关如何存储文件和附件的进一步指导。
1) I don't know ASP or IIS, so I can't help you here. I store images on Amazon S3, but I don't know if that's relevant to your case.
2) Try to organize photos based on their model, then ID. So if images are user avatar images, your path would look like avatars/ID/image.
3) If you create multiple images of different sizes, try assigning a name to each size. For instance, you have "original" image, "thumbnail" image (90x90), and "preview" image (400*300). This way you can store the different sizes either like avatars/ID/image_SIZE or avatars/ID/SIZE/image.
4) If you are following the way I mentioned, then you only need to store information for the original image in your DB, and from that you can derive all other images at runtime.
All those points are from the amazing PaperClip plugin for Ruby on Rails. Take a look at it for further guidance on how they store files and attachments.