Open AmoDinho opened 5 years ago
const AWS = require('aws-sdk')
AWS.config.update({region: 'us-east-1'})
var rekognition = new AWS.Rekognition()
const fs = require('fs');
exports.handler = (event, context, callback) => {
const toDataURL = url => fetch(url)
.then(response => response.blob())
.then(blob => new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onloadend = () => resolve(reader.result)
reader.onerror = reject
reader.readAsDataURL(blob)
}))
const image = toDataURL(event.imageInfo)
.then(dataURL => {
dataURL;
})
var params = {
Image: {
Bytes: image
/* S3Object: {
Bucket: "pmb-traimages",
Name: "/" + event.imageInfo,
Version: "Latest Version"
}*/
}
};
rekognition.detectText(params, function(err, data){
if(err){
callback(null, {
data: JSON.stringify(err.stack)
})
} else {
const myData = JSON.stringify(data)
callback(null, {
data: myData
})
}
})
};
/*
How would you use axios with/or node fetch library
and convert oall of the code to es6?
//A Func that reads the url of the s3 bucket
async readS3() => {
return new Promise((resolve, reject) => {
const Options = {
path: event.info,
method: 'PUT'
};
const req = http.request(options, (res) => {
resolve('Success')
});
req.on('error', (e) => {
reject(e.message)
});
req.write('');
req.end();
});
};
//create a variable that holds the blob of the image
const imageBlob = readS3()
.then(response => response.blob())
.then(blob => new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onloadend = () => resolve(reader.result)
reader.onerror = reject
reader.readAsDataURL(blob)
}))
//create the rekognition blob
var params = {
Image: {
Bytes: imageBlob
}
};
//call rekognition and return it
rekognition.detectText(params, (data, err) => {
try {
const rekData = JSON.stringyfy(data)
return rekData
} catch (err){
return err.message
}
})
*/
"\"InvalidS3ObjectException: Unable to get object metadata from S3. Check object key, region and/or access permissions.\n