我对 AWS 非常陌生,正在尝试编写一个简单的无服务器应用程序来检测 S3 存储桶中上传图像的文本。图片“2272922.jpg”已上传到S3存储桶中。下面的代码片段通过 Node 在本地工作并返回完美的响应
但从 Lambda 执行(或从 API 网关调用)时始终返回 null。
'use strict'
const AWS = require('aws-sdk')
AWS.config.update({ region: process.env.AWS_REGION })
var rekognition = new AWS.Rekognition();
console.log('Calling lmabda function detect with arguments');
// Lambda function entry point
exports.handler = async (event) => {
return await processImage(event)
}
//function which will extract the text from S3 bucket image
const processImage = async function (event) {
try {
var params = {
Image: {
S3Object: {
Bucket: "s3uploadbucket",
Name: "2272922.jpg"
}
},
};
rekognition.detectText(params, function (err, data) {
console.log(data);
if (err) console.log(err, err.stack); // an error occurred
else {
var table = "<table><tr><th>Low</th><th>High</th></tr>";
// show each text data detected
for (var i = 0; i < data.TextDetections.length; i++) {
table += '<tr><td>' + "Detected: " + data.TextDetections[i].DetectedText + '</td></tr>' +
'<tr><td>' + "Confidence: " + data.TextDetections[i].Confidence + '</td></tr>' +
'<tr><td>' + "Id : " + data.TextDetections[i].Id + '</td></tr>'
'<tr><td>' + "Parent Id: " + data.TextDetections[i].ParentId + '</td></tr>'
'<tr><td>' + "Type: " + data.TextDetections[i].Type + '</td></tr>'
}
table += "</table>";
console.log(table);
const response = {
statusCode: 200,
body: JSON.stringify(table)
}
return response;
}
});
} catch (error) {
console.log(error);
return error;
}
}
我已指定以下角色来执行此 lamda 函数。由于认为问题是由于 lambda 在某个地方错过了 Rekognition 相关策略(因为该函数在本地工作),因此通过控制台编辑了角色并尝试了所有与 Rekognition 相关的策略,但没有成功。
Policies:
- Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- s3:GetObject
Resource: arn:aws:s3:::*
- Effect: Allow
Action:
- rekognition:DetectText
- rekognition:DetectLabels
Resource: "*"
最佳答案
我在 rekognition.detectText(params)
和最后的 promise()
之前缺少“await”关键字。此外,该函数返回来自 detectorText 的返回文本,而不是我编写的预期表。我修改了如下代码,以表格格式给出了检测到的文本
'use strict'
const AWS = require('aws-sdk')
AWS.config.update({ region: process.env.AWS_REGION })
var rekognition = new AWS.Rekognition();
console.log('Calling lmabda function detect with arguments');
// Lambda function entry point
exports.handler = async (event) => {
return await processImage(event)
}
//function which will extract the text from S3 bucket image
const processImage = async function (event) {
try {
var params = {
Image: {
S3Object: {
Bucket: event.BucketName,
Name: event.Image
}
},
};
let data = await rekognition.detectText(params).promise();
var table = "<table><tr><th>Low</th><th>High</th></tr>";
// show each text data detected
for (var i = 0; i < data.TextDetections.length; i++) {
table += '<tr><td>' + "Detected: " + data.TextDetections[i].DetectedText + '</td></tr>' +
'<tr><td>' + "Confidence: " + data.TextDetections[i].Confidence + '</td></tr>' +
'<tr><td>' + "Id : " + data.TextDetections[i].Id + '</td></tr>'
'<tr><td>' + "Parent Id: " + data.TextDetections[i].ParentId + '</td></tr>'
'<tr><td>' + "Type: " + data.TextDetections[i].Type + '</td></tr>'
}
table += "</table>";
const response = {
statusCode: 200,
body: JSON.stringify(table)
}
return response;
} catch (error) {
console.log(error);
return error;
}
}
关于node.js - 具有 Rekognition 的 AWS Lambda 函数始终返回 null node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66841724/