[英]Are the AWS Comprehend synchronous calls (e.g. BatchDetectSentiment) logged in Cloudtrail?
[英]How to access AWS Comprehend using aws-sdk-comprehend gem?
我正在努力让我的 Rails 应用程序与 AWS Comprehend 服务交互以提取文本实体。 我正在使用aws-sdk-comprehend
gem。 我已经成功地让我的应用程序与 AWS Rekognition 服务一起使用aws-sdk-rekognition
gem 进行图像分析。
我似乎无法获得正确的 AWS Comprehend 身份验证,因为我的所有调用都会导致Aws::Comprehend::Errors::InvalidRequestException
。
我设置了以下所有 ENV 变量:
我的代码看起来像这样:
class MyApp::Aws::ComprehendService < MyApp::ServiceBase
def call
@credentials = Aws::Credentials.new(ENV['AWSAccessKeyId'], ENV['AWSSecretKey'])
@client = Aws::Comprehend::Client.new(region: "us-west-1", credentials: credentials)
@client.detect_entities({text: "this is a simply little blob of text", language_code: "en"})
end
end
这导致Aws::Comprehend::Errors::InvalidRequestException
。 所以我也试过:
class MyApp::Aws::ComprehendService < MyApp::ServiceBase
def call
# use ENV credential format I've seen in examples...
@credentials = Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY'])
@client = Aws::Comprehend::Client.new(region: "us-west-1", credentials: credentials)
@client.detect_entities({text: "this is a simply little blob of text", language_code: "en"})
end
end
我找到了一个没有使用@credential
方法的例子。 该示例声称“初始化方法将自行加载凭据环境变量”。 所以我尝试了这个:
class MyApp::Aws::ComprehendService < MyApp::ServiceBase
def call
# ignore setting the credentials
@client = Aws::Comprehend::Client.new(region: "us-west-1")
@client.detect_entities({text: "this is a simply little blob of text", language_code: "en"}).
end
end
这也导致Aws::Comprehend::Errors::InvalidRequestException
。
你能看到我做错了什么吗? 有没有人成功地使用这个 gem 与 Comprehend API 进行交互?
根据Aws::Comprehend::Client#detect_entities
的文档
如果系统在您的输入文档中检测到文档级错误,API 将返回
InvalidRequestException
错误响应。 有关此异常的详细信息,请参阅 Comprehend 开发人员指南中的半结构化文档中的错误。
所以看起来你的使用不一定是问题,而是输入文件本身。
然而,响应应包括根据开发人员指南的实际问题:
文档级错误
如果 ClassifyDocument 或 DetectEntities API 操作在输入文档中检测到文档级错误,API 将返回
InvalidRequestException
错误响应。在错误响应中, Reason字段包含值 INVALID_DOCUMENT。
详细信息字段包含以下值之一:
DOCUMENT_SIZE_EXCEEDED – 文档尺寸太大。 检查文件的大小并重新提交请求。
UNSUPPORTED_DOC_TYPE – 不支持文档类型。 检查文件类型并重新提交请求。
PAGE_LIMIT_EXCEEDED – 文档中的页面太多。 检查文件中的页数并重新提交请求。
TEXTRACT_ACCESS_DENIED_EXCEPTION – 拒绝访问 Amazon Textract。 确认您的账户有权使用 Amazon Textract DetectDocumentText 和 AnalyzeDocument API 操作并重新提交请求。
Aws Aws::Comprehend::Errors::InvalidRequestException
object 已记录在案,因此您似乎可以通过
def call
# use ENV credential format I've seen in examples...
@credentials = Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY'])
@client = Aws::Comprehend::Client.new(region: "us-west-1", credentials: credentials)
begin
@client.detect_entities({text: "this is a simply little blob of text", language_code: "en"})
rescue Aws::Comprehend::Errors::InvalidRequestException => e
# interrogate the error object here e.g.
puts {reason: e.reason, detail: e.detail}
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.