vue express uploading multiple files to amazon s3

吃可爱长大的小学妹 提交于 2019-12-06 07:35:32

The solution I found for using aws-sdk. The good that I will also use this solution and my projects.

Credits: StackOver - Answer Reference: Multer-s3

In your helper file you'll leave it like this:

const aws = require('aws-sdk')
const multer = require('multer')
const multerS3 = require('multer-s3')

aws.config = new aws.Config({
    accessKeyId: <my access id>,
    secretAccessKey: <my secret key>,
    region: <my region>
})
const s3 = new aws.S3()
const upload = multer({
    storage: multerS3({
    s3: s3,
    acl: 'public-read',
    bucket: 'vue-express-upload',
    contentType: function(req, file,cb){
        cb(null, file.mimetype)
    },
    key: function (req, file, cb) {
        cb(null, file.originalname)
    }
    })
})

module.exports = upload

The key of the file you can also change to something more unique, like the date / minutes / seconds. It is at your discretion, I used the filename as key.

In your router the route will look like this:

const upload = require('../helper/sendingImage')

router.post('/bulkupload', upload.array('files'), (req, res) => {
    console.log('success')
    //console.log(req.files)
    res.send('Uploadedd')
    //res.send(req.files)    
})

To view upload data:

router.post('/bulkupload', upload.array('files'), (req, res) => {
    res.send({
        status: 200,
        data: {
            sucess: true,
            file: req.files
        }
    })   
})

If there is no middleware file, it simply will not send. Will not return error. The only errors that can return are those of AWS access information.

Note:I used the postman.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!