gpt4 book ai didi

node.js - Multer-S3 具有锐利的上传和调整功能

转载 作者:太空宇宙 更新时间:2023-11-03 22:20:03 24 4
gpt4 key购买 nike

我已经能够使用 multer-s3 将图像文件上传到 s3库,但现在我尝试添加 sharp图书馆要调整大小和旋转,但无法弄清楚它需要去哪里或如何去。这是我当前用于上传的代码:

var options = {
'endpoint' : 'https://xxx',
'accessKeyId' : '123',
'secretAccessKey' : '123abc',
'region' : 'xxx'
};

const s3 = new aws.S3(options);

const upload = multer({
storage: multerS3({
s3: s3,
bucket: 'wave',
acl: 'public-read',
metadata: function (req, file, cb) {
cb(null, Object.assign({}, req.body));
},
key: function (request, file, cb) {
console.log('inside multerS3',file);
cb(null, file.originalname);
}
})
}).array('file', 1);

app.post('/upload/', (req,res) => {
upload(req,res, async function (error) {
if(error){
res.send({ status : error });
}else{
console.log(req.files[0]);
res.send({ status : 'true' });
}
});
});

并对文件执行类似的操作:

 sharp(input)
.resize({ width: 100 })
.toBuffer()
.then(data => {
// 100 pixels wide, auto-scaled height
});

任何帮助将不胜感激:)

最佳答案

我在 Sharp 支持下尝试了很多不同的 multer-s3 软件包,但没有一个对我有用,所以我决定使用 multeraws-sdk锐利

这是一个有效的 Express.js 示例。

const path = require('path')
const sharp = require('sharp')
const AWS = require('aws-sdk')
const multer = require('multer')
const express = require('express')
require('dotenv').config({ path: path.join(__dirname, '.env') })

const { AWS_BUCKET, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_ENDPOINT } = process.env

const app = express()
const port = 9000

app.use(express.json())
app.listen(port, () => {
console.log('Example app listening on port http://localhost:' + port)
})

app.get('/', (req, res) => {
res.send(`
<form action='/upload' method='post' enctype='multipart/form-data'>
<input type='file' name='file' multiple />
<input type='submit' value='upload' />
</form>
`)
})

const sharpify = async originalFile => {
try {
const image = sharp(originalFile.buffer)
const meta = await image.metadata()
const { format } = meta
const config = {
jpeg: { quality: 80 },
webp: { quality: 80 },
png: { quality: 80 }
}
const newFile = await image[format](config[format])
.resize({ width: 1000, withoutEnlargement: true })
return newFile
} catch (err) {
throw new Error(err)
}
}

const uploadToAWS = props => {
return new Promise((resolve, reject) => {
const s3 = new AWS.S3({
accessKeyId: AWS_ACCESS_KEY_ID,
secretAccessKey: AWS_SECRET_ACCESS_KEY,
endpoint: new AWS.Endpoint(AWS_ENDPOINT)
})
s3.upload(props, (err, data) => {
if (err) reject(err)
resolve(data)
})
})
}

app.post('/upload', multer().fields([{ name: 'file' }]), async (req, res) => {
try {
const files = req.files.file
for (const key in files) {
const originalFile = files[key]

const newFile = await sharpify(originalFile)

await uploadToAWS({
Body: newFile,
ACL: 'public-read',
Bucket: AWS_BUCKET,
ContentType: originalFile.mimetype,
Key: `directory/${originalFile.originalname}`
})
}

res.json({ success: true })
} catch (err) {
res.json({ success: false, error: err.message })
}
})

关于node.js - Multer-S3 具有锐利的上传和调整功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57864113/

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