NoSQL之MongoDB(2)---使用GridFS处理大文件

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

转载于:https://my.oschina.net/766/blog/211142

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值