AWS S3 Bucket Implementation In Java
Posted By : Vivek Joshi | 25-Nov-2018
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
Prerequisites:- For using S3 bucket for storage we first have to create an AWS account for using Amazon’s S3 services. After
Setup S3 with java
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; } }
And Third (name getFileUrlFromS3) used for retrieve file from
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Vivek Joshi
Vivek is Web App Developer in Java Technology and also working on NodeJS.