Skip to content

Instantly share code, notes, and snippets.

@mmccall10
Last active January 31, 2018 17:24
Show Gist options
  • Save mmccall10/28fc4b01923270ccdcf36666c40bd5f5 to your computer and use it in GitHub Desktop.
Save mmccall10/28fc4b01923270ccdcf36666c40bd5f5 to your computer and use it in GitHub Desktop.
Upload file to s3 lambda (article embed)
const AWS = require('aws-sdk');
const fileType = require('file-type');
const config = new AWS.Config({
accessKeyId: process.env.ACCESS_KEY_ID,
secretAccessKey: process.env.SECRET_ACCESS_KEY,
region: process.env.REGION
});
AWS.config.update(config);
const s3 = new AWS.S3({apiVersion: '2006-03-01'});
const imageTypes = [
'image/gif',
'image/jpeg',
'image/png'
];
exports.handler = (event, context, callback) => {
//get the image data from upload
const body = JSON.parse(event.body);
const fileBuffer = new Buffer(body['image'], 'base64');
const fileTypeInfo = fileType(fileBuffer);
//validate image is on right type
if (fileBuffer.length < 500000 && imageTypes.includes(fileTypeInfo.mime)) {
// upload it to s3 with unix timestamp as a file name
const fileName = `${Math.floor(new Date() / 1000)}.${fileTypeInfo.ext}`;
const bucket = process.env.BUCKET;
const params = {
Body: fileBuffer,
Key: fileName,
Bucket: bucket,
ContentEncoding: 'base64',
ContentType: fileTypeInfo.mime
};
s3.putObject(params, (err, data) => {
if (err) callback(new Error([err.statusCode], [err.message]));
callback(null, {
statusCode: '200',
headers: {'Access-Control-Allow-Origin': '*'},
body: JSON.stringify({'data': data})
});
});
} else {
callback(null, {
statusCode: '402',
headers: {'Access-Control-Allow-Origin': '*'},
body: JSON.stringify({"message": "Not a valid file type or file too big."})
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment