AWS Textract InvalidParameterException

Nab*_*eel 5 amazon-web-services .net-core amazon-textract

我有一个 .Net core 客户端应用程序,根据 AWS 文档使用带有 S3、SNS 和 SQS 的 amazon Textract,检测和分析多页文档中的文本(https://docs.aws.amazon.com/textract/latest/dg/async .html )

使用 AmazonTextractServiceRole 策略创建 AWS 角色,并根据文档添加以下信任关系 ( https://docs.aws.amazon.com/textract/latest/dg/api-async-roles.html ) { "Version": "2012-10-17", "声明": [ { "效果": "允许", "主体": { "服务": "texttract.amazonaws.com" }, "操作": "sts:AssumeRole" } ] }

已订阅 SQS 主题并授予 Amazon SNS 主题按照 aws 文档将消息发送到 Amazon SQS 队列的权限。

所有资源包括S3 Bucket、SNS、SQS都位于同一个us-west2区域

以下方法显示一般错误“InvalidParameterException”请求具有无效参数

但如果NotificationChannel 部分被注释,则代码工作正常并返回正确的作业ID。

错误消息没有给出有关参数的清晰图片。高度赞赏任何帮助。

public async Task<string> ScanDocument()
{
            string roleArn = "aws:iam::xxxxxxxxxxxx:instance-profile/MyTextractRole";
            string topicArn = "aws:sns:us-west-2:xxxxxxxxxxxx:AmazonTextract-My-Topic";
            string bucketName = "mybucket";
            string filename = "mytestdoc.pdf";

            var request = new StartDocumentAnalysisRequest();
            var notificationChannel = new NotificationChannel();
            notificationChannel.RoleArn = roleArn;
            notificationChannel.SNSTopicArn = topicArn;

            var s3Object = new S3Object
            {
                Bucket = bucketName,
                Name = filename
            };
            request.DocumentLocation = new DocumentLocation
            {
                S3Object = s3Object
            };
            request.FeatureTypes = new List<string>() { "TABLES", "FORMS" };
            request.NotificationChannel = channel; /* Commenting this line work the code*/
            var response = await this._textractService.StartDocumentAnalysisAsync(request);
            return response.JobId;

        }
Run Code Online (Sandbox Code Playgroud)

Mas*_*cks 8

调试无效的 AWS 请求

AWS 开发工具包会在本地验证您的请求对象,然后再将其分派到 AWS 服务器。此验证将失败,并出现毫无帮助的不透明错误,例如 OP。

由于该SDK是开源的,您可以检查源代码以帮助缩小无效参数的范围。

在我们查看代码之前:SDK(和文档)实际上是从描述 API、其要求以及如何验证它们的特殊 JSON 文件生成的。实际的代码是根据这些 JSON 文件生成的。

我将使用 Node.js SDK 作为示例,但我确信类似的方法也适用于其他 SDK,包括 .NET

在我们的案例 (AWS Textract) 中,最新的 Api 版本是2018-06-27. 果然,JSON 源文件在 GitHub 上,这里。

就我而言,实验将问题范围缩小到了ClientRequestToken. 该错误是一个不透明的InvalidParameterException. 我在SDK源JSON文件中搜索了它,果然在第392行:

"ClientRequestToken": {
  "type": "string",
  "max": 64,
  "min": 1,
  "pattern": "^[a-zA-Z0-9-_]+$"
},
Run Code Online (Sandbox Code Playgroud)

一大堆未记录的要求!

就我而言,我使用的令牌违反了正则表达式(pattern在上面的源代码中)。更改我的令牌代码以满足正则表达式解决了问题。

我建议针对此类不透明类型错误使用这种方法。


Nab*_*eel 1

经过漫长的一天分析这个问题。我能够解决它..根据文档主题只需要 SendMessage Action 到 SQS 。但将其更改为 All SQS Action 后,它开始工作。但 AWS 错误消息仍然具有误导性和令人困惑