AWS S3 Bucket Implementation In Java

Posted By : Vivek Joshi | 25-Nov-2018

Introduction :- Amazon S3 (Simple Storage Service) is a storage service provided by the Amazon on internet. It is useful to store the huge amount of data into the internet.

Amazon S3 is a storage service, which is different from file cloud storage. Each object is stored as a record with its metadata included and given an ID number. Applications uses this ID number to get to an object from S3 bucket. Unlike file cloud storage, a developer can access these files through a rest api for an application. Customer can store and download practically any file or object that is up to 5 GB in size.


Prerequisites:- For using S3 bucket for storage we first have to create an AWS account for using Amazon’s S3 services. After create a account we have to
 
create s s3 bucket into our account (Note:- the bucket name should be unique).
 

Setup S3 with java application :- For using S3 service with your java application we need a AWS SDK for java dependency. We can download this dependency from Maven.

 

Maven dependency for AWS S3 is :

<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-s3 -->

<dependency>

   <groupId>com.amazonaws</groupId>

   <artifactId>aws-java-sdk-s3</artifactId>

   <version>1.11.455</version>

</dependency>

 

 

Java Example for S3 bucket:- Example for implementing S3 bucket into a java application for storing and retrieve files.

 

package com.example.amazon.s3;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Date;
import java.util.Objects;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.amazonaws.SdkClientException;
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.GeneratePresignedUrlRequest;

public class AwsS3BucketUtil {

	private String region = "<region of s3 bucket>";

	private String bucketName = "<bucket name>";

	private String fileLocation = "<file location/folder name in s3 bucket>";

	private String awsAccessKey = "< your awsAccessKey>";

	private String awsSecretKey = "<your awsSecretKey>";

	private AmazonS3 s3Client = null;

	private Logger logger = LoggerFactory.getLogger(AwsS3BucketUtil.class);

	/* Create or get Amazon S3 client */

	public AmazonS3 getS3BucketClient() {

		if (Objects.isNull(s3Client)) {

			BasicAWSCredentials awsCredentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey);

			s3Client = AmazonS3ClientBuilder.standard().withRegion(Regions.fromName(region))
					.withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).build();

			/*
			 * bellow code is use if you already provide you s3 bucket direct access without
			 * credential
			 */

			s3Client = AmazonS3ClientBuilder.standard().withRegion(Regions.fromName(region)).build();

		}
		return s3Client;
	}

	public Boolean saveFileIntoS3(String fileName, File file) {

		Boolean isFileSavedInS3 = false;

		AmazonS3 awsS3Client = getS3BucketClient();

		try {
			awsS3Client.putObject(bucketName, awsFolder + File.separator + fileName, file);

			isFileSavedInS3 = true;
		} catch (SdkClientException e) {

			logger.debug("Exception in  saving file into S3 {}", e);
		}
		return isFileSavedInS3;
	}

	public String getFileUrlFromS3(String fileName) {
		AmazonS3 awsS3Client = getS3BucketClient();

		String awsWalletFileLocation = awsFolder + File.separator + fileName;
		Date expiration = new Date();
		long expTimeMillis = expiration.getTime();
		expTimeMillis += 1000 * 60 * 60; // for 1 hours
		expiration.setTime(expTimeMillis);
		try {

			GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName,
					awsWalletFileLocation).withExpiration(expiration);

			URL preSignedUrl = awsS3Client.generatePresignedUrl(generatePresignedUrlRequest);
			return preSignedUrl.toString();

		} catch (SdkClientException | IOException e) {
			logger.error("error in geting file from S3 {}", e);
		}
		return null;
	}
}
 

 

 

Conclusion :- In the above code we create a class with three functions : first (name getS3BucketClient) is use for get the java s3 bucket client object. Second (name saveFileIntoS3) is used for store any file into s3 bucket with a particular name and file location.

And Third (name getFileUrlFromS3) used for retrieve file from s3 bucket which will return a file URL which will valid for 1 hour.

 
 
 
 
 
 
 
 

About Author

Author Image
Vivek Joshi

Vivek is Web App Developer in Java Technology and also working on NodeJS.

Request for Proposal

Name is required

Comment is required

Sending message..