Get size of folder and all contents for each user
I have seen in several SAAS sites where people store data and the site tracks how much data is on their servers. They display that information to the user.
Using ruby on rails how do some of these sites do this? It seems like enough sites do it that there must be a standard way I am just not aware of. Or is it everyone pretty much implements their own solut开发者_JAVA技巧ion?
If everyone has implemented their own solution then is this a good appraoch using:
`du -s <directory>`
and just parsing the data.
All depends on how the site is storing the actual data, and meta-data about the objects that its users upload to it. It is entirely possible that the Data isn't being stored on a traditional file system (such as S3). So in cases that like du
wouldn't work.
So if you store the meta data in the database, including the size, you can just get the sum of the upload size via a query, and not have to have hit the underlying filesystem...
du
is a very good tool for this sort of thing, it's probably faster than what you could roll by hand using Find, but you'll need to be careful when parsing the output.
It's not uncommon for directories to contain exotic characters in their names which includes the obvious like spaces, and the unusual like newlines. This makes parsing the output of du
somewhat unreliable if someone does this:
% mkdir "foo
1234 bar"
If that's not a big deal, forget about it. Otherwise you'll need to recurse and do the math manually, something that can take a while for the Ruby interpreter on large filesystems.
精彩评论