// snippet-sourcedescription:[DetectDocumentText.java demonstrates how to detect text in the input document that is retrieved from an Amazon S3 bucket.]
// snippet-service:[Amazon Textract]
// snippet-keyword:[Java]
// snippet-keyword:[Amazon Textract]
// snippet-keyword:[Code Sample]
// snippet-sourcetype:[full-example]
// snippet-sourcedate:[7-8-2020]
// snippet-sourceauthor:[scmacdon - AWS]

/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * This file is licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License. A copy of
 * the License is located at
 *
 * http://aws.amazon.com/apache2.0/
 *
 * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

package com.example.textract;

// snippet-start:[textract.java2._detect_s3_text.import]
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.textract.model.S3Object;
import software.amazon.awssdk.services.textract.TextractClient;
import software.amazon.awssdk.services.textract.model.Document;
import software.amazon.awssdk.services.textract.model.DetectDocumentTextRequest;
import software.amazon.awssdk.services.textract.model.DetectDocumentTextResponse;
import software.amazon.awssdk.services.textract.model.Block;
import software.amazon.awssdk.services.textract.model.DocumentMetadata;
import software.amazon.awssdk.services.textract.model.TextractException;
import java.util.Iterator;
import java.util.List;
// snippet-end:[textract.java2._detect_s3_text.import]

public class DetectDocumentTextS3 {

    public static void main(String[] args) {

        final String USAGE = "\n" +
                "Usage:\n" +
                "    DetectDocumentTextS3 <bucketName> <docName> \n\n" +
                "Where:\n" +
                "    bucketName - The name of the Amazon S3 bucket that contains the document \n\n" +
                "    docName - The document name (must be an image, i.e., book.png) \n";

        if (args.length < 2) {
            System.out.println(USAGE);
            System.exit(1);
        }

        String bucketName = args[0];
        String docName = args[1];

        Region region = Region.US_WEST_2;
        TextractClient textractClient = TextractClient.builder()
                .region(region)
                .build();

        detectDocTextS3(textractClient, bucketName, docName);

    }

    // snippet-start:[textract.java2._detect_s3_text.main]
    public static void detectDocTextS3 (TextractClient textractClient,  String bucketName, String docName) {

        try {
        S3Object s3Object = S3Object.builder()
                .bucket(bucketName)
                .name(docName)
                .build();

        // Create a Document object and reference the s3Object instance
        Document myDoc = Document.builder()
                .s3Object(s3Object)
                .build();

        // Create a DetectDocumentTextRequest object
        DetectDocumentTextRequest detectDocumentTextRequest = DetectDocumentTextRequest.builder()
                .document(myDoc)
                .build();

        // Invoke the detectDocumentText method
        DetectDocumentTextResponse textResponse = textractClient.detectDocumentText(detectDocumentTextRequest);

        List<Block> docInfo = textResponse.blocks();

        Iterator<Block> blockIterator = docInfo.iterator();

        while(blockIterator.hasNext()) {
            Block block = blockIterator.next();
            System.out.println("The block type is " +block.blockType().toString());
        }

        DocumentMetadata documentMetadata = textResponse.documentMetadata();
        System.out.println("The number of pages in the document is " +documentMetadata.pages());

    } catch (TextractException e) {

        System.err.println(e.getMessage());
        System.exit(1);
    }
        // snippet-end:[textract.java2._detect_s3_text.main]
  }
}

@@ -2718,17 +2718,16 @@
 tClient,
- 
  String 
@@ -2779,16 +2779,20 @@
         
+    
 S3Object
@@ -2838,16 +2838,20 @@
         
+  
+  
 .bucket(
@@ -2878,16 +2878,20 @@
         
+    
 .name(do
@@ -2905,32 +2905,36 @@
                 
+ 
+   
 .build();%0A%0A     
@@ -2920,32 +2920,36 @@
      .build();%0A%0A
+    
         // Creat
@@ -3004,32 +3004,36 @@
 nstance%0A        
+    
 Document myDoc =
@@ -3068,16 +3068,20 @@
         
+  
+  
 .s3Objec
@@ -3100,32 +3100,36 @@
                 
+    
 .build();%0A%0A     
@@ -3123,32 +3123,36 @@
 ild();%0A%0A        
+ 
+   
 // Create a Dete
@@ -3180,32 +3180,36 @@
  object%0A        
+   
+ 
 DetectDocumentTe
@@ -3298,16 +3298,20 @@
         
+    
 .documen
@@ -3327,32 +3327,36 @@
                 
+    
 .build();%0A%0A     
@@ -3342,32 +3342,36 @@
      .build();%0A%0A
+    
         // Invok
@@ -3402,32 +3402,36 @@
  method%0A        
+    
 DetectDocumentTe
@@ -3527,16 +3527,20 @@
         
+   
+ 
 List%3CBlo
@@ -3573,24 +3573,28 @@
 .blocks();%0A%0A
+    
         Iter
@@ -3650,16 +3650,20 @@
         
+ 
+   
 while(bl
@@ -3683,24 +3683,28 @@
 asNext()) %7B%0A
+    
             
@@ -3743,32 +3743,36 @@
 ();%0A            
+    
 System.out.print
@@ -3827,35 +3827,43 @@
 ing());%0A        
+  
+  
 %7D%0A%0A
+    
         Document
@@ -3919,24 +3919,28 @@
 Metadata();%0A
+    
         Syst
@@ -4029,16 +4029,20 @@
 );%0A%0A    
+   
+ 
 %7D catch 
@@ -4062,24 +4062,28 @@
 ption e) %7B%0A%0A
+    
         Syst
@@ -4114,32 +4114,36 @@
 age());%0A        
+  
+  
 System.exit(1);%0A
@@ -4138,24 +4138,28 @@
 em.exit(1);%0A
+    
     %7D%0A      
@@ -4213,14 +4213,16 @@
 t.main%5D%0A
+  
   %7D%0A%7D%0A

Update DetectDocumentTextS3.java