How to get an object from S3 bucket using Java – AWS S3 GetObject

2 weeks ago Lalit Bhagtani 0

AWS S3 GetObject – In this tutorial, we will learn about how to get an object from Amazon S3 bucket using java language.

Project Setup

Create a simple maven project in your favorite IDE and add below mentioned dependency in your pom.xml file.

<dependency>
  <groupId>com.amazonaws</groupId>
  <artifactId>aws-java-sdk-s3</artifactId>
  <version>1.11.533</version>
</dependency>

For latest version of aws library, check this page.

S3 Connection

Create an object of AmazonS3 ( com.amazonaws.services.s3.AmazonS3 ) class for sending a client request to S3. To get instance of this class, we will use AmazonS3ClientBuilder builder class. It requires three important parameters :- 

  1. Region :- It is a region where S3 table will be stored.
  2. ACCESS_KEY :- It is a access key for using S3. You can generate this key, using aws management console.
  3. SECRET_KEY :- It is a secret key of above mentioned access key.

Here is a code example :-

AmazonS3 s3 = AmazonS3ClientBuilder.standard()
				.withRegion(Regions.AP_SOUTH_1)
				.withCredentials(new AWSStaticCredentialsProvider
                                                    (new BasicAWSCredentials("ACCESS_KEY","SECRET_KEY")))
				.build();

Get Object

An AmazonS3.getObject method gets an object from the S3 bucket. A bucket name and Object Key are only information required for getting the object.

This method returns an object, which contains Object metadata and Object content as an HTTP stream. The user should be very careful while using this method as an underlying HTTP connection cannot be reused until the user finishes reading the data and closes the stream. Few points to keep in mind while using this method:- 

  1. Use the data from the input stream in AmazonS3 object as soon as possible
  2. Read all data from the stream.
  3. Close the input stream in AmazonS3 object as soon as possible

If the above points are not followed, the user can run out of resources by allocating too many open, but unused, HTTP connections.

The method getObject(GetObjectRequest getObjectRequest, File destinationFile) should be used as this method ensures that the underlying HTTP stream resources are automatically closed and Amazon S3 clients handles immediate storage of the object contents to the specified file.

Example

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectInputStream;

public class GetObject {

	public static void main(String[] args) {

		/* Create S3 Client Object */
		AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRegion(Regions.AP_SOUTH_1)
				.withCredentials(new AWSStaticCredentialsProvider(
						new BasicAWSCredentials("ACCESS_KEY","SECRET_KEY")))
				.build();
		
		S3ObjectInputStream inputStream = null;
		FileOutputStream fos = null;
		
		try {			
			/* First way to Get Object from Amazon S3 */
			
			/* Send Get Object Bucket Request */
			S3Object object = s3.getObject("lb-aws-learning", "s3.png");
			
			/* Get Object InputStream */
            inputStream = object.getObjectContent();
            
            /* Write InputStream Data to File */
            fos = new FileOutputStream( new File("s3.png") );
            byte[] buffer = new byte[1024];
            int length = 0;
            while ((length = inputStream.read(buffer)) &amp;amp;gt; 0) {
                fos.write(buffer, 0, length);
            }
            
            /* Second Way to Get Object from Amazon S3 */
            
            /* Create an Object of GetObjectRequest */
            /* Arguments: Bucket Name, Object Key */
            GetObjectRequest request = new GetObjectRequest("lb-aws-learning", "s3.png");
            
            /* Send Get Object Bucket Request &amp;amp;amp; Save it to File 's4.png' */
			ObjectMetadata metadata = s3.getObject(request, new File("s4.png"));
			
			System.out.println("Last Modified Date" + metadata.getLastModified());
			
		} catch (AmazonServiceException | IOException e) {

			System.out.println(e.getMessage());
		} finally {
			try {
				if (inputStream != null) {
					inputStream.close();				
				}
				if (fos != null) {
					fos.close();
				}
			} catch (IOException e) {
				System.out.println(e.getMessage());
			}			
		}
	}
}

References :-

  1. GetObjectRequest Java Docs
  2. S3Object Java Docs
  3. ObjectMetadata Java Docs

That’s all for how to get an object from Amazon S3 bucket using java language (AWS S3 getObject). If you liked it, please share your thoughts in comments section and share it with others too.