procoder
procoder

Reputation: 31

using gridfs-stream write string to gridfs

I came acorss snippet to write file to gridfs, but I am unable to find write way to update string to Gridfs.

the below snippet will update using path, but what about direct string buffer?

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");
});

Upvotes: 1

Views: 3031

Answers (3)

Maksym
Maksym

Reputation: 1508

This page comes up first when searching for how to store a string data in Mongo GridFS using NodeJS. Since most of the answers here and elsewhere are based on deprecated API, I decided to post a code based on a more recent GridFSBucket. The code has an additional provision to overwrite existing files. If you don't need that - you can replace openUploadStreamWithId with openUploadStream which creates duplicates.

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();

Credit: This is assembled from a dozen of other StackOverflow pages and MongoDB API help.

Upvotes: 3

fos.alex
fos.alex

Reputation: 5637

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

Upvotes: 1

user3658423
user3658423

Reputation: 1944

I was faced with the same problem. I just simply deleted the file using gfs.remove and then added the updated file to GridFS.

Also FYI. I used async.series to finish the delete operation first and then write the updated file to GridFS.

Hope this helps!

Upvotes: 0

Related Questions