首页 > 解决方案 > AWS 文本无效参数异常

问题描述

我有一个 .Net 核心客户端应用程序,根据 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 ) {“版本”添加了以下信任关系: “2012-10-17”,“声明”:[{“效果”:“允许”,“主体”:{“服务”:“textract.amazonaws.com”},“行动”:“sts:AssumeRole”} ] }

根据 aws 文档,订阅 SQS 到该主题并授予 Amazon SNS 主题向 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;

        }

标签: amazon-web-services.net-coreamazon-textract

解决方案


调试无效的 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-_]+$"
},

一大堆未记录的要求!

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

对于这类不透明的类型错误,我推荐这种方法。


推荐阅读