Uploading image to amazon s3 using multer-s3 nodejs
我正在尝试使用
TypeError: Expected opts.s3 to be object
node_modules/multer-s3/index.js:69:20
这是我的服务器代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | var upload = multer({ storage: s3({ dirname: '/', bucket: 'bucket', secretAccessKey: 'key', accessKeyId: 'key', region: 'us-west-2', filename: function (req, file, cb) { cb(null, file.originalname); } }) }); app.post('/upload', upload.array('file'), function (req, res, next) { res.send("Uploaded!"); }); |
为什么我收到此错误?
完成并正常工作的Node Cheat |使用multer-s3上传到s3。
码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | var express = require('express'), aws = require('aws-sdk'), bodyParser = require('body-parser'), multer = require('multer'), multerS3 = require('multer-s3'); aws.config.update({ secretAccessKey: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', accessKeyId: 'XXXXXXXXXXXXXXX', region: 'us-east-1' }); var app = express(), s3 = new aws.S3(); app.use(bodyParser.json()); var upload = multer({ storage: multerS3({ s3: s3, bucket: 'bucket-name', key: function (req, file, cb) { console.log(file); cb(null, file.originalname); //use Date.now() for unique file keys } }) }); //open in browser to see upload form app.get('/', function (req, res) { res.sendFile(__dirname + '/index.html');//index.html is inside node-cheat }); //use by upload form app.post('/upload', upload.array('upl',1), function (req, res, next) { res.send("Uploaded!"); }); app.listen(3000, function () { console.log('Example app listening on port 3000!'); }); |
对于完整的回购:
克隆节点作弊express_multer_s3,先运行
快乐的帮助!
@ V31回答得很好,我仍然想加2美分。
我相信将一个责任放在一个文件中,以实现更好的代码组织和调试目的。
我已经创建了一个用于上传
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | require('dotenv').config(); const AWS = require('aws-sdk'); const multer = require('multer'); const multerS3 = require('multer-s3'); const s3Config = new AWS.S3({ accessKeyId: process.env.AWS_IAM_USER_KEY, secretAccessKey: process.env.AWS_IAM_USER_SECRET, Bucket: process.env.AWS_BUCKET_NAME }); const fileFilter = (req, file, cb) => { if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') { cb(null, true) } else { cb(null, false) } } // this is just to test locally if multer is working fine. const storage = multer.diskStorage({ destination: (req, res, cb) => { cb(null, 'src/api/media/profiles') }, filename: (req, file, cb) => { cb(null, new Date().toISOString() + '-' + file.originalname) } }) const multerS3Config = multerS3({ s3: s3Config, bucket: process.env.AWS_BUCKET_NAME, metadata: function (req, file, cb) { cb(null, { fieldName: file.fieldname }); }, key: function (req, file, cb) { console.log(file) cb(null, new Date().toISOString() + '-' + file.originalname) } }); const upload = multer({ storage: multerS3Config, fileFilter: fileFilter, limits: { fileSize: 1024 * 1024 * 5 // we are allowing only 5 MB files } }) exports.profileImage = upload; |
这是在我的路线
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | const express = require('express'); const ProfileController = require('../profile/controller'); const { profileImage } = require('../utils/upload.js'); const routes = (app) => { const apiRoutes = express.Router(); apiRoutes.use('/profile', profileRoutes); profileRoutes.post('/',profileImage.single('profileImage'), ProfileController.saveProfile); app.use('/api', apiRoutes); } module.exports = routes |
邮递员屏幕截图
1 2 3 4 5 6 7 8 9 10 11 12 | var upload = multer({ storage: multerS3({ s3: s3, bucket: 'some-bucket', metadata: function (req, file, cb) { cb(null, {fieldName: file.fieldname}); }, key: function (req, file, cb) { cb(null, Date.now().toString()) } }) }) |
MulterS3文件
我只想加分
在所有答案中都有很多评论,例如如何在上传后获取公共URL和S3响应对象,并让我们了解实现和案例,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // INITIALIZE NPMS var AWS = require('aws-sdk'), multer = require('multer'), multerS3 = require('multer-s3'), path = require('path'); // CONFIGURATION OF S3 AWS.config.update({ secretAccessKey: '***********************************', accessKeyId: '****************', region: 'us-east-1' }); // CREATE OBJECT FOR S3 const S3 = new AWS.S3(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | // CREATE MULTER FUNCTION FOR UPLOAD var upload = multer({ // CREATE MULTER-S3 FUNCTION FOR STORAGE storage: multerS3({ s3: S3, acl: 'public-read', // bucket - WE CAN PASS SUB FOLDER NAME ALSO LIKE 'bucket-name/sub-folder1' bucket: 'bucket-name', // META DATA FOR PUTTING FIELD NAME metadata: function (req, file, cb) { cb(null, { fieldName: file.fieldname }); }, // SET / MODIFY ORIGINAL FILE NAME key: function (req, file, cb) { cb(null, file.originalname); //set unique file name if you wise using Date.toISOString() // EXAMPLE 1 // cb(null, Date.now() + '-' + file.originalname); // EXAMPLE 2 // cb(null, new Date().toISOString() + '-' + file.originalname); } }), // SET DEFAULT FILE SIZE UPLOAD LIMIT limits: { fileSize: 1024 * 1024 * 50 }, // 50MB // FILTER OPTIONS LIKE VALIDATING FILE EXTENSION fileFilter: function(req, file, cb) { const filetypes = /jpeg|jpg|png/; const extname = filetypes.test(path.extname(file.originalname).toLowerCase()); const mimetype = filetypes.test(file.mimetype); if (mimetype && extname) { return cb(null, true); } else { cb("Error: Allow images only of extensions jpeg|jpg|png !"); } } }); |
有三种情况,如果我们要在上传后从S3检索文件
情况1:当我们使用
1 2 3 4 | app.post('/upload', upload.single('file'), function (req, res, next) { console.log('Uploaded!'); res.send(req.file); }); |
情况2:当我们使用
1 2 3 4 | app.post('/upload', upload.array('file', 1), function (req, res, next) { console.log('Uploaded!'); res.send(req.files); }); |
情况3:当我们使用
1 2 3 4 5 6 7 | app.post('/upload', upload.fields([ { name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 } ]), function (req, res, next) { console.log('Uploaded!'); res.send(req.files); }); |
/ ***使用Multer上传图片图片正在上传* /
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | const fileStorage = multer.diskStorage({ destination: function(req, file, cb) { cb(null,"./public/uploads"); }, filename: function(req, file, cb) { cb(null, file.originalname); } }); /** AWS catalog */ aws.config.update({ secretAccessKey: process.env.SECRET_KEY, accessKeyId: process.env.ACCESS_KEY, region:"us-east-1" }); const s3 = new aws.S3(); const awsStorage = multerS3({ s3: s3, bucket: process.env.BUCKET_NAME, key: function(req, file, cb) { console.log(file); cb(null, file.originalname); } }); const upload = multer({ storage: awsStorage(), /** in above line if you are using local storage in ./public/uploads folder than use ******* storage: fileStorage, * if you are using aws s3 bucket storage than use ******* storage: awsStorage(), */ limits: { fileSize: 5000000 }, fileFilter: function(req, file, cb) { checkFileType(file, cb); } }); app.post("/user-profile-image", upload.single("profile"), (req, res, err) => { try { res.send(req.file); } catch (err) { res.send(400); } }); const checkFileType = (file, cb) => { const filetypes = /jpeg|jpg|png|gif/; const extname = filetypes.test(path.extname(file.originalname).toLowerCase()); const mimetype = filetypes.test(file.mimetype); if (mimetype && extname) { return cb(null, true); } else { cb("Error: Images Only!"); } }; |
我正在将S3传递给mutler,例如
1 | S3: {object} |
将其更改为小号s3对我有用:-
1 | S3: {object} |