我来了将文件写入gridfs的acorss片段,但是我无法找到将字符串更新为Gridfs的写方式。
下面的代码片段将使用路径更新,但是直接字符串缓冲区呢?
var metadata = {
"path": path
};
var writestream = gfs.createWriteStream({
filename: name,
mode: 'w',
content_type: type,
metadata: metadata
});
fs.createReadStream(path).pipe(writestream);
// var buf = new Buffer("hello");
writestream.on('close', function (file) {
console.log("Gridfs created");
});
答案 0 :(得分:1)
Even though it's been a while. I had this problem and solved it creating a stream from the string instead of a file. Like this:
var writestream = gfs.createWriteStream({
filename: fileName
});
// Create stream with buffer to pipe to writestream
var s = new stream.Readable();
s.push(pic);
s.push(null); // Push null to end stream
s.pipe(writestream);
writestream.on('close', function (file) {
// Do anything with the file
cb(null, file.filename);
}).on('error', cb);
Got the stream idea from here
答案 1 :(得分:1)
搜索如何使用NodeJS在Mongo GridFS中存储字符串数据时,将首先显示此页面。由于此处和其他地方的大多数答案都基于不赞成使用的API,因此我决定发布基于最新GridFSBucket的代码。该代码还有一个额外的规定来覆盖现有文件。如果不需要-您可以将openUploadStreamWithId替换为openUploadStream,以创建重复项。
var mongo = require('mongodb');
var stream = require('stream');
var MongoClient = mongo.MongoClient;
function getFileSystemItem(dbo, filename) {
var buf = new Buffer('');
return new Promise(function(resolve, reject) {
var bucket = new mongo.GridFSBucket(dbo);
var readstream = bucket.openDownloadStream(filename);
readstream.on('data', (chunk) => {
buf = Buffer.concat([buf, chunk]);
});
readstream.on('error', (err) => {
reject(err);
});
readstream.on('end', () => {
var res = buf.toString();
buf = null; // Clean up memory
readstream.destroy();
resolve(res);
});
});
}
function putFileSystemItem(dbo, filename, data) {
var putItemHelper = function(bucket, resolve, reject) {
var writeStream = bucket.openUploadStreamWithId(filename, filename);
var s = new stream.Readable();
s.push(data);
s.push(null); // Push null to end stream
s.pipe(writeStream);
writeStream.on('finish', resolve);
writeStream.on('error', reject);
};
return new Promise(function(resolve, reject) {
var bucket = new mongo.GridFSBucket(dbo);
bucket.find({_id: filename}).count(function(err, count) {
if (err) return reject(err);
if (count > 0) {
bucket.delete(filename, function() {
putItemHelper(bucket, resolve, reject);
}, reject)
} else {
putItemHelper(bucket, resolve, reject);
}
}, reject);
});
}
function test() {
var MongoUrl = 'mongodb://localhost:27017';
var dbName = 'test';
MongoClient.connect(MongoUrl, function(err, db) {
if (err) throw err;
var dbo = db.db(DbName);
putFileSystemItem(dbo, 'test', 'this is a test')
.then(function(a) {
console.log('Wrote a gridFS file with metadata:', a);
return getFileSystemItem(dbo, 'test')
})
.then(function(a) {
console.log('Got data back from a gridFS file:', a);
db.close();
})
.catch(function(e) {
console.log(e);
db.close();
})
});
}
test();
信用:它是由其他十几个StackOverflow页面和MongoDB API帮助组成的。
答案 2 :(得分:0)
我遇到了同样的问题。我只是使用gfs.remove删除文件,然后将更新的文件添加到GridFS。
另外,仅供参考。我使用async.series首先完成删除操作,然后将更新的文件写入GridFS。
希望这有帮助!