upload storage: database vs. file system

while reading through the agile web development book, i was kind of surprised to see that uploaded files were stored in the database. maybe this isn't anything new, but it was the first time i had seen file uploads handled this way. it seems like that's the way most of the articles and tutorials i've found have shown to do it. why is this? is there something better about storing the file in the database, or is it for convenience?

sorry if this has been posted a hundred times before, there was nothing i could find on why most rails apps handle files this way.

I don’t know that I’d say most rails apps handle files this way. Certainly there are some situations where this is done, but I would hazzard a guess that this is a minority of times.

ActsAsAttachment defaults to filesystem, but gives the option of db storeage. I guess it’s up to personal preference.

There have been some very large threads on the rails list a while ago regarding this that went into a lot of detail.

A very quick search at www.ruby-forum.com turned up

http://www.ruby-forum.com/topic/54239#33224

http://www.ruby-forum.com/topic/71824#100751

Hope that helps

Daniel N wrote:

    while reading through the agile web development book, i was kind of     surprised to see that uploaded files were stored in the database.     maybe     this isn't anything new, but it was the first time i had seen file     uploads handled this way. it seems like that's the way most of the     articles and tutorials i've found have shown to do it. why is this? is     there something better about storing the file in the database, or     is it     for convenience?

    sorry if this has been posted a hundred times before, there was     nothing     i could find on why most rails apps handle files this way.

I don't know that I'd say *most* rails apps handle files this way. Certainly there are some situations where this is done, but I would hazzard a guess that this is a minority of times.

ActsAsAttachment defaults to filesystem, but gives the option of db storeage. I guess it's up to personal preference.

There have been some very large threads on the rails list a while ago regarding this that went into a lot of detail.

A very quick search at www.ruby-forum.com <http://www.ruby-forum.com> turned up

Db vs filesystem - Rails - Ruby-Forum

Upload file in a database or a server? - Rails - Ruby-Forum

Hope that helps

I imagine the points if discussion are: 1. If it's in the DB, it scales well - when you move to a multi-server architecture for hosting your application, the data from the DB will be available to all processes.. whereas you may have some concerns if the file you want is stored on the file system of one server.

2. If it's in the DB, it's very easy to protect access to the data by using a controller. It's slightly (not much) more complicated if you're storing it on the file system.

3. I imagine that if you do not want to protect access to your file (i.e., it is stored in a publicly accessible location), then it may be slightly faster to serve it up as 'static content' by getting the webserver to send it out rather than sending it through the application server.

However, I may be wrong in all the points above.. :smiley:

Cheers Mohit.

> ActsAsAttachment defaults to filesystem, but gives the option of db > storeage. I guess it's up to personal preference.

I actually wrote it for db-backed storage because file_column did the file stuff.

In my experience, it's usually better to keep the files on disk unless you're talking really small or dynamic files. The rails send_file() method will stream larger files in chunks, rather than requiring you to load the whole file in memory and send it. Private files should of course be saved outside of the public path too.

If you are using db files, you could employ some kind of page caching that writes the asset to the hard drive in some cached location the first time and serves that on future requests.