Skip to content

Instantly share code, notes, and snippets.

@cassmtnr
Last active August 20, 2019 03:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cassmtnr/972ef402fae6b0c1a7e319c7088a33d6 to your computer and use it in GitHub Desktop.
Save cassmtnr/972ef402fae6b0c1a7e319c7088a33d6 to your computer and use it in GitHub Desktop.
upload-image-to-s3
'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