1

I'm an AWS newbie trying to use Textract API, their OCR service. As far as I understood I need to upload files to a S3 bucket and then run textract on it.

I got the bucket on and the file inside it: enter image description here

I got the permissions: enter image description here

But when I run my code it bugs.

        import boto3
        import trp

        # Document
        s3BucketName = "textract-console-us-east-1-057eddde-3f44-45c5-9208-fec27f9f6420"
        documentName = "ok0001_prioridade01_x45f3.pdf"
]\[\[""
        # Amazon Textract client
        textract = boto3.client('textract',region_name="us-east-1",aws_access_key_id="xxxxxx",
                                aws_secret_access_key="xxxxxxxxx")

        # Call Amazon Textract
        response = textract.analyze_document(
            Document={
                'S3Object': {
                    'Bucket': s3BucketName,
                    'Name': documentName
                }
            },
            FeatureTypes=["TABLES"])

Here is the error I get:

botocore.errorfactory.InvalidS3ObjectException: An error occurred (InvalidS3ObjectException) when calling the AnalyzeDocument operation: Unable to get object metadata from S3. Check object key, region and/or access permissions.

What am I missing? How could I solve that?

2 Answers 2

2

You are missing S3 access policy, you should add AmazonS3ReadOnlyAccess policy if you want a quick solution according to your needs.

A good practice is to apply the least privilege access principle and keep granting access when needed. So I'd advice you to create a specific policy to access your S3 bucket textract-console-us-east-1-057eddde-3f44-45c5-9208-fec27f9f6420 only and only in us-east-1 region.

1

Amazon Textract currently supports PNG, JPEG, and PDF formats. Looks like you are using PDF.

Once you have a valid format, you can use the Python S3 API to read the data of the object in the S3 object. Once you read the object, you can pass the byte array to the analyze_document method. TO see a full example of how to use the AWS SDK for Python (Boto3) with Amazon Textract to detect text, form, and table elements in document images.

https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/python/example_code/textract/textract_wrapper.py

Try following that code example to see if your issue is resolved.

"Could you provide some clearance on the params to use"

I just ran the Java V2 example and it works perfecly. In this example, i am using a PNG file located in a specific Amazon S3 bucket.

Here are the parameters that you need:

enter image description here

Make sure when implementing this in Python, you set the same parameters.

2
  • Could you provide some clearance on the params to use? TypeError: __init__() missing 3 required positional arguments: 'textract_client', 's3_resource', and 'sqs_resource'
    – aabujamra
    Commented Apr 12, 2021 at 13:30
  • See the above pic that shows a successful call and the params you need
    – smac2020
    Commented Apr 12, 2021 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.