The NoSQL MongoDB (2) --- GridFS use large files

.GridFS introduce a GridFS is the solution MongoDB binary data stored in a database for handling large files. GridFS not MongoDB own characteristics, MongoDB does not implement its code. GridFS just how to deal with the development of large files in the database, is done to store and retrieve large files via API interface driven by the development of language. By design, MongoDB document (BSON objects) can not exceed 16M, which is to maintain the highest performance level. If the document is more than 16M, when the inquiry will take up a lot of memory. GridFS specify a mechanism to split a large file into multiple documents. Achieved by developing language extensions, e.g. php extension, when the storage block is stored, when retrieving the combined block. Developers do not need to know the internal details, storage and processing file is a transparent and efficient manner. GridFS stored in two separate collection: files and blocks. The basic idea is for each file is stored in GridFS. File will have a document that contains file name, size, upload time and other user-defined metadata. A content stored in a file or in a plurality of document blocks. PHP is 256Kbyte size block. Diagram below: Second 1. implemented using php upload page:
# 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. Store # 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>
# Micro 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();

?>
Use getBytes there is a potential problem, the entire contents of the file is loaded into memory. If you read the poor performance of large files this way. GridFS block is stored in the file, then the individual may be read and output from each of the blocks, thereby avoiding the above problem. In view of this, sometimes you can file segmentation to achieve HTTP.

Reproduced in: https: //my.oschina.net/766/blog/211142

Guess you like

Origin blog.csdn.net/weixin_33774615/article/details/91547488