From 31e4b854dbe621a8ed2e53dbc2d7059efd08b836 Mon Sep 17 00:00:00 2001 From: Matthieu Bessat Date: Tue, 7 Jul 2020 13:29:06 +0200 Subject: [PATCH] feat: add s3 media controller --- src/controllers/MediaController.ts | 49 ++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/controllers/MediaController.ts diff --git a/src/controllers/MediaController.ts b/src/controllers/MediaController.ts new file mode 100644 index 0000000..f215cd6 --- /dev/null +++ b/src/controllers/MediaController.ts @@ -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 } + }) + } +} \ No newline at end of file