node.js - 如何在上传之前将 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() 的图像的工作情况下一样。
解决方案
推荐阅读
- javascript - Next.js 服务器继续运行,但浏览器中大约 20 分钟没有显示任何内容
- excel - 将信息粘贴到不同的工作表中(希望指定从每个工作表的单元格 A1 开始)
- python - 我在“TypeError”中犯了什么错误?为什么它在除了部分的变量名中显示“可以未定义”?
- java - 从类创建对象时,仅为创建的第一个对象设置不同的默认值?
- android - BiometricManager.canAuthenticate() = BIOMETRIC_SUCCESS 但 BiometricPrompt.AuthenticationCallback() 在锁定时返回 errorCode 9
- google-tag-manager - FormSubmit 获取表单的数据属性
- kotlin - 最好地实现对调用层透明地发生的批处理 api 请求的设计模式
- c++ - Is there a way to seclude a loop in c++?
- bar-chart - Recharts 条形图显示带有“自动”标签的空条
- c# - 为什么带有 ADFS 的 OpenId 突然停止工作?