Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save damiancipolat/17d450b46f42ddbf3864f3ff159132f4 to your computer and use it in GitHub Desktop.
Save damiancipolat/17d450b46f42ddbf3864f3ff159132f4 to your computer and use it in GitHub Desktop.
Aws rekognition lambda - read image from encoded image using base64
const AWS = require('aws-sdk');
const rekognition = new AWS.Rekognition();
//Analyze image.
const analyzeImg = (image) => rekognition.detectFaces(image).promise();
//Decode image.
const decodeImage = (image64)=>{
return {
Image: {
Bytes: new Buffer(image64, 'base64')
},
Attributes: ['ALL']
};
}
const validatRequest = (body)=>{
const {
userId,
image
} = body;
if (!(userId&&image))
throw {code:404,error:'Bad request'};
return true;
}
exports.handler = async (event) => {
try {
const request = JSON.parse(event.body);
validatRequest(request);
const {
userId,
image
} = request;
const decodedImage = decodeImage(image);
const result = await analyzeImg(decodedImage);
console.info(result);
return {
statusCode:200,
body:JSON.stringify(result)
};
} catch(err){
console.error(err);
return {
statusCode:500,
error:JSON.stringify(err)
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment