Beispiel #1
0
        public async Task <GetDocumentAnalysisResponse> GetJobResults(string jobId)
        {
            var response = await _textract.GetDocumentAnalysisAsync(new GetDocumentAnalysisRequest
            {
                JobId = jobId
            });

            //response.Wait();
            return(response);
        }
 private Amazon.Textract.Model.GetDocumentAnalysisResponse CallAWSServiceOperation(IAmazonTextract client, Amazon.Textract.Model.GetDocumentAnalysisRequest request)
 {
     Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "Amazon Textract", "GetDocumentAnalysis");
     try
     {
         #if DESKTOP
         return(client.GetDocumentAnalysis(request));
         #elif CORECLR
         return(client.GetDocumentAnalysisAsync(request).GetAwaiter().GetResult());
         #else
                 #error "Unknown build edition"
         #endif
     }
     catch (AmazonServiceException exc)
     {
         var webException = exc.InnerException as System.Net.WebException;
         if (webException != null)
         {
             throw new Exception(Utils.Common.FormatNameResolutionFailureMessage(client.Config, webException.Message), webException);
         }
         throw;
     }
 }
Beispiel #3
0
        public override async Task ProcessMessageAsync(TextractNotification queueEvent)
        {
            LogInfo($"notificationMessage {JsonConvert.SerializeObject(queueEvent)}");
            if (queueEvent.Status == JobStatus.SUCCEEDED)
            {
                try {
                    var    jobId            = queueEvent.JobId;
                    string nextToken        = null;
                    var    counter          = 0;
                    var    manifestContents = new List <string>();
                    do
                    {
                        // get the result from the job
                        var getRequest = new GetDocumentAnalysisRequest {
                            JobId     = jobId,
                            NextToken = nextToken
                        };
                        LogInfo(JsonConvert.SerializeObject(getRequest));
                        var response = await _textractClient.GetDocumentAnalysisAsync(getRequest);

                        // LogInfo(JsonConvert.SerializeObject(response));

                        // save response to s3
                        var filePath = $"results/{jobId}/results_{counter}.json";
                        using (var memoryStream = new MemoryStream()) {
                            using (var streamWriter = new StreamWriter(memoryStream)) {
                                streamWriter.Write(JsonConvert.SerializeObject(response));
                                streamWriter.Flush();
                                var s3Response = await _s3Client.PutObjectAsync(new PutObjectRequest {
                                    BucketName  = _bucketName,
                                    Key         = filePath,
                                    InputStream = memoryStream
                                });

                                if (s3Response == null || s3Response.HttpStatusCode != (HttpStatusCode)200)
                                {
                                    throw new Exception("Unable to save results file to s3");
                                }
                            }
                        }

                        // add to the manifest
                        manifestContents.Add(filePath);

                        // get the next response token
                        nextToken = response.NextToken;
                        counter++;
                    } while (nextToken != null);

                    var manifestContentsString = string.Join(';', manifestContents);
                    LogInfo(manifestContentsString);

                    // write manifest
                    using (var memoryStream1 = new MemoryStream()) {
                        using (var streamWriter1 = new StreamWriter(memoryStream1)) {
                            streamWriter1.WriteLine(manifestContentsString);
                            streamWriter1.Flush();
                            var s3ManifestResponse = await _s3Client.PutObjectAsync(new PutObjectRequest {
                                BucketName  = _bucketName,
                                Key         = $"results/{jobId}/manifest",
                                InputStream = memoryStream1
                            });

                            if (s3ManifestResponse == null || s3ManifestResponse.HttpStatusCode != (HttpStatusCode)200)
                            {
                                throw new Exception("Unable to save manifest file to s3");
                            }
                        }
                    }
                    Thread.Sleep(100);
                }
                catch (Exception e) {
                    LogError(e);
                }
            }
        }