Last active
August 20, 2019 03:32
-
-
Save cassmtnr/972ef402fae6b0c1a7e319c7088a33d6 to your computer and use it in GitHub Desktop.
upload-image-to-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
'use strict'; | |
let config = { | |
AWS = { | |
accessKeyId: ACCESSKEYHERE, | |
secretAccessKey: SECRETEACCESSKEYHERE, | |
region: REGIONHERE | |
} | |
} | |
const AWS = require('aws-sdk'); | |
const nanoid = require('nanoid'); | |
AWS.config.update(config.AWS); | |
const s3 = new AWS.S3(); | |
let bucketName = 'node-str-img-bucket'; | |
let fileName = nanoid().toString(); | |
let rawdata = req.body.image; | |
let matches = rawdata.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/); | |
let fileType = matches[1]; | |
let buffer = new Buffer(matches[2], 'base64'); | |
let s3Params = { | |
Bucket: bucketName, | |
Key: fileName, | |
Body: buffer, | |
ContentEncoding: 'base64', | |
ContentType: fileType, | |
ACL: 'public-read' | |
}; | |
await s3.putObject(s3Params, (error, data) => { | |
if (error) { | |
console.log('Erro: '); | |
console.log(error); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment