Javascript 如何在S3存储桶中存储图像?
在我的express应用程序中,我将图像保存到repo中的一个文件夹中 但是我不认为这是正确的,我应该将它们存储在其他地方,我认为s3将是一个不错的选择 我以前从未做过,所以我不知道它是如何工作的 我的当前代码在缩放图像后保存图像:Javascript 如何在S3存储桶中存储图像?,javascript,node.js,express,amazon-s3,Javascript,Node.js,Express,Amazon S3,在我的express应用程序中,我将图像保存到repo中的一个文件夹中 但是我不认为这是正确的,我应该将它们存储在其他地方,我认为s3将是一个不错的选择 我以前从未做过,所以我不知道它是如何工作的 我的当前代码在缩放图像后保存图像: exports.resize = async (req, res, next) => { // check if there is no new file to resize if (!req.file) { next(); // skip t
exports.resize = async (req, res, next) => {
// check if there is no new file to resize
if (!req.file) {
next(); // skip to the next middleware
return;
}
const extension = req.file.mimetype.split('/')[1]
req.body.photo = `${uuid.v4()}.${extension}`
// now we resize
const photo = await jimp.read(req.file.buffer)
await photo.cover(300, 300);
// await photo.resize(800, jimp.AUTO);
await photo.write(`./public/uploads/${req.body.photo}`);
// once we have written the photo to our filesystem, keep going!
next()
};
将图像保存到aws s3的过程是什么
谢谢您可以在npm上使用
从这里,您需要配置包以连接到存储桶
有些用途是:
const AWS = require('aws-sdk');
const fs = require('fs');
const s3 = new AWS.S3({
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
});
\
您可以在nodeJS中将映像转换为base64,以便稍后发送
还有一个常规上传方法,写着:
const uploadFile = () => {
fs.readFile(fileName, (err, data) => {
if (err) throw err;
const params = {
Bucket: 'testBucket', // bucket name
Key: 'image.png', // filename
Body: JSON.stringify(data, null, 2)
};
s3.upload(params, function(s3Err, data) {
if (s3Err) throw s3Err;
console.log(`File uploaded successfully at ${data.Location}`);
});
});
};
参考:您可以在npm上使用
从这里,您需要配置包以连接到存储桶
有些用途是:
const AWS = require('aws-sdk');
const fs = require('fs');
const s3 = new AWS.S3({
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
});
\
您可以在nodeJS中将映像转换为base64,以便稍后发送
还有一个常规上传方法,写着:
const uploadFile = () => {
fs.readFile(fileName, (err, data) => {
if (err) throw err;
const params = {
Bucket: 'testBucket', // bucket name
Key: 'image.png', // filename
Body: JSON.stringify(data, null, 2)
};
s3.upload(params, function(s3Err, data) {
if (s3Err) throw s3Err;
console.log(`File uploaded successfully at ${data.Location}`);
});
});
};
参考:
创建文件上载服务
创建AmazonS3的一个实例
设置上载图像的路由
更多参考:
创建文件上载服务
创建AmazonS3的一个实例
设置上载图像的路由
更多参考资料:您好,请看一下我的问题,非常感谢您的帮助。您好,请看一下我的问题,非常感谢您的帮助。
const express = require("express");
const router = express.Router();
const upload = require('../services/multer');
const singleUpload = upload.single('image')
router.post('/image-upload', function(req, res) {
singleUpload(req, res, function(err, some) {
if (err) {
return res.status(422).send({errors: [{title: 'Image Upload Error', detail: err.message}] });
}
return res.json({'imageUrl': req.file.location});
});
})
module.exports = router;