Created
April 4, 2018 12:21
-
-
Save varna/864507c3d3194008c9015a01653df34c to your computer and use it in GitHub Desktop.
Multer with S3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import express from 'express' | |
import compression from 'compression' | |
import multer from 'multer' | |
import upload from './upload.mjs' | |
const uploadFile = upload.single('uploadedfile') | |
const app = express() | |
app.use(compression()) | |
app.use(express.static('./dist')) | |
app.post('/upload', (req, res) => { | |
uploadFile(req, res, err => { | |
if (err) { | |
return res.status(500).send(err) | |
} else { | |
const ending = req.file.key.replace(/\.[^/.]+$/, "") | |
return res.send(`https://gpsmeasure.com/map/${ending}`) | |
} | |
}) | |
}) | |
app.get('/*', (req, res) => | |
res.sendFile('index.html', { | |
root: process.cwd() + '/dist' | |
}) | |
) | |
app.listen(3333, '0.0.0.0', () => { | |
console.log(`Listening on https://0.0.0.0:3333`) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import multer from 'multer' | |
import multerS3 from 'multer-s3' | |
import generate from 'nanoid/generate' | |
import AWS from 'aws-sdk' | |
/** | |
* AWS Config | |
*/ | |
const s3 = new AWS.S3({ | |
accessKeyId: process.env.ACCESS_KEY_ID, | |
secretAccessKey: process.env.SECRET_ACCESS_KEY | |
}) | |
/** | |
* Key (path and filename) for object in bucket | |
*/ | |
const key = (req, file, cb) => { | |
const date = new Date().toISOString().replace('-', '/').split('T')[0].replace('-', '/') | |
const id = generate('1234567890qwertyuiopasdfghjklzxcvbnm', 32) | |
const path = `${date}/${id}.kml` | |
cb(null, path) | |
} | |
/** | |
* Multer Storage config | |
*/ | |
const storage = multerS3({ | |
s3, | |
bucket: 'kml.gpsmeasure.com', | |
key | |
}) | |
/** | |
* File limits | |
*/ | |
const limits = { | |
fieldNameSize: 100, | |
fieldSize: 1024, | |
fields: 0, | |
fileSize: 5 * 1024 * 1024, | |
files: 1, | |
parts: 100, | |
headerPairs: 2000 | |
} | |
/** | |
* The function should call `cb` with a boolean | |
* to indicate if the file should be accepted | |
* | |
* @param req | |
* @param file | |
* @param cb | |
*/ | |
const fileFilter = (req, file, cb) => { | |
// Check MIME | |
if (!(file.mimetype && file.mimetype === 'application/vnd.google-earth.kml+xml')) | |
return cb(new Error('File is not "application/vnd.google-earth.kml+xml" type.')) | |
// Everything is OK | |
cb(null, true) | |
// // To reject this file pass `false`, like so: | |
// cb(null, false) | |
} | |
const upload = multer({ | |
storage, | |
limits, | |
fileFilter | |
}) | |
export default upload |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment