本文介绍了我应该在哪里存储照片?文件系统还是数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
存储上传的照片和文档 - 文件系统与数据库 blob

我开始开发一个网络应用程序,其主要目的是显示照片.用户也可以上传照片.

I am starting to develop a web app, the primary purpose of which is to display photos. The users will be able to upload photos as well.

出现的第一个问题是照片的存储位置:文件系统还是数据库.

The first question that came up was where to store the photos: on the file system or the database.

我将使用 Windows 设备来托管该站点.数据库为 MySQL,后端代码为 C#,使用 ASP.NET MVC.

I will be using a Windows box to host the site. The database is MySQL and the backend code is in C# utilizing ASP.NET MVC.

推荐答案

当然是文件系统,除非您的目标是在每日的wtf 上写一个故事.最简单的方法是按照可以从文件本身派生的属性(例如其 SHA-1 哈希)来组织照片.然后只需将哈希存储在数据库中,附加到照片的主键和其他属性(上传者、上传日期等).

Filesystem, of course, unless you're aiming for a story on thedailywtf. The easiest way is to have the photos organized by a property you can derive from the file itself, such as its SHA-1 hash. Then just store the hash in the database, attached to the photo's primary key and other attributes (who uploaded it, upload date, etc).

将文件系统上的照片分开也是一个好主意,这样您就不会在一个目录中留下数百万个文件.所以你会有这样的事情:

It's also a good idea to divvy up the photos on the filesystem, so you don't end up with millions of files in a single directory. So you'll have something like this:

storage/00/e4/f56c0de1c61fdb926e79e8a0a65bd12930c9.jpg
storage/25/9a/ec1c55bfb660548a6770238668c4b117d92f.jpg
storage/5d/d5/4b01d98f17a9ad9dd1526b49ba39b5aa37a1.jpg
storage/63/49/6f740b6c284ce6685dc17d473a7360ace249.jpg
storage/b1/75/066d178188dde110149a8422ab651b0ee615.jpg
storage/b1/20/a2b7d02b7b0c43530677ab06235382a37e20.jpg
storage/da/39/a3ee5e6b4b0d3255bfef95601890afd80709.jpg

如果您迁移到分片存储,这也很容易移植.

This is also easy to port if you ever move to sharded storage.

这篇关于我应该在哪里存储照片?文件系统还是数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 12:31