我是gridfs mongodb的新手,并创建一个POST服务来接收视频的字节,并希望将字节保存在mongodb中。我研究过gridfs,但无法得到清晰的图片。我已经编写了代码,但我不知道它是否足以处理大文件。或者如何从设备接收字节并以gridfs存储在任何最佳方式?或任何其他方式来存储视频(字节)?很感谢任何形式的帮助。下面是代码。
var bytes = [];
bytes = req.param('video');
var db = new Db('myDB', new Server('localhost', 27017,{w:1}));
db.open(function(err, db) {
// Our file ID
var fileId = new ObjectID();
// Open a new file
var gridStore = new GridStore(db, fileId, 'w');
gridStore.open(function(err, gridStore) {
//Write a buffer
gridStore.write(new Buffer(bytes), function(err, gridStore) {
// Close the
gridStore.close(function(err, result) {
// Read back all the written content and verify the correctness
GridStore.read(db, fileId, function(err, fileData) {
console.log(fileData);
db.close();
});
});
});
// });
});
});
答案 0 :(得分:1)
我在mongodb中提供了base64字符串插入的示例。您可以使用它并将其应用于视频字节字符串。只需替换它的字符串。
var mongo = require('mongodb');
var Grid = require('gridfs-stream');
var streamifier = require('streamifier');
// create or use an existing mongodb-native db instance.
// for this example we'll just create one:
var db = new mongo.Db('mydb', new mongo.Server("127.0.0.1", 27017));
// make sure the db instance is open before passing into `Grid`
db.open(function(err) {
if (err)
return handleError(err);
var gfs = Grid(db, mongo);
var writestream = gfs.createWriteStream({
mode: 'w',
filename: 'dummy_video', // files.contentType (Optional. A valid MIME type for the document.)
content_type: 'video/mp4' // files.filename (Optional. A human-readable name for the document.)
});
var vidoBase64 = 'data:video/mp4;base64,iVBORw0KGgoAAAANSUhEUgAAACwAAAAsCAYAAAFpg2qXAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlI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JRUqOFiTwFAm2V2wy1jSHGgkK5ECx4Kau/loYveB+DA1HPwb6sC/aOKgUbKDQMxGomNBozubYUO1SACUeb9Sq/d8Bdq1eYsmbgdtScPGBUEhp4QkLwdNl61I44iBaGZ8hyaPw8Q4hJSK4ykQiwBxLDR00fUWQDEpIA/b6B+1xycWg1ro0M5MDxB/JEHvT+gwpRI0g00m1XJcIfyPCL0voCOPpUhiEtDhTxloYDyFjrO+INJMgm5gwVGUgNqjy8gI/RdQfJ0KxZ4LtmIP1IDXhmaqoQDAw41Xoa2q54PYoc+hbrzKiGXc1RRahIkwoM050gnA7HwDHQlCntZkAAD2FHBZPcvq3AAAAABJRU5ErkJggg==';//The base64 has a Video
// if you are getting this byte string into chunks you need to man
var byte_string = vidoBase64.substr(23);//The base64 has a imageURL
var buffer = new Buffer(byte_string); //new Buffer(b64string, 'base64'); you can use base64 encoding with creating new buffer string
var response = streamifier.createReadStream(buffer).pipe(writestream); // returns response which is having all information regarding saved byte string
var lastInsertedFileId = response._store.fileId; // now you can store it into another document for future use.
console.log(lastInsertedFileId); // this is _id saved in mydb.fs.files
//Now you can check mydb.fs.files and mydb.fs.chunks document.
});
现在,您可以通过以下代码---
提取相同输入数据的完整块 var output = '';
var readStream = gfs.createReadStream({
_id: lastInsertedFileId // this id was stored in db when inserted a video stream above
});
readStream.on("data", function(chunk) {
output += chunk;
});
// dump contents to console when complete
readStream.on("end", function() {
console.log("Final Output");
console.log(output);
});
根据您的要求修改代码。
由于