From a93a893cae2a590eacdce4da9dc5a6b557fdce99 Mon Sep 17 00:00:00 2001 From: Dave Glover Date: Fri, 19 Apr 2024 20:23:05 +1000 Subject: [PATCH] Update UploadFile.cs stream rather than write the inbound image to storage --- src/api/NewPatient/UploadFile.cs | 31 +++++++++---------------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/src/api/NewPatient/UploadFile.cs b/src/api/NewPatient/UploadFile.cs index 5dff473..ece26ea 100644 --- a/src/api/NewPatient/UploadFile.cs +++ b/src/api/NewPatient/UploadFile.cs @@ -1,7 +1,5 @@ using Azure; using Azure.AI.FormRecognizer.DocumentAnalysis; -using Azure.Storage.Blobs; -using Azure.Storage.Sas; using Contoso.Healthcare.Api.Models; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -19,7 +17,6 @@ public static class UploadFile [FunctionName(nameof(UploadFile))] public static async Task Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "new-patient/upload-file")] HttpRequest req, - [Blob("unprocessed-patient-forms", Connection = "NEW_PATIENT_STORAGE")] BlobContainerClient containerClient, [CosmosDB( databaseName: "patientDb", containerName: "patientContainer", @@ -29,15 +26,20 @@ public static async Task Run( var formData = await req.ReadFormAsync(); var file = formData.Files["file"]; - var (patientId, filename) = await StoreFile(file, containerClient); - var outputs = await ExtractFormInfo(containerClient, filename); + if (file == null) + { + return new BadRequestObjectResult("File not found"); + } + + string patientId = Guid.NewGuid().ToString(); + var outputs = await ExtractFormInfo(file); await formResponse.AddAsync(new FormRecognizerResponse(FormRecognizerResponse.GetId(patientId), outputs)); return new OkObjectResult(new { PatientId = patientId }); } - private static async Task> ExtractFormInfo(BlobContainerClient containerClient, string filename) + private static async Task> ExtractFormInfo(IFormFile file) { string? endpoint = Environment.GetEnvironmentVariable("FORM_RECOGNIZER_ENDPOINT"); string? apiKey = Environment.GetEnvironmentVariable("FORM_RECOGNIZER_API_KEY"); @@ -51,10 +53,7 @@ public static async Task Run( var credential = new AzureKeyCredential(apiKey); var client = new DocumentAnalysisClient(new Uri(endpoint), credential); - var blobClient = containerClient.GetBlobClient(filename); - var uri = blobClient.GenerateSasUri(BlobSasPermissions.Read, DateTimeOffset.UtcNow.AddMinutes(5)); - - AnalyzeDocumentOperation operation = await client.AnalyzeDocumentFromUriAsync(WaitUntil.Completed, modelId, uri); + AnalyzeDocumentOperation operation = await client.AnalyzeDocumentAsync(WaitUntil.Completed, modelId, file.OpenReadStream()); AnalyzeResult result = operation.Value; var outputs = new Dictionary(); @@ -69,16 +68,4 @@ public static async Task Run( return outputs; } - - private static async Task<(string, string)> StoreFile(IFormFile file, BlobContainerClient containerClient) - { - _ = await containerClient.CreateIfNotExistsAsync(); - - var patientId = Guid.NewGuid().ToString(); - - var name = $"{patientId}_{file.FileName}"; - _ = await containerClient.UploadBlobAsync(name, file.OpenReadStream()); - - return (patientId, name); - } } \ No newline at end of file