【问题标题】:How to read from an Amazon S3 Bucket and call AWS services如何从 Amazon S3 存储桶中读取数据并调用 AWS 服务
【发布时间】:2021-10-16 08:17:37
【问题描述】:

我可以调用 AWS Textract 从我的本地路径中读取图像。如何集成此文本代码以使用 S3 存储桶代码读取上传到创建的 S3 存储桶的图像。

用于从本地路径提取图像的工作文本提取代码

package aws.cloud.work;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.io.InputStream;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.textract.AmazonTextract;
import com.amazonaws.services.textract.AmazonTextractClientBuilder;
import com.amazonaws.services.textract.model.DetectDocumentTextRequest;
import com.amazonaws.services.textract.model.DetectDocumentTextResult;
import com.amazonaws.services.textract.model.Document;
import com.amazonaws.util.IOUtils;

public class TextractDemo {

    static AmazonTextractClientBuilder clientBuilder = AmazonTextractClientBuilder.standard()
            .withRegion(Regions.US_EAST_1);

    private static FileWriter file;

    public static void main(String[] args) throws IOException {

//AWS Credentials to access AWS Textract services

        clientBuilder.setCredentials(new AWSStaticCredentialsProvider(
                new BasicAWSCredentials("Access Key", "Secret key")));

//Set the path of the image to be textract. Can be configured to use from S3

      String document="C:\\Users\\image-local-path\\sampleTT.jpg";
      ByteBuffer imageBytes;

//Code to use AWS Textract services

        try (InputStream inputStream = new FileInputStream(new File(document))) {
            imageBytes = ByteBuffer.wrap(IOUtils.toByteArray(inputStream));
        }
        AmazonTextract client = clientBuilder.build();
        DetectDocumentTextRequest request = new DetectDocumentTextRequest()
                .withDocument(new Document().withBytes(imageBytes));

        /*
         * DetectDocumentTextResult result = client.detectDocumentText(request);
         * System.out.println(result); result.getBlocks().forEach(block ->{
         * if(block.getBlockType().equals("LINE")) System.out.println("text is "+
         * block.getText() + " confidence is "+ block.getConfidence());
         */ 

//      
        DetectDocumentTextResult result = client.detectDocumentText(request);
        System.out.println(result);
        JSONObject obj = new JSONObject();
        result.getBlocks().forEach(block -> {
            if (block.getBlockType().equals("LINE"))
                System.out.println("text is " + block.getText() + " confidence is " + block.getConfidence());
            JSONArray fields = new JSONArray();

            fields.add(block.getText() + " , " + block.getConfidence());
            obj.put(block.getText(), fields);

        });

//To import the results into JSON file and output the console output as sample.txt      
        try {
            file = new FileWriter("/Users/output-path/sample.txt");
            file.write(obj.toJSONString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                file.flush();
                file.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}

This is an example of the console out where the "text" and corresponding "confidence scores" are returned

我设法从文档中找到的 S3 存储桶代码集成:

        String document = "sampleTT.jpg";
        String bucket = "textract-images";

        AmazonS3 s3client = AmazonS3ClientBuilder.standard()
                .withEndpointConfiguration( 
                        new EndpointConfiguration("https://s3.amazonaws.com","us-east-1"))
                .build();
        
               
        // Get the document from S3
        com.amazonaws.services.s3.model.S3Object s3object = s3client.getObject(bucket, document);
        S3ObjectInputStream inputStream = s3object.getObjectContent();
        BufferedImage image = ImageIO.read(inputStream);

(已编辑)- 感谢 @smac2020,我目前有一个有效的 Rekognition 代码,它从我的 AWS 控制台 S3 存储桶中读取并运行我引用的 Rekognition 服务。但是,我无法修改和合并它 文本源代码

package com.amazonaws.samples;

import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.rekognition.AmazonRekognition;
import com.amazonaws.services.rekognition.AmazonRekognitionClientBuilder;
import com.amazonaws.services.rekognition.model.AmazonRekognitionException;
import com.amazonaws.services.rekognition.model.DetectLabelsRequest;
import com.amazonaws.services.rekognition.model.DetectLabelsResult;
import com.amazonaws.services.rekognition.model.Image;
import com.amazonaws.services.rekognition.model.Label;
import com.amazonaws.services.rekognition.model.S3Object;
import java.util.List;

public class DetectLabels {

 public static void main(String[] args) throws Exception {

    String photo = "sampleTT.jpg";
    String bucket = "Textract-bucket";

    
    
//    AmazonRekognition rekognitionClient = AmazonRekognitionClientBuilder.standard().withRegion("ap-southeast-1").build();

    AWSCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider (new BasicAWSCredentials("Access Key", "Secret Key"));
    AmazonRekognition rekognitionClient = AmazonRekognitionClientBuilder.standard().withCredentials(credentialsProvider).withRegion("ap-southeast-1").build();

    
    DetectLabelsRequest request = new DetectLabelsRequest()
         .withImage(new Image()
         .withS3Object(new S3Object()
         .withName(photo).withBucket(bucket)))
         .withMaxLabels(10)
         .withMinConfidence(75F);

    try {
       DetectLabelsResult result = rekognitionClient.detectLabels(request);
       List <Label> labels = result.getLabels();

       System.out.println("Detected labels for " + photo);
       for (Label label: labels) {
          System.out.println(label.getName() + ": " + label.getConfidence().toString());
       }
    } catch(AmazonRekognitionException e) {
       e.printStackTrace();
    }
 }
}

【问题讨论】:

  • 您没有参考最新的 Jave 代码示例以使用适用于 Java 的 AWS 开发工具包。在此处查看示例:github.com/awsdocs/aws-doc-sdk-examples/tree/master/javav2
  • 感谢 @smac2020,我目前有一个可以工作的 Rekognition 代码,它从我的 AWS 控制台 S3 存储桶中读取并运行我引用的 Rekognition 服务。但是,我无法修改它并将其与 Textract 源代码合并。有关我添加的 Rekognition Code,请参阅 (已编辑) 部分。

标签: java amazon-web-services spring-boot amazon-s3 amazon-textract


【解决方案1】:

看起来您正试图从 Spring Boot 应用程序中读取 Amazon S3 对象,然后将该字节数组传递给 DetectDocumentTextRequest

有一个教程展示了一个非常相似的用例,其中 Spring BOOT 应用程序从 Amazon S3 对象读取字节并将其传递给 Amazon Rekognition 服务(而不是 Textract)。

Java 代码是:

// Get the byte[] from this AWS S3 object.
public byte[] getObjectBytes (String bucketName, String keyName) {

    s3 = getClient();

    try {
        GetObjectRequest objectRequest = GetObjectRequest
                .builder()
                .key(keyName)
                .bucket(bucketName)
                .build();
        
        ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
        byte[] data = objectBytes.asByteArray();
        return data;

    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return null;
}

请参阅这篇 AWS 开发文章,了解如何构建具有此功能的 Spring BOOT 应用程序。

Creating an example AWS photo analyzer application using the AWS SDK for Java

此示例使用 AWS SDK For Java V2。如果您不熟悉使用最新的 SDK 版本,我建议您从这里开始:

Get started with the AWS SDK for Java 2.x

【讨论】:

  • 感谢@smac2020 的参考和帮助。我试图将我的源代码保存在一个 java 类中,比如我刚刚在我的主帖下面添加的 Rekognition 代码。我正在尝试将这段代码集成到: AmazonS3 s3client = AmazonS3ClientBuilder.standard().withEndpointConfiguration(new EndpointConfiguration("s3.amazonaws.com","us-east-1")).build(); // 从 S3 com.amazonaws.services.s3.model 获取文档。 S3Object s3object = s3client.getObject(bucket, document); S3ObjectInputStream inputStream = s3object.getObjectContent();
猜你喜欢
  • 2022-01-13
  • 2018-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多