feat: add s3 media controller

This commit is contained in:
Matthieu Bessat 2020-07-07 13:29:06 +02:00
parent f9c776b7a3
commit 31e4b854db

View file

@ -0,0 +1,49 @@
import Organization from '../models/Organization'
import * as express from 'express'
import aws from 'aws-sdk'
import multer from 'multer'
import multerS3 from 'multer-s3'
export default class MediaController {
static uploadRoute(): express.RequestHandler[] {
//
aws.config.update({
secretAccessKey: process.env.S3_SECRET_KEY,
accessKeyId: process.env.S3_ACCESS_KEY,
region: 'fr-par',
httpOptions: {
timeout: 4 * 60 * 1000,
}
})
const s3 = new aws.S3({
endpoint: 's3.fr-par.scw.cloud'
})
return [
multer({
storage: multerS3({
s3: s3,
bucket: 'development-bucket',
acl: 'public-read',
contentType: multerS3.AUTO_CONTENT_TYPE,
key: (req: any, file: any, cb: any) => {
console.log(file)
cb(null, Date.now().toString() + '_' + file.originalname) //use Date.now() for unique file keys
}
})
}).single('file'),
this.upload
]
}
static upload(req: express.Request, res: express.Response) {
res.json({
success: true,
data: { file: req.file }
})
}
}