0

I'm new to node.js and i'm trying to make an application which saves photos of users just like a normal application. The user can set a profile picture and could add other pictures as well in their wall.

I'm also done with other parts of my application, but I'm trying to figure out what would be the best way to save those images -- since my application should be able to scale the number of users to a big number.

I referenced : How to upload, display and save images using node.js and express ( to save images on server)

and also: http://blog.robertonodi.me/managing-files-with-node-js-and-mongodb-gridfs/ (to save images on mongo via grid-fs)

and I'm wondering what would be the best option.

So, could you please suggest what I must be rooting for?

Thanks,

Community
  • 1
  • 1
appsdownload
  • 751
  • 7
  • 20

1 Answers1

0

It depends on your application needs, one thing I've did for a similar applications was to create an abstraction over the file storage logic of the server application.

var DiskStorage = require('./disk');
var S3Storage = require('./s3');
var GridFS = require('./gridfs');

function FileStorage(opts) {
  if (opts.type === 'disk') {
    this.storage = new DiskStorage(opts);
  }

  if (opts.type === 's3') {
    this.storage = new S3Storage(opts);
  }

  if (opts.type === 'gridfs') {
    this.storage = new GridFS(opts);
  }
}

FileStorage.prototype.store = function(opts, callback) {
  this.storage.store(opts, callback);
}

FileStorage.prototype.serve = function(filename, stream, callback) {
  this.storage.serve(filename, stream, callback);
}

module.exports = FileStorage;

Basically you will have different implementation for your logic to store user uploaded content. And when you need it you can just scale from your local file storage/mongo gridfs to a S3 maybe. But for a seamless transition when you store the user file relationship in your database you could store also the file provider, local or S3.

Saving images directly to the local file system can be sometimes a little bit complicated when we are talking about many uploaded content, you could easily run into limitations like How many files can I put in a directory?. GridFS should not have such a problem, I've had pretty good experience using MongoDB for file storage, but this depends from application to application.

Community
  • 1
  • 1
Robert Onodi
  • 120
  • 5