2

I want to use the textract API for document analysis, but when I tried to create an instance of AWS.Textract it throws an error saying

module initialization error: TypeError

I tried different things, Initially I tries this,

const AWS = require('aws-sdk');
const Textract = new AWS.Textract();

it didn't work and said cannot find object

then after some googling, I found this link and adjusted the code to this

require('aws-sdk/clients/textract');
var textractClient = new AWS.Textract();

and this,

const Textract = require('aws-sdk/clients/textract');
var textractClient = new Textract();

None of these are working, what am doing wrong here?

4

1 回答 1

4

不要生气,但您的标题有点令人困惑,因为它指的是 aws-cli 但是您描述中的代码片段是在 nodejs 中。你能澄清一下哪个是哪个吗?

在 JavaScript/nodejs SDK 中,您可以初始化 Amazon Textract 对象,如以下代码片段所示:

'use strict';

// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');

// Set region
AWS.config.region = 'us-east-1';

var textract = new AWS.Textract();

// Set parameters for the API
var params = {
    DocumentLocation: { /* required */
      S3Object: {
        Bucket: 'syumaK-bucket',
        Name: 'document.pdf'
      }
    },
    FeatureTypes: [ /* required */
      'TABLES','FORMS'
    ],
    NotificationChannel: {
      RoleArn: 'arn:aws:iam::19250632xxxx:role/AWSTextractRole', /* required */
      SNSTopicArn: 'arn:aws:sns:us-east-1:19250632xxxx:AmazonTextractTopic1562662993926'/* required */
    }
  };
  textract.startDocumentAnalysis(params, function(err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else     console.log(data);           // successful response
  });

我已经使用以下环境规范测试了上面的代码片段:

  • 操作系统:Mac High Sierra v10.13.6
  • aws-sdk:“^2.489.0”
于 2019-07-09T17:49:50.123 回答