一.GridFS介绍
GridFS是MongoDB的二进制数据存储在数据库中的解决方案,用来处理大文件。GridFS不是MongoDB自身特性,MongoDB没有实现它的代码。GridFS只是制定大文件在数据库中如何处理,是通过开发语言驱动来完成和通过API接口来存储检索大文件。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-gridfs-process-bigfile/
按照设计,MongoDB文档(BSON对象)不能超过16M,这是为了使性能保持在最高水平。如果文档超过16M,当查询时将占用大量的内存。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-gridfs-process-bigfile/
GridFS指定了将一个大文件分割成多个文档的机制。通过开发语言扩展来实现,例如php扩展,在存储时,分块存储,在检索时,合并分块。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-gridfs-process-bigfile/
开发人员无需知道内部细节,存储和处理文件是一个透明高效的方式。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-gridfs-process-bigfile/
GridFS存储在两个独立的集合中:文件和块。基本的想法是为每一个文件被存储在GridFS。文件将有一个文档包含文件名,大小,上传时间以及其他用户定义的元数据。文件的内容存储在一个或多个文档块中。 PHP是以256Kbyte大小来分块。文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-gridfs-process-bigfile/
示意图如下:文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-gridfs-process-bigfile/
文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-gridfs-process-bigfile/
二.使用php来实现文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-gridfs-process-bigfile/
1.上传页面:文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-gridfs-process-bigfile/
# 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.存储文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-gridfs-process-bigfile/
# vi store.php文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-gridfs-process-bigfile/
<?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文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-gridfs-process-bigfile/
> use ttlsa文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-gridfs-process-bigfile/
switched to db ttlsa文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-gridfs-process-bigfile/
> show collections文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-gridfs-process-bigfile/
fs.chunks文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-gridfs-process-bigfile/
fs.files文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-gridfs-process-bigfile/
system.indexes文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-gridfs-process-bigfile/
> db.fs.files.findOne()文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-gridfs-process-bigfile/
{文章源自运维生存时间-https://www.ttlsa.com/mongodb/mongodb-gridfs-process-bigfile/
"_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
<?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
<?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是将文件分块存储的,那么可以单独的从每个块读取和输出,从而避免上述问题。
鉴于此,有时候可以将文件分块来实现断点续传。
来自外部的引用