2017-05-04 160 views
0

我使用NodeJS上传文件。我的要求是将流读入一个变量,以便将其存储到AWS SQS中。我不想将文件存储在磁盘上。这可能吗?我只需要上传的文件进入流。我正在使用的代码是(upload.js):使用NodeJS和BusBoy上传文件

var http = require('http'); 
var Busboy = require('busboy'); 

module.exports.UploadImage = function (req, res, next) { 

    var busboy = new Busboy({ headers: req.headers }); 

    // Listen for event when Busboy finds a file to stream. 
    busboy.on('file', function (fieldname, file, filename, encoding, mimetype) { 

     // We are streaming! Handle chunks 
     file.on('data', function (data) { 
       // Here we can act on the data chunks streamed. 
     }); 

     // Completed streaming the file. 
     file.on('end', function (stream) { 
      //Here I need to get the stream to send to SQS 
     }); 
    }); 

    // Listen for event when Busboy finds a non-file field. 
    busboy.on('field', function (fieldname, val) { 
      // Do something with non-file field. 
    }); 

    // Listen for event when Busboy is finished parsing the form. 
    busboy.on('finish', function() { 
     res.statusCode = 200; 
     res.end(); 
    }); 

    // Pipe the HTTP Request into Busboy. 
    req.pipe(busboy); 
}; 

如何获取上传的流?

回答

0

在busboy'文件'事件上,你得到一个名为'file'的参数,这是一个流,所以你可以管它。

例如

busboy.on('file', function (fieldname, file, filename, encoding, mimetype) { 
    file.pipe(streamToSQS) 
} 
0

我希望这将帮助你。

busboy.on('file', function (fieldname, file, filename, encoding, mimetype) { 
    var filename = "filename"; 
    s3Helper.pdfUploadToS3(file, filename); 
    } 
busboy.on('finish', function() { 
     res.status(200).json({ 'message': "File uploaded successfully." }); 
    }); 
    req.pipe(busboy);