2017-05-04 5 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.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); 
関連する問題