简体   繁体   English

使用 MySQL 存储/上传文件:索引与 blob

[英]Storing/uploading file/s with MySQL: indexing vs blob

I want to allow file upload(public/per user), but not sure about how to properly do it.我想允许文件上传(公共/每个用户),但不确定如何正确执行。

I've read that it is not recommended to use MySQL for this and instead should use file system for files and indexing them in the database.我读过不建议为此使用 MySQL ,而应该使用文件系统来存储文件并在数据库中为它们建立索引。 I remember reading some popular(with many votes) Q/A about this in SO, but can't find it(please send a link if you can).我记得在 SO 中阅读了一些关于此的流行(有很多票)Q/A,但找不到它(如果可以,请发送链接)。

So how should I do it?那我应该怎么做呢? I should use some inaccessible folder and store files there with an ID as their name and this name(ID of the file) in the table of files with user_id, and when the user request a file, I should check auth and then send the corresponding file to the user?我应该使用一些无法访问的文件夹并将文件存储在那里,其中 ID 作为它们的名称,这个名称(文件的 ID)在带有 user_id 的文件表中,当用户请求文件时,我应该检查身份验证然后发送相应的文件文件给用户?

There's no single answer to this question.这个问题没有单一的答案。 It depends how you want your application to work.这取决于您希望应用程序如何工作。 It could be perfectly fine, for example, to put the files in a directory that is http-accessible, if you don't need to restrict access.例如,如果您不需要限制访问,将文件放在可通过 http 访问的目录中可能会非常好。 Then you don't need to pass the file through any code, you just link directly to it.然后你不需要通过任何代码传递文件,你只是直接链接到它。

There are also legitimate reasons to store a file in the database.将文件存储在数据库中也有正当理由。 For instance, it's automatically included in backups, it is guaranteed to be deleted when you delete the database row, it obeys transaction semantics, and so on.例如,它自动包含在备份中,保证在您删除数据库行时将其删除,它遵守事务语义,等等。

This has been asked frequently on Stack Overflow.这在 Stack Overflow 上经常被问到。 Here are a few links to ones that I have answered.以下是一些我已经回答过的链接。

I also cover this in the chapter "Phantom Files" in my book, SQL Antipatterns Volume 1: Avoiding the Pitfalls of Database Programming .我也在我的书SQL 反模式第 1 卷:避免数据库编程的陷阱 中的“幻影文件”一章中介绍了这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM