我正在使用穆特上传媒体到我的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);我知道我需要发送一些多部分/表格部分,但我无法找到解决方案。如果你建议我一件很棒的事。
发布于 2020-11-23 15:15:31
选项1 -使用穆特-S3变换中间件处理文件转换的S3上载,比如使用锐利调整图像大小。
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")
})
})发布于 2020-11-22 09:16:18
穆特是一个勤杂工包装。您可以使用busboy而不是multer来处理请求,并进行所需的所有操作。
const Busboy = require('busboy');
router.post('/media', function(req, res) {
var busboy = new Busboy({ headers: req.headers });
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
console.log('File ' + filename + ' upload incoming');
file.on('end', function() {
console.log('File ' + filename + ' upload finished');
// make all you want, eg:
let thumbnail = myFunctionToReturnImageFile(file);
upload(thumbnail);
res.send('file uploaded');
});
});
});发布于 2020-11-21 07:55:07
您可以尝试使用Node.js图像处理(如sharp )来调整大小,然后上传到S3。
sharp(req.file)
.resize(320, 240)
.toBuffer()
.then(thumbnail => { upload(thumbnail) })
.catch( err => { ... });https://stackoverflow.com/questions/64900983
复制相似问题