In this post, let's see how we can expose an Azure Document Intelligence (DI) Service through Azure Front Door (AFD) and consume it via a .NET Client Application that uses Azure.AI.DocumentIntelligence package.
Say, we have a DI service,
https://{document-intelligence-service-name}.cognitiveservices.azure.com/
And we need this service to be consumed via,
https://{front-door-name}.azurefd.net/di-api/
For an example,
POST https://{document-intelligence-service-name}.cognitiveservices.azure.com/documentintelligence/documentClassifiers/{modelId}:analyze
POST https://{front-door-name}/di-api/documentintelligence/documentClassifiers/{modelId}:analyze
To start off, following are already created.
- An Azure Document Intelligence Service
- Azure Front Door and CDN profiles (Azure Front Door Standard with Quick Create)
|
Add Origin Group |
|
Add Origin |
1. Using default-route, with a Rule set to Override origin group
2. Creating a new route with Pattens to match and Origin path
using Azure;
using Azure.AI.DocumentIntelligence;
string endpoint = "https://{front-door-name}.azurefd.net/di-api/";
string apiKey = "{document-intelligence-service-api-key}";
DocumentIntelligenceClient documentIntelligenceClient = new (new Uri(endpoint), new AzureKeyCredential(apiKey));
string classifierId = "{some-classification-model-id}";
string testFilePath = "path\to\test\file.pdf";
using FileStream fileStream = new FileStream(testFilePath, FileMode.Open, FileAccess.Read);
BinaryData binaryData = BinaryData.FromStream(fileStream);
ClassifyDocumentOptions classifyDocumentOptions = new(classifierId, binaryData);
Operation<AnalyzeResult> operation =
await documentIntelligenceClient.ClassifyDocumentAsync(WaitUntil.Completed, classifyDocumentOptions);
AnalyzeResult result = operation.Value;
foreach (AnalyzedDocument document in result.Documents)
{
Console.WriteLine($"Found a document of type: '{document.DocumentType}'");
}
Happy Coding.