- A+
一.GridFS介绍
GridFS是MongoDB的二进制数据存储在数据库中的解决方案,用来处理大文件。GridFS不是MongoDB自身特性,MongoDB没有实现它的代码。GridFS只是制定大文件在数据库中如何处理,是通过开发语言驱动来完成和通过API接口来存储检索大文件。
按照设计,MongoDB文档(BSON对象)不能超过16M,这是为了使性能保持在最高水平。如果文档超过16M,当查询时将占用大量的内存。
GridFS指定了将一个大文件分割成多个文档的机制。通过开发语言扩展来实现,例如php扩展,在存储时,分块存储,在检索时,合并分块。
开发人员无需知道内部细节,存储和处理文件是一个透明高效的方式。
GridFS存储在两个独立的集合中:文件和块。基本的想法是为每一个文件被存储在GridFS。文件将有一个文档包含文件名,大小,上传时间以及其他用户定义的元数据。文件的内容存储在一个或多个文档块中。 PHP是以256Kbyte大小来分块。
示意图如下:
二.使用php来实现
1.上传页面:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# vi upload.html <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>Upload Files</title> </head> <body> <h2>Select files to upload</h2> <form enctype="multipart/form-data" action="/store.php" method="post"> <input type="file" name="file"><br> <input type="submit" name="submit" value="Upload"> </form> </body> </html> |
2.存储
# vi store.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?php $host="127.0.0.1"; $port="27017"; $dbname="ttlsa"; $coname="ttlsa_com"; if($_FILES['file']['error'] !== 0){ die('Error upload file. Error code '.$_FILES['file']['error']); } $filename=$_FILES['file']['name']; $filetype=$_FILES['file']['type']; $tmpfilepath=$_FILES['file']['tmp_name']; $conn = new Mongo("mongodb://".$host.":".$port,array('timeout'=>100)); $database = $conn->selectDB($dbname); $collection = $database->selectCollection($coname); $gridfs=$database->getGridFS(); $id=$gridfs->storeFile($tmpfilepath,array('filename'=>$filename,'filetype'=>$filetype)); echo "File Uploaded. ID: ".$id."\n"; ?> |
使用mongo shell
> use ttlsa
switched to db ttlsa
> show collections
fs.chunks
fs.files
system.indexes
> db.fs.files.findOne()
{
"_id" : ObjectId("4febfe966803fa3812000008"),
"filename" : "1.sh",
"filetype" : "application/x-shellscript",
"uploadDate" : ISODate("2012-06-28T06:49:58.397Z"),
"length" : 2437,
"chunkSize" : 262144,
"md5" : "e5e5966456777722d68f7104b75cc461"
}
> db.fs.chunks.find({files_id:ObjectId("4febfe966803fa3812000008")})
{ "_id" : ObjectId("4febfe966803fa3812000009"), "files_id" : ObjectId("4febfe966803fa3812000008"), "n" : 0, "data" : BinData(2,"......此处省略=") }
3.读取
# vi list.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
<?php $host="127.0.0.1"; $port="27017"; $dbname="ttlsa"; $conn = new Mongo("mongodb://".$host.":".$port,array('timeout'=>100)); $database = $conn->selectDB($dbname); $gridfs=$database->getGridFS(); $objects=$gridfs->find(); ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>List Upload Files</title> </head> <body> <h2>List upload files from GridFS</h2> <table class="table-list" cellspacing="0" cellpadding="3" border="1"> <tr> <th width="20%">Filename</th> <th width="30%">MD5</th> <th width="18%">uploadDate</th> <th width="10%">chunkSize</th> <th width="*">Size</th> </tr> <?php while($object=$objects->getNext()) : ?> <tr> <td> <a href="view.php?id=<?php echo $object->file['_id'];?>"><?php echo $object->file['filename'];?> </a> </td> <td> <?php echo $object->file['md5'];?> </td> <td> <?php echo date('Y-m-d H:i:s',$object->file['uploadDate']->sec);?> </td> <td> <?php echo ceil($object->file['chunkSize']/1024).' KB';?> </td> <td> <?php echo ceil($object->file['length']/1024).' KB';?> </td> </tr> <?php endwhile ;?> </table> </body> </html> |
# vi view.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php $host="127.0.0.1"; $port="27017"; $dbname="ttlsa"; $id=$_GET['id']; $conn = new Mongo("mongodb://".$host.":".$port,array('timeout'=>100)); $database = $conn->selectDB($dbname); $gridfs=$database->getGridFS(); $object=$gridfs->findOne(array('_id'=>new MongoId($id))); header('Content-type: '.$object->file['filetype']); echo $object->getBytes(); ?> |
使用getBytes会有一个潜在的问题,将文件内容全部加载到内存中。如果读取大文件这种方式性能差。GridFS是将文件分块存储的,那么可以单独的从每个块读取和输出,从而避免上述问题。
鉴于此,有时候可以将文件分块来实现断点续传。

来自外部的引用: 1