我正在尝试使用 aws-sdk 的服务 Textract.getDocumentTextDetection 对 pdf 文件进行文本提取,其中我正在传递由方法“startDocumentTextDetection”返回的 JobId。
首先,我尝试使用 s3 服务的方法“upload”上传图像/pdf,该方法成功运行并返回我这样的响应
{
ETag: '"9d022c9c496f2663d2fgv45d181e475"',
Location:
'https://bucket-name.s3.amazonaws.com/beacon-logo.png',
key: 'beacon-logo.png',
Key: 'beacon-logo.png',
Bucket: 'bucket-name'
}
在此之后,我调用 textract 的方法“startDocumentTextDetection”来获取 JobId,它返回给我这个:
{ JobId:
'11df404ce0fa7c958ba23579890a52388132a01d326802a1eggh76915c55qw1e' }
在此之后,我调用 AWS 服务的另一种方法“getDocumentTextDetection”从上传的文件中提取单词,该文件返回给我对象:
{ JobStatus: 'IN_PROGRESS' }
这是错误的。方法“getDocumentTextDetection”应返回如下内容: https ://docs.aws.amazon.com/textract/latest/dg/API_GetDocumentTextDetection.html
以下是到目前为止编写的所有代码:
const textract = new AWS.Textract({
apiVersion: "2018-06-27",
accessKeyId: "my-access-key-id",
secretAccessKey: "my-secret-access-key",
region: "region",
ACL: "public-read"
});
const s3 = new AWS.S3();
router.post("/", function (req, res, next) {
const paramsS3Upload = {
Bucket: "bucket-name",
Key: req.files.document.name,
ACL: "public-read",
ContentType: req.files.document.mimetype,
Body: req.files.document.data
};
var s3options = {
partSize: 10 * 1024 * 1024,
queueSize: 1
};
s3.upload(paramsS3Upload, s3options, function (s3Err, s3data) {
if (s3Err) throw s3Err;
var params = {
DocumentLocation: {
S3Object: {
Bucket: s3data.Bucket,
Name: s3data.Key,
Version: s3data.VersionId
}
},
NotificationChannel: {
RoleArn: "arn:aws:iam::id:role/role-name",
SNSTopicArn: "arn:aws:sns:region:id:topic-name"
}
};
textract.startDocumentTextDetection(params, function (err, data) {
if (err) console.log(err, err.stack);
else {
var textDetectionParams = {
JobId: data.JobId
};
textract.getDocumentTextDetection(textDetectionParams, function (
err,
textDetectData
) {
if (err) console.log(err, err.stack);
else {
console.log(textDetectData); // Not getting proper response here!!
res.send({
data: textDetectData
});
}
});
}
});
});
});
预期的输出应该是这样的: https ://docs.aws.amazon.com/textract/latest/dg/API_GetDocumentTextDetection.html 。无法弄清楚出了什么问题。