数据库学习——用mongo存储小文件

可以用mongodb自带的GridFS实现一个简单的分布式小文件存储系统,mongo提供命令行工具mongofiles以及各种语言的驱动提供GridFS操作的接口。

 

1,mongofiles操作文件

 

写文件到GridFS

 

$ mongofiles.exe put test.txt -d ciaos -l d:\test.txt
connected to: 127.0.0.1
added file: { _id: ObjectId('50b436be961c6ba1158ef731'), filename: "test.txt", chunkSize: 262144, uploadDate: new Date(1353987774155), md5: "126f336a523c7bc84f25aea99d617e97", length: 11 }
done!

$ mongo.exe
MongoDB shell version: 2.2.1
connecting to: test
use ciaosuse ciaos
switched to db ciaos
> db.fs.files.find()db.fs.files.find()
{ "_id" : ObjectId("50b436be961c6ba1158ef731"), "filename" : "test.txt", "chunkSize" : 262144, "uploadDate" : ISODate("2012-11-27T03:42:54.155Z"), "md5" : "126f336a523c7bc84f25aea99d617e97", "length" : 11 }

从GridFS读文件

 

$ mongofiles.exe get test.txt -d ciaos -l d:\local.txt
connected to: 127.0.0.1
done write to: d:\local.txt
 

2,php驱动操作文件

 

<?php

//put a file
$mongo = new Mongo();
$db = $mongo->selectDB("ciaos");
$gridfs = $db->getGridFS();

$mid = $gridfs->put("D://local.txt");

var_dump($mid);
/*
object(MongoId)#5 (1) {
  ["$id"]=>
  string(24) "50b437e1959d706424000001"
}
*/

//get a file
$fsfile = $gridfs->get($mid);

var_dump($fsfile->getFilename());
//string(13) "D://local.txt"
var_dump($fsfile->getSize());
//int(11)
var_dump($fsfile->getBytes());
//string(11) "this is txt"

?>

我们可以使用mongo的集群配置文件的副本数目简单实现一个小文件的分布式存储系统。

你可能感兴趣的:(mongo gridfs)