This section tells you about how to configure an IAM user to access S3 and install AWS SDK for Java. It also talks about how to create S3 buckets, put objects, and get objects using the sample code. It explains how the sample code runs as well.
AWS SDK for Java is a Java API for AWS and contains AWS the Java library, code samples, and Eclipse IDE support. You can easily build scalable applications by working with Amazon S3, Amazon Glacier, and more.
To get started with AWS SDK for Java, it is necessary to install the following on your development machine:
- J2SE Development Kit 6.0 or later
- Apache Maven
First, we set up an IAM user, create a user policy, and attach the policy to the IAM user in the IAM management console in order to securely allow the IAM user to access the S3 bucket. We can define the access control for the IAM user by configuring the IAM policy. Then, we install AWS SDK for Java by using Apache Maven and git.
There are two ways to install AWS SDK for Java, one is to get the source code from GitHub, and the other is to use Apache Maven. We use the latter because the official site recommends this way and it is much easier.
- Sign in to the AWS management console and move to the IAM console at https://console.aws.amazon.com/iam/home.
- In the navigation panel, click on Users and then on Create New Users.
- Enter the username and select Generate an access key for each user, then click on Create.
- Click on Download Credentials and save the credentials. We will use the credentials composed of Access Key ID and Secret Access Key to access the S3 bucket.
- Select the IAM user.
- Click on Attach User Policy.
- Click on Select Policy Template and then click on Amazon S3 Full Access.
- Click on Apply Policy.
Next, we clone a repository for the S3 Java sample application and run the application by using the Maven command (mvn
). First, we set up the AWS credentials to operate S3, clone the sample application repository from GitHub, and then build and run the sample application using the mvn
command:
- Create a credential file and put the access key ID and the secret access key in the credentials. You can see the access key ID and the secret access key in the credentials when we create an IAM user and retrieve the CSV file in the management console:
Tip
Downloading the example code
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
- Download the sample SDK application:
- Run the sample application:
The sample code works as shown in the following diagram; initiating the credentials to allow access Amazon S3, creating and listing a bucket in a region, putting, getting, and listing objects into the bucket, and then finally deleting the objects, and then the bucket:
Now, let's run the sample application and see the output of the preceding command, as shown in the following screenshot, and then follow the source code step by step:
Then, let's examine the sample code at aws-java-sample/src/main/java/com/amazonaws/samples/S3Sample.java
.
The AmazonS3Client
method instantiates an AWS service client with the default credential provider chain (~/.aws/credentials
). Then, the Region.getRegion
method retrieves a region object, and chooses the US West (Oregon) region for the AWS client:
The createBucket
method creates an S3 bucket with the specified name in the specified region:
The listBuckets
method lists and gets the bucket name:
The putObject
method uploads objects into the specified bucket. The objects consist of the following code:
The getObject
method gets the object stored in the specified bucket:
The ListObjects
method returns a list of summary information of the object in the specified bucket:
The deleteObject
method deletes the specified object in the specified bucket.
The reason to clean up objects before deleting the S3 bucket is that, it is unable to remove an S3 bucket with objects. We need to remove all objects in an S3 bucket first and then delete the bucket:
The deleteBucket
method deletes the specified bucket in the region.
The AmazonServiceException
class provides the error messages, for example, the request ID, HTTP status code, the AWS error code, for a failed request from the client in order to examine the request. On the other hand, the AmazonClientException
class can be used for mainly providing error responses when the client is unable to get a response from AWS resources or successfully make the service call, for example, a client failed to make a service call because no network was present: