【发布时间】:2019-09-03 08:49:27
【问题描述】:
从 Azure 函数(Blob 触发器)中调用 Azure Forms Recognizer .NET SDK 时出现错误(BadRequest)
我基本上使用文档中 C# 快速入门中的示例代码。我已经训练了模型,并且可以成功地从 .NET Core 命令行应用程序调用表单识别器,并且还可以使用 cURL。我现在想触发相同的流程,但是当 PDF 上传到 Azure 存储 Blob 时。在函数中使用相同的 modelId、订阅和端点调用 AnalyzeWithCustomModelAsync() 时出现 BadRequest 错误。我在 myBlob Stream 上尝试了许多变体,创建了 FileStream、MemoryStream,使用传递给 Function 的 uri 创建了新的 Stream 等等。
[FunctionName("ImportInvoice")]
public static async System.Threading.Tasks.Task RunAsync([BlobTrigger("originals/{name}", Connection = "invoiceConn")]Stream myBlob, string name, Uri uri, string blobTrigger, ILogger log)
{
log.LogInformation($"ImportInvoice Function triggered by new invoice: {name} size: {myBlob.Length} Bytes");
log.LogInformation($"Invoice Uri: {uri.AbsoluteUri}");
log.LogInformation($"System.IO.Stream.CanRead is: {myBlob.CanRead}");
try
{
IFormRecognizerClient formClient = new FormRecognizerClient(new ApiKeyServiceClientCredentials(subscriptionKey))
{
Endpoint = formRecognizerEndpoint
};
using (Stream invoiceStream = myBlob)
{
log.LogInformation($"About to analyse with custom model {modelId}");
// Fails here with BadRequest
AnalyzeResult result = await formClient.AnalyzeWithCustomModelAsync(modelId, myBlob, contentType: "application/pdf");
log.LogInformation("Invoice analysed");
foreach (var page in result.Pages)
{
foreach (var kv in page.KeyValuePairs)
{
if (kv.Key.Count > 0 && kv.Value.Count > 0)
log.LogInformation(kv.Key[0].Text + ": " + kv.Value[0].Text);
}
}
}
}
catch (ErrorResponseException e)
{
log.LogInformation("Analyze PDF form : " + e.Message);
}
catch (Exception ex)
{
log.LogInformation("Analyze PDF form : " + ex.Message);
}
}
我传入了 modelId、传递给函数 (myBlob) 的 Stream 和正确的内容类型 (application/pdf)。我希望 formClient.AnalyzeWithCustomModelAsync(modelId, myBlob, contentType: "application/pdf") 调用成功并查看键和值(相同的代码在函数之外工作)但我得到以下信息:
Executing 'ImportInvoice' (Reason='New blob detected: originals/NewCareInvoice5.pdf', Id=63f26c79-f269-4ea8-b6e8-8b4f6d6eb6e2)
ImportInvoice Function triggered by new invoice: NewCareInvoice5.pdf size: 134507 Bytes
Invoice Uri: https://myinvoices.blob.core.windows.net/originals/NewCareInvoice5.pdf
System.IO.Stream.CanRead is: True
About to analyse with custom model <correct model id shown here>
Analyze PDF form : Operation returned an invalid status code 'BadRequest'
我假设问题是流,但我绕着圈子试图引用它或创建或复制新流,但似乎遗漏了一些东西。我无法获得有关错误的更多详细信息(例如来自 InnerException)。
非常感谢任何帮助!
【问题讨论】: