-
-
Save cassmtnr/d7f444116276ae96ed61e40a1a268366 to your computer and use it in GitHub Desktop.
upload
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
'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