首页 > 解决方案 > 如何在上传之前将 MulterS3 文件流传递/管道到另一个函数

问题描述

我正在运行一个小型 Node.js express 应用程序,它使用multer-s3-transform和 sharp 包以及simple-thumbnail包将图像和视频上传到我的 S3 存储桶。我的代码非常适合图像,图像通过,调整大小,然后继续上传到我的存储桶。但是,我正在尝试使用 simple-thumbnail 将视频缩略图上传到我的 s3 存储桶,但我不确定如何将流传递给这个包的 genThumbnail 函数。在文档中它指出您可以将流传入和传出它,想知道如何做到这一点。

关注的包:
https ://github.com/gmenih341/multer-s3
https://github.com/ScottyFillups/simple-thumbnail

const express = require('express');
const app = express();
const aws = require('aws-sdk');
const multerS3 = require('multer-s3');
const sharp = require('sharp');
const ffmpeg = require('ffmpeg-static');
const genThumbnail = require('simple-thumbnail');

let s3 = new aws.S3({});


let upload = multer({
    storage: multerS3({
        s3: s3,
        bucket: MY_BUCKET,
        acl: 'public-read',
        cacheControl: 'max-age=31536000',
        shouldTransform: true,
        contentType: multerS3.AUTO_CONTENT_TYPE,
        transforms: [{
            id: 'original',
            key: function (req, file, cb) {
                let fileName = Date.now().toString();
                cb(null, fileName + '-original')
            },
            transform: function (req, file, cb) {
                if (file.mimetype == 'video/mp4') {
                    //HERE IS THE PROBLEM, first two arguments of genThumbnail can take in a stream.Readable, and stream.Writable
                    cb(null, genThumbnail(null, null, '150x100', { path: ffmpeg.path }))
                } else {
                    //THIS WORKS (WHEN NOT VIDEO)
                    cb(null, sharp().jpeg())
                }

            }
        }]
    })
}); 

app.post('/upload', upload.array('theFile'), (req, res) => {
    //response
});

我希望genThumbnail()创建一个缩略图并继续沿文件流传递,就像在使用 sharp() 的图像的工作情况下一样。

标签: node.jsexpressmulter-s3

解决方案


推荐阅读