Skip to content

Instantly share code, notes, and snippets.

@marteinn
Last active April 20, 2019 12:36
Show Gist options
  • Save marteinn/c1071b5e22086ace14cca38ec4460fb8 to your computer and use it in GitHub Desktop.
Save marteinn/c1071b5e22086ace14cca38ec4460fb8 to your computer and use it in GitHub Desktop.
How to pass a file to AWS Textract using serverless.js, where the file is in base64 format.
/*
This is a example serverless.js handler where we pass the
document we want to analyze as a base64 string (in a json payload)
and returns the result from textract.
Request:
{
"document": "/9j/4AAQS......."
}
Response:
{
"analyzeData": {
"DocumentMetadata": {
"Pages": 1
},
...
}
*/
const AWS = require("aws-sdk");
AWS.config.update({
accessKeyId: "XXXXX",
secretAccessKey: "XXXXX",
region: "eu-west-1",
});
const getImageText = async (event, context) => {
const { document } = JSON.parse(event.body);
const params = {
Document: {
Bytes: new Buffer(document, 'base64'),
},
FeatureTypes: [
"TABLES",
]
};
let analyzeData = null;
try {
const textract = new AWS.Textract();
analyzeData = await textract.analyzeDocument(document);
} catch (err) {
return {
statusCode: err.statusCode,
body: JSON.stringify({
message: "Error passing data to analyze",
code: err.code,
})
};
}
return {
statusCode: 200,
body: JSON.stringify({
analyzeData,
})
}
};
module.exports = {
getImageText,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment