gpt4 book ai didi

node.js - 使用不带中间件功能的multer上载到S3

转载 作者:行者123 更新时间:2023-12-03 12:12:13 25 4
gpt4 key购买 nike

我正在使用multer将媒体上传到我的s3存储桶。我正在使用multer-s3作为中间件来上传媒体,例如:

const upload = multer({
storage: multerS3({
s3: s3,
bucket: myBucket,
key: function (req, file, cb) {
cb(null, new Date().getTime() + '_' + file.originalname)
}
})
});
并在路由中将其称为:
router.post("/media",upload.single("media"))
这很好用。但是,对我不起作用的情况是:假设我上传了一个图像,并且我想通过在上载之前调整其大小来存储图像的多个版本。我无法像正常情况那样调用上传功能。我想做类似的事情:
let thumbnail = myFunctionToReturnImageFile(req.file);
upload(thumbnail);
我知道我需要发送一些多部分/形式部分,但我找不到解决方案。如果您建议我做某事,那将会很棒。

最佳答案

选项1 -使用multer-s3-transform中间件通过文件转换来处理S3上传,例如使用sharp调整图像大小。

const multer = require('multer')
const multerS3 = require('multer-s3-transform')
const sharp = require('sharp')
const AWS = require('aws-sdk')
const S3 = new AWS.S3({
accessKeyId: ...,
secretAccessKey: ...
})

const upload = multer({
storage: multerS3({
s3: S3,
bucket: ...,
shouldTransform: true,
transforms: [
{
id: 'original',
key: (req, file, cb) => cb(null, new Date().getTime() + '_' + req.file.originalname),
transform: (req, file, cb) => cb(null, sharp().jpg())
},
{
id: 'large',
key: (req, file, cb) => cb(null, new Date().getTime() + '_large_' + req.file.originalname),
transform: (req, file, cb) => cb(null, sharp().resize(1200, 900).jpg())
},
{
id: 'small',
key: (req, file, cb) => cb(null, new Date().getTime() + '_small_' + req.file.originalname),
transform: (req, file, cb) => cb(null, sharp().resize(400, 300).jpg())
}
]
})
})

router.post('/media', upload.single('media'))
选项2 -如果您坚持手动操作,则可以使用简单的 multer处理传入的文件,使用 sharp调整其大小,然后使用 AWS-SDK将每个调整大小的文件上载到S3。
const multer = require('multer')
const sharp = require('sharp')
const AWS = require('aws-sdk')
const S3 = new AWS.S3({
accessKeyId: ...,
secretAccessKey: ...
})

router.post('/media', multer().single('media'), (req, res) => {
// req.file represents the uploaded file
let time = new Date().getTime()

Promise.all([
// original file
S3.upload({
Bucket: ...,
Key: time + '_' + req.file.originalname,
Body: req.file.buffer
}),

// large preview
sharp(req.file)
.resize(1200, 900)
.toBuffer()
.then(resized => S3.upload({
Bucket: ...,
Key: time + '_large_' + req.file.originalname,
Body: resized
}).promise()),

// small thumbnail
sharp(req.file)
.resize(400, 300)
.toBuffer()
.then(resized => S3.upload({
Bucket: ...,
Key: time + '_small_' + req.file.originalname,
Body: resized
}).promise()),
])
.then(() => res.send("Images uploaded"))
.catch(e => {
console.warn(e) // debug this error
res.status(500).send("Unable to upload images")
})
})

关于node.js - 使用不带中间件功能的multer上载到S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64900983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com