Mira, lo mejor que puedes hacer es simplemente subir la imagen al disco y guardar la URL en MongoDB. Descanse cuando recupere la imagen nuevamente. Simplemente especifique la URL y obtendrá una imagen. El código para cargar es el siguiente.
app.post('/upload', function(req, res) {
// Get the temporary location of the file
var tmp_path = req.files.thumbnail.path;
// Set where the file should actually exists - in this case it is in the "images" directory.
target_path = '/tmp/' + req.files.thumbnail.name;
// Move the file from the temporary location to the intended location
fs.rename(tmp_path, target_path, function(err) {
if (err)
throw err;
// Delete the temporary file, so that the explicitly set temporary upload dir does not get filled with unwanted files.
fs.unlink(tmp_path, function() {
if (err)
throw err;
//
});
});
});
Ahora guarde la ruta de destino en su base de datos MongoDB.
Nuevamente, mientras recupera la imagen, simplemente extraiga la URL de la base de datos MongoDB y úsela en este método.
fs.readFile(target_path, "binary", function(error, file) {
if(error) {
res.writeHead(500, {"Content-Type": "text/plain"});
res.write(error + "\n");
res.end();
}
else {
res.writeHead(200, {"Content-Type": "image/png"});
res.write(file, "binary");
}
});
npm install multer --save
y luego en su aplicación puede accederreq.files.your_file_param_name
y guardar en s3 conaws-sdk
ofs.writeFile(...)