Skip to content

Instantly share code, notes, and snippets.

@gunnar2k
Created September 26, 2015 13:32
Show Gist options
  • Save gunnar2k/a6fb42213fa112318c00 to your computer and use it in GitHub Desktop.
Save gunnar2k/a6fb42213fa112318c00 to your computer and use it in GitHub Desktop.
Node/Express upload POST file to AWS
express = require('express')
router = express.Router()
formidable = require('formidable')
s3 = require('s3')
s3client = s3.createClient
s3Options:
accessKeyId: process.env.AWS_KEY
secretAccessKey: process.env.AWS_SECRET
region: "eu-west-1"
router.post '/upload', (req, res) ->
form = new formidable.IncomingForm()
form.parse req, (err, fields, files) ->
file = files.file
if err || !file
res.send 400, 'Couldnt parse request or there was no file uploaded'
params =
localFile: file.path
s3Params:
Bucket : process.env.AWS_BUCKET_IMAGE_UPLOADS
Key : "imageUploads/" + require('rand-token').generate(12) + "-" + file.name
ContentType : file.type
ACL : "public-read"
s3client.uploadFile(params).on 'end', ->
res.json url:s3.getPublicUrlHttp(params.s3Params.Bucket, params.s3Params.Key)
.on 'error', (err) ->
console.log err
res.send 400, 'Error uploading image to cloud'
module.exports = router
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment