Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cassmtnr
Forked from wharley/aws-s3.js
Created January 26, 2018 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cassmtnr/d7f444116276ae96ed61e40a1a268366 to your computer and use it in GitHub Desktop.
Save cassmtnr/d7f444116276ae96ed61e40a1a268366 to your computer and use it in GitHub Desktop.
upload
'use strict'
const AWS = require('aws-sdk')
const env = require('../../.env')
const formidable = require('formidable')
const fs = require('fs')
const sendErrorsFromDB = (res, dbErros) => {
const errors = []
_.forIn(dbErros.errors, error => errors.push(error.message))
return res.status(400).json({ errors })
}
const upload = (req, res, next) => {
AWS.config.region = env.awsRegion
AWS.config.accessKeyId = env.awsSercretKeyId
AWS.config.secretAccessKey = env.awsSecretUser
const s3 = new AWS.S3()
const bucket = 'nameBucketofamazons3'
const form = new formidable.IncomingForm()
form.parse(req, (err, fields, files) => {
const bodystream = fs.createReadStream(files.files.path)
const s3Params = {
Bucket: bucket,
Key: files.files.name,
Body: bodystream,
ContentType: files.files.type,
ACL: 'public-read'
}
s3.upload(s3Params, (err, data) => {
if(err) return sendErrorsFromDB(res, err)
const returnData = {
signedRequest: data,
url: data.Location
}
res.json(returnData)
})
})
}
module.exports = { upload }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment