Getting Started with the AWS SDK for Java: A Beginner’s Guide
What you’ll learn
- Goal: Set up a Java project, install the AWS SDK, authenticate, and call basic AWS services (S3 and DynamoDB) with runnable examples.
- Assumptions: Java 11+ installed and basic Java familiarity.
1. Choose the right SDK version
- Use AWS SDK for Java v2 for modern features, non-blocking I/O, and simpler dependency management.
- Use v1 only for legacy projects.
2. Create a new Java project
- Using Maven: create a project and add the SDK dependencies.
- Minimal pom.xml dependencies (examples for v2):
xml
software.amazon.awssdk s3 2.20.0 software.amazon.awssdk dynamodb 2.20.0
- For Gradle, add corresponding implementation lines.
3. Configure credentials and region
- Recommended methods (in order): Environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY), shared credentials file (~/.aws/credentials), or IAM role (for EC2/ECS).
- Set default region via AWS_REGION or in code:
java
Region region = Region.US_EAST_1;S3Client s3 = S3Client.builder().region(region).build();
4. Basic S3 example: upload and download
- Upload a file:
java
PutObjectRequest request = PutObjectRequest.builder() .bucket(“my-bucket”) .key(“example.txt”) .build(); s3.putObject(request, RequestBody.fromFile(Paths.get(“local-file.txt”)));
- Download a file:
java
GetObjectRequest getReq = GetObjectRequest.builder() .bucket(“my-bucket”) .key(“example.txt”) .build(); s3.getObject(getReq, ResponseTransformer.toFile(Paths.get(“downloaded.txt”)));
- Tips: handle S3 exceptions (S3Exception), enable retry/backoff for production.
5. Basic DynamoDB example: put and get item
- Create a DynamoDbClient and write an item:
java
DynamoDbClient ddb = DynamoDbClient.builder().region(region).build(); Map item = new HashMap<>();item.put(“Id”, AttributeValue.builder().s(“123”).build());item.put(“Name”, AttributeValue.builder().s(“Example”).build()); PutItemRequest putReq = PutItemRequest.builder() .tableName(“MyTable”) .item(item) .build(); ddb.putItem(putReq);
- Read an item:
java
GetItemRequest getReq = GetItemRequest.builder() .tableName(“MyTable”) .key(Map.of(“Id”, AttributeValue.builder().s(“123”).build())) .build(); GetItemResponse resp = ddb.getItem(getReq);
- Tip: use DynamoDB Enhanced Client for object mapping and simpler code.
6. Error handling and retries
- Catch service-specific exceptions (S3Exception, DynamoDbException).
- Use SDK’s built-in retry configuration or customize using RetryPolicy on the client builder.
7. Security best practices
- Never hard-code credentials.
- Use least-privilege IAM policies for service access.
- Prefer IAM roles for compute services (EC2, ECS, Lambda).
8. Local testing and tooling
- For S3/DynamoDB local testing use localstack or DynamoDB Local. Configure endpoints in the client builder for local endpoints during tests.
9. Next steps & recommendations
- Learn async/non-blocking patterns with the v2 async clients.
- Explore higher-level libraries (DynamoDB Enhanced Client).
- Add logging and metrics (CloudWatch) and automate deployment with IaC (CloudFormation/Terraform).
Example project checklist
- Create Maven/Gradle project.
- Add SDK v2 dependencies.
- Configure credentials/region.
- Implement S3 upload/download and DynamoDB put/get examples.
- Add error handling, retries, and unit/integration tests (using localstack).
- Apply IAM least-privilege policies and move credentials to environment or roles.
If you want, I can generate a complete sample Maven project (pom.xml + Java classes) you can clone and run.
Leave a Reply