Expert AWS Development
上QQ阅读APP看书,第一时间看更新

AWS SDK for Java

Let's start with the Java SDK. This SDK helps to minimize the complexity and provision to coding using Java APIs for AWS Services such as Amazon EC2, Amazon DynamoDB, Amazon S3, and many more. You can download a single package from the AWS website which includes the AWS Java library and code samples with documentation.

Currently, you can download the AWS SDK for Java v1.11.x code base and AWS has recently launched an AWS SDK for Java v2.0, which is major code change. This version is built on Java 8. It has added features such as non-blocking I/O and a pluggable API layer (by default, it will use Apache but you can change this as per your project needs). In this version, you can see some API changes:

  • Client builders are the only way to create the client services, which means the clients are immutable after creation
  • All Plain Old Java Objects (POJOsare immutable and must be created from the builder
  • Many region classes such as Region, Regions, and RegionUtils are merged into a single Region class
This AWS SDK for Java v2.0 is a developer preview version and not recommended for production use.

Let's explore how to install, set up, and use AWS SDK for Java.

 You need to set up AWS SDK for Java on your machine to use in your project. Please perform the following steps to set up the environment and run the sample code in Java using the AWS SDK:

  1. AWS account setup and IAM user creation: You have to set up an AWS account and credentials to use AWS SDK. To increase the level of security for your AWS account, it is always preferable to create an IAM user. Use the created IAM user instead of the root user. Once you create an IAM user, you have to create an access key. You can download or view the access key ID and secret access key in the resulting dialog box. It's always best practice to download and store them in your local environment.
  2. AWS credentials and region setup: For your local application development, you need to set up credentials and regions.

The AWS credentials profile file is located in your filesystem. It should be at the following path:

    • For Linux, macOS, or Unix: ~/.aws/credentials
    • For Windows: C:\Users\USERNAME\.aws\credentials

The file format should be as follows:

[default]
aws_access_key_id = downloaded_access_key_id
aws_secret_access_key = downloaded_secret_access_key

Another alternative is to set up AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables. Now, replace your AWS access key ID and secret access key for the values of downloaded_access_key_id and downloaded_secret_access_key.

The AWS region configuration file is located in your filesystem. It should be at the following path:

    • For Linux, macOS, or Unix: ~/.aws/config
    • For Windows: C:\Users\USERNAME\.aws\config

This file format should be as follows:

[default]
region = your_region_name

Now, replace your AWS region for the value or region. Another alternative is you can set up AWS_REGION as an environment variable.

  • Java Development Environment: JDK 6.0 or later versions are required for the AWS SDK. The latest JDK versions are available for download from the Oracle website. J2SE 6.0 does not support SHA 256-signed SSL certificates, which are required for all HTTP connections with AWS after September 2015. You can use J2SE7.0 or newer versions, which are not affected by the certificate issue.

You can use different methods to include the AWS SDK for your Java project. We will explore all methods in this chapter:

    • Apache Maven: You can use specific SDK components or the full SDK with the help of Apache Maven.
    • Gradle: Maven Bill of Materials (BOM) in a Gradle project can be used to automatically manage the dependency for your project.
    • Eclipse IDE: The AWS toolkit can be integrated into an existing Eclipse IDE. It will automatically download, install, and update the Java SDK with a few settings.