Rails:在数据库中存储二进制文件 [英] Rails: Storing binary files in database

查看:42
本文介绍了Rails:在数据库中存储二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Rails,是否有理由将附件(可以是任何时间的文件)存储在文件系统而不是数据库中?数据库对我来说似乎更简单,无需担心文件系统路径、结构等,您只需查看 blob 字段即可.但是大多数人似乎都在使用文件系统,这让我猜测这样做肯定有一些好处,但我没有得到,或者使用数据库进行此类存储有一些缺点.(在本例中,我使用的是 postgres).

Using Rails, is there a reason why I should store attachments (could be a file of any time), in the filesystem instead of in the database? The database seems simpler to me, no need to worry about filesystem paths, structure, etc., you just look in your blob field. But most people seem to use the filesystem that it leaves me guessing that there must be some benefits to doing so that I'm not getting, or some disadvantages to using the database for such storage. (In this case, I'm using postgres).

推荐答案

这是一个非常标准的设计问题,并没有真正的唯一正确答案".

This is a pretty standard design question, and there isn't really a "one true answer".

我通常遵循的经验法则是数据进入数据库,文件进入文件".

The rule of thumb I typically follow is "data goes in databases, files go in files".

要记住的一些注意事项:

Some of the considerations to keep in mind:

  1. 如果一个文件存储在数据库中,你将如何通过 http 提供它?请记住,您需要设置内容类型、文件名等.如果它是文件系统上的文件,Web 服务器会为您处理所有这些内容.非常快速和高效(甚至可能在内核空间中),不需要解释代码.

  1. If a file is stored in the database, how are you going to serve it out via http? Remember, you need to set the content type, filename, etc. If it's a file on the filesystem, the web server takes care of all that stuff for you. Very quickly and efficiently (perhaps even in kernel space), no interpreted code needed.

文件通常很大.大型数据库当然是可行的,但它们速度慢且不方便备份等.为什么在不需要的情况下使数据库变得庞大?

Files are typically big. Big databases are certainly viable, but they are slow and inconvenient to back up etc. Why make your database huge when you don't have to?

很像2.,将文件复制到多台机器真的很容易.假设您正在运行一个集群,您可以定期将文件系统从您的主机同步到您的从机,并使用标准的静态 http 服务.显然数据库也可以聚类,只是不一定直观.

Much like 2., it's really easy to copy files to multiple machines. Say you're running a cluster, you can just periodically rsync the filesystem from your master machine to your slaves and use standard static http serving. Obviously databases can be clustered as well, it's just not necessarily as intuitive.

与 3 的另一面相反,如果您已经对数据库进行了集群,那么必须另外处理集群文件是管理复杂性.我会说,这将是考虑将文件存储在数据库中的一个原因.

On the flip side of 3, if you're already clustering your database, then having to deal with clustered files in addition is administrative complexity. This would be a reason to consider storing files in the DB, I'd say.

数据库中的 Blob 数据通常是不透明的.您无法对其进行过滤、排序或分组.这降低了将其存储在数据库中的价值.

Blob data in databases is typically opaque. You can't filter it, sort by it, or group by it. That lessens the value of storing it in the database.

另一方面,数据库理解并发.您可以使用事务隔离的标准模型来确保两个客户端不会尝试同时编辑同一个文件.这可能很好.并不是说您不能使用锁文件,但现在您需要理解两件事而不是一件.

On the flip side, databases understand concurrency. You can use your standard model of transaction isolation to ensure that two clients don't try to edit the same file at the same time. This might be nice. Not to say you couldn't use lockfiles, but now you've got two things to understand instead of one.

可访问性.文件系统中的文件可以使用常规工具打开.Vi、Photoshop、Word,无论您需要什么.这很方便.您将如何从 blob 字段中打开该 Word 文档?

Accessibility. Files in a filesystem can be opened with regular tools. Vi, Photoshop, Word, whatever you need. This can be convenient. How are you gonna open that word document out of a blob field?

权限.文件系统有权限,它们在后面可能会很痛苦.相反,它们可能对您的应用程序有用.如果您利用 7,权限真的会咬您,因为它几乎可以保证您的 Web 服务器以与您的应用程序不同的权限运行.

Permissions. Filesystems have permissions, and they can be a pain in the rear. Conversely, they might be useful to your application. Permissions will really bite you if you're taking advantage of 7, because it's almost guaranteed that your web server runs with different permissions than your applications.

缓存(来自下面的sarah mei).这在客户端上面起到了 http 问题的作用(你会记得正确设置生命周期吗?).在文件系统上的服务器端文件是一种非常容易理解和优化的访问模式.您的数据库可能会也可能不会很好地优化大型 blob 字段,而且您几乎可以保证从数据库到 Web 服务器还有额外的网络传输.

Cacheing (from sarah mei below). This plays into the http question above on the client side (are you going to remember to set lifetimes correctly?). On the server side files on a filesystem are a very well-understood and optimized access pattern. Large blob fields may or may not be optimized well by your database, and you're almost guaranteed to have an additional network trip from the database to the web server as well.

简而言之,人们倾向于将文件系统用于文件,因为它们最支持类文件习语.不过,您没有理由必须这样做,而且文件系统正变得越来越像数据库,因此最终看到完全融合一点也不令我感到惊讶.

In short, people tend to use filesystems for files because they support file-like idioms the best. There's no reason you have to do it though, and filesystems are becoming more and more like databases so it wouldn't surprise me at all to see a complete convergence eventually.

这篇关于Rails:在数据库中存储二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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