Created
January 9, 2019 05:42
-
-
Save nathanparker/b97cf603ecee4de94b80683e1f76e77c to your computer and use it in GitHub Desktop.
This file contains hidden or 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'; | |
| var _ = require('underscore'); | |
| var config = require('../../config/environment'); | |
| var AWS = require('aws-sdk'); | |
| var multiparty = require('multiparty'); | |
| var uuidv4 = require('uuid/v4'); | |
| AWS.config.update({ | |
| accessKeyId: config.aws.clientID, | |
| secretAccessKey: config.aws.clientSecret, | |
| region: config.aws.clientRegion | |
| }); | |
| var s3 = new AWS.S3({params: {Bucket: 'product-builder'}}); | |
| exports.upload = function(req, res) { | |
| var form = new multiparty.Form(); | |
| form.parse(req, function(err, fields, files) { | |
| console.log(fields); | |
| var file = files.file[0], | |
| contentType = file.headers['content-type'], | |
| extension = file.path.substring(file.path.lastIndexOf('.')), | |
| key = fields.path[0] + fields.id[0] + '/' + uuidv4() + extension, | |
| // key = 'settings/' + uuidv4() + extension, | |
| buf = new Buffer(fields.base64[0].replace(/^data:image\/\w+;base64,/, ''),'base64'); | |
| var data = { | |
| Key: key, | |
| Body: buf, | |
| ContentEncoding: 'base64', | |
| ContentType: contentType != '' ? contentType : 'application/octet-stream', | |
| ACL: 'public-read' | |
| }; | |
| s3.putObject(data, function(err, data) { | |
| if (err) return res.status(500).send(err); | |
| var obj = {data, key}; | |
| return res.status(200).send(obj); | |
| }); | |
| }); | |
| }; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@randasimpson