Implementation of S3 Bucket in Java

Posted By : Pavan Kumar | 29-Jun-2020

Java

Loading...

S3 stands for Amazon Simple storage service.

s3 Bucket is accessible in two ways:

i) API-key and secret key

ii) Bucket region and Bucket name

In the case of API-key and secret key, we can declare bucket location as public because it can be accessed by the right API-key and secret key. It is secure.

In the case of the region and name of the bucket, we can't assign the bucket address as public. It needs to be private. We can give authority to the production server or testing environment, not to the public.

Programatically, we can put, get, rename, and delete objects from bucket location.

S3 bucket provides us the facility to set an expiration time for accessing particular object. By default s3 bucket object expiration time is 15 minutes. Each bucket object can be accessed by authorized header information.

b) Following are the dependencies, before start working on AWS S3 bucket.

com.amazonaws

aws-java-sdk

1.11.163

AWS bucket price varies according to bucket region

For security purposes, bucket region and bucket location should be accessible from production environment only.

c) How to upload files to the location of s3 bucket.

public String uploadDocumentIntoToBucket(MultipartFile file, String uploadedFileLocation) throws Exception {

String updatedFileName = null;

String originalFileName = file.getOriginalFilename();

int dot = originalFileName.lastIndexOf(".");

String extension = (dot == -1) ? "" : originalFileName.substring(dot + 1);

updatedFileName = UUID.randomUUID().toString() + "." + extension;

try {

InputStream is = file.getInputStream();

ObjectMetadata meta = new ObjectMetadata();

meta.setContentLength(is.available());

AmazonS3 s3Client = AwsUtil.s3Authentication("bucket-region-name");

s3Client.putObject(new PutObjectRequest(uploadedFileLocation, updatedFileName, is, meta)

.withCannedAcl(CannedAccessControlList.Private));

is.close();

LOGGER.info("uploaded document to bucket: "+updatedFileName);

return updatedFileName;

}

catch (Exception ex) {

LOGGER.info("unable to upload file on bucket: "+ex.getMessage());

throw new Exception(messageService.getMessage("Unable to upload document"));

}

}

d) private file url generation

public static AmazonS3 s3Authentication(String region) {

AmazonS3 s3Client = AmazonS3Client.builder().withRegion(region).withPathStyleAccessEnabled(true).build();

return s3Client;

}

public static String generatePreSignedUrl(String fileName,String bucketName,String region){

AmazonS3 s3client = s3Authentication(region);

GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName,fileName);

generatePresignedUrlRequest.setMethod(HttpMethod.GET);

Date currentDate=new Date();

long timestamp=currentDate.getTime();

timestamp+=1000*60*60;// 1 hour

currentDate.setTime(timestamp);

generatePresignedUrlRequest.setExpiration(currentDate);

URL url = s3client.generatePresignedUrl(generatePresignedUrlRequest);

return url.toString();

}

We are a Java Development Company that enables enterprises to streamline operations and drive returns, using next-gen technologies, with our development services. Our services include developing applications for all your ERP needs from CRM, WFM, and HRM to eCommerce, accounting, and wealth management software. We use an extensive tech stack including Javascript and JUnit to develop end to end customized software for your enterprise. Get in touch with our experts to know more about how you can implement these technologies into your business.