我正在使用NodeJS上传文件。我的要求是将流读入一个变量,以便将其存储到AWS SQS中。我不想将文件存储在磁盘上。这个是可能的吗?我只需要上传到流中的文件。我使用的代码是(upload.js):
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);
};如何获取上传的流?
发布于 2017-07-30 06:50:28
在busboy 'file‘事件中,你会得到名为'file’的参数,这是一个流,所以你可以通过管道传输它。
例如
busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
file.pipe(streamToSQS)
}发布于 2017-08-11 14:36:38
我希望这会对你有所帮助。
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);发布于 2018-10-22 21:48:49
虽然current和existing参数假设人们实际上可以将流( file )发送到可以接收流的某个地方,但实际的块是在您实现的file回调方法中接收的。
来自文档:(https://www.npmjs.com/package/busboy)
file.on('data', function(data) {
// data.length bytes seems to indicate a chunk
console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
});
file.on('end', function() {
console.log('File [' + fieldname + '] Finished');
});
更新:
找到构造函数文档,第二个参数是一个可读的流。
文件(< string >字段名,< ReadableStream >流,< string >文件名,< string >传输编码,< string >mimeType) -为找到的每个新文件格式字段发出。transferEncoding包含文件流的“Content-Transfer-Encoding”值。mimeType包含文件流的“Content-Type值”。
https://stackoverflow.com/questions/43787534
复制相似问题