SDKs overview
AWS Textract provides a managed machine learning service that automates text extraction, handwriting recognition, and data extraction from documents. To interact with Textract programmatically, developers typically use the AWS Software Development Kits (SDKs). These SDKs abstract the underlying RESTful API calls, handling authentication, request signing, and error handling, allowing developers to focus on application logic rather than low-level networking details. The SDKs support various programming languages, offering a consistent interface across different development environments.
Using an SDK for AWS Textract simplifies tasks such as sending documents for analysis, retrieving results, and managing asynchronous operations. For instance, the SDKs enable developers to call the AnalyzeDocument API to extract structured data from forms and tables, or the DetectDocumentText API for general text extraction. The official AWS SDKs are maintained by Amazon and are the recommended method for integrating Textract into applications due to their comprehensive feature support and alignment with AWS service updates.
Official SDKs by language
AWS provides official SDKs for a range of popular programming languages, ensuring broad compatibility for developers building applications that utilize Textract. These SDKs are designed to integrate seamlessly with other AWS services and follow standard practices for each language ecosystem. The table below outlines the primary official SDKs relevant to AWS Textract, including their typical package names and installation methods. Each SDK offers methods corresponding to the Textract API operations, such as analyze_document or detect_document_text.
| Language | Package/Library Name | Typical Install Command | Maturity |
|---|---|---|---|
| Python | Boto3 | pip install boto3 |
Stable, actively maintained |
| Java | AWS SDK for Java | Add to Maven/Gradle dependencies | Stable, actively maintained |
| JavaScript | AWS SDK for JavaScript v3 | npm install @aws-sdk/client-textract |
Stable, actively maintained |
| Go | AWS SDK for Go v2 | go get github.com/aws/aws-sdk-go-v2/service/textract |
Stable, actively maintained |
| C++ | AWS SDK for C++ | Via CMake and package managers | Stable, actively maintained |
| Ruby | AWS SDK for Ruby | gem install aws-sdk-textract |
Stable, actively maintained |
| .NET | AWS SDK for .NET | dotnet add package AWSSDK.Textract |
Stable, actively maintained |
| PHP | AWS SDK for PHP | composer require aws/aws-sdk-php |
Stable, actively maintained |
Each SDK provides specific clients for Textract, such as TextractClient in the AWS SDK for Python (Boto3) or TextractClient in the AWS SDK for JavaScript v3. These clients encapsulate the API operations and facilitate interaction with the Textract service endpoints. The official AWS documentation provides detailed API references and code examples for each supported language, guiding developers through various use cases and configurations for AWS Textract operations.
Installation
Installation of the AWS SDKs generally follows the standard practices for each programming language's ecosystem. Prior to installation, ensure you have the appropriate language runtime and package manager set up in your development environment. For example, Python requires pip, Node.js requires npm or yarn, Java typically uses Maven or Gradle, and PHP uses Composer. Configuration of AWS credentials is also essential for the SDKs to authenticate with your AWS account. This can be done via environment variables, shared credential files (~/.aws/credentials), or IAM roles for applications running on AWS infrastructure.
Python (Boto3)
Boto3 is the AWS SDK for Python. To install, use pip:
pip install boto3
After installation, configure your AWS credentials. A common method is to set environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, and AWS_DEFAULT_REGION. For more advanced credential management, refer to the Boto3 credential configuration documentation.
Java (AWS SDK for Java)
For Java projects, you typically add the SDK as a dependency in your build tool. For Maven, add the following to your pom.xml:
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>textract</artifactId>
<version>2.x.x</version> <!-- Replace with the latest version -->
</dependency>
</dependencies>
For Gradle, add to your build.gradle:
dependencies {
implementation 'software.amazon.awssdk:textract:2.x.x' // Replace with the latest version
}
The AWS SDK for Java Developer Guide provides comprehensive setup instructions.
JavaScript (AWS SDK for JavaScript v3)
The AWS SDK for JavaScript v3 is modular. Install the Textract client specifically:
npm install @aws-sdk/client-textract
For configuring credentials in a Node.js environment, environment variables or a shared credentials file are common. In a browser environment, temporary credentials obtained via Amazon Cognito or IAM roles are typically used for security. Consult the AWS SDK for JavaScript v3 credential loading guide for detailed options.
Quickstart example
This Python example demonstrates how to use the Boto3 SDK to detect text in a document stored in an Amazon S3 bucket. Before running this code, ensure you have Boto3 installed, AWS credentials configured, and an image file (e.g., document.png) uploaded to an S3 bucket (e.g., my-textract-bucket).
import boto3
def detect_text_from_s3(bucket_name, document_name):
client = boto3.client('textract', region_name='us-east-1') # Specify your region
response = client.detect_document_text(
Document={
'S3Object': {
'Bucket': bucket_name,
'Name': document_name
}
}
)
print('Detected Text:')
for item in response['Blocks']:
if item['BlockType'] == 'LINE':
print(item['Text'])
if __name__ == '__main__':
# Replace with your S3 bucket name and document key
s3_bucket = 'my-textract-bucket'
s3_document_key = 'document.png'
detect_text_from_s3(s3_bucket, s3_document_key)
This script initializes a Textract client, calls the detect_document_text method with the S3 object details, and then iterates through the returned Blocks to print out each detected line of text. For more complex scenarios, such as extracting forms or tables, the analyze_document API method would be used, often with asynchronous processing for larger documents. The AWS Textract Developer Guide offers extensive examples for various document analysis tasks.
Community libraries
While the official AWS SDKs are the primary and most robust way to interact with Textract, the developer community sometimes creates higher-level abstractions or specialized tools built on top of the SDKs. These community-driven projects can sometimes offer simplified interfaces for specific use cases, integrate Textract with other frameworks, or provide utilities for data post-processing.
However, when considering community libraries, it is important to evaluate their maintenance status, documentation quality, and alignment with the latest Textract API features. Projects that are not officially supported by AWS may not always keep pace with service updates, potentially leading to compatibility issues or missing new functionalities. Developers should review the source code and community activity to ensure the library is suitable for production environments.
For example, some might find Python libraries that wrap Boto3 for specific document types, or utilities that help visualize Textract output. A common use case for community tools might be to simplify the parsing of JSON output from Textract into more accessible data structures, or to integrate Textract with other non-AWS specific data processing pipelines. For instance, a developer might create a custom library to convert Textract's form key-value pairs into a pandas DataFrame for easier data manipulation in Python. While there isn't one universally recognized community library for Textract due to the comprehensive nature of the official SDKs, developers often share custom scripts and utilities on platforms like GitHub to address specific integration challenges. The AWS Developer Community pages can be a starting point for discovering such contributions or engaging with other developers for best practices.