Rest API to upload documents on AMAZON BUCKET

Posted By : Shubhankar Ajmani | 30-May-2020

Loading...

Rest API to upload documents on AMAZON BUCKE

Amazon Web services provide a good alternative to upload and retrieve data to their cloud using amazon S3 bucket which can be used in many ways in projects.

For uploading files to s3 we require an s3 bucket access and permissions by using Amazon AWS services.

Following are the required attributes for amazon s3 bucket connection:

  • Bucket Name
  • Region
  • Access Name
  • Access Secret
  • Bucket Url

In spring boot we need to first define the configuration in the application.properties file

APPLICATION.PROPERTIES

s3.buckek.name=XXXXX

s3.default.folder=XXX

s3.access.name=XXXXX

s3.access.secret=XXX

region=XXXXX

Now we define our business logic to upload and downloads file to s3 bucket.

For this, we need a multipart file and autowired dependency injection of Amazon S3 client.

SERVICE CLASS

@Service

public class AmazonS3ClientServiceImpl {

@Autowired

private AmazonS3 s3client;

@Value("${s3.buckek.name}")

String defaultBucketName;

@Value("${s3.default.folder}")

String defaultBaseFolder;

private final long EXPIRATION_TIME = 1000 * 60 * 60;

public List<Bucket> getAllBuckets() {

return s3client.listBuckets();

}

/**

*To Upload File

* @param uploadFile

* @param name

* @return download URl for uploaded file

* @author Shubhankar

*/

public String uploadFile(MultipartFile uploadFile, String name) throws IOException {

File file = convertMultiPartToFile(uploadFile);

String url = null;

String key = "uploads/";

String prefix = "";

String bucketName = defaultBucketName;

Object listResponse;

PutObjectResult putObjectResult = null;

// boolean b = s3client.doesObjectExist(defaultBucketName, prefix);

if (name != null && uploadFile != null) {

putObjectResult = s3client.putObject(defaultBucketName, key + name + "/" + uploadFile.getOriginalFilename(), file);

url = generatePresignedUrl(uploadFile.getOriginalFilename(), name);

file.delete();

} else {

if (name != null) {

throw new DataAlreadyExistException("Attachment Type Must Not be Empty");

} else {

throw new DataAlreadyExistException("Please Attach the file");

}

}

return url;

}

/**

*To Convert Multipart File into File

* @param file

*

* @return Converted File output

* @author Shubhankar

*/

private File convertMultiPartToFile(MultipartFile file) throws IOException {

File convFile = new File(file.getOriginalFilename());

FileOutputStream fos = new FileOutputStream(convFile);

fos.write(file.getBytes());

fos.close();

return convFile;

}

/**

*To generate download URL

* @param fileName

* @param folderName

* @return generated Output String

* @author Shubhankar

*/

public String generatePresignedUrl(String fileName, String folderName) {

// AmazonS3 s3Client = gets3Client();

Date expiration = new Date();

long timestamp = expiration.getTime();

timestamp += EXPIRATION_TIME;// link expiration time is 1 hour

expiration.setTime(timestamp);

GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(

defaultBucketName, defaultBaseFolder + "/" + folderName + "/" + fileName).withMethod(HttpMethod.GET).withExpiration(expiration);

URL url = s3client.generatePresignedUrl(generatePresignedUrlRequest);

return url.toString();

}

}

Further we can also generate a predesigned url to downlaod the uploaded file using predefined methods provided by the Amazon API which by using its algorithm generates the url for a particular expiration time.

Futher we require a Controller class to call service methods and to create REST API for our use

CONTROLLER CLASS

@RestController

@RequestMapping(UrlMappings.UPLOAD_FILE)

public class FileHandlerController {

@Autowired

AmazonS3ClientServiceImpl s3Factory;

/**

* To upload the documents

*

* @param files

* @return

* @throws IOException

* @author Shubhankar

*/

@PostMapping(path = "/upload")

public ResponseEntity<ResponseDTO<Map<String, String>>> uploadFile(@RequestParam String poNumber, @RequestPart(value = "file", required = false) MultipartFile files) throws IOException {

String url = s3Factory.uploadFile(files, poNumber);

Map<String, String> result = new HashMap<>();

result.put("key", files.getOriginalFilename());

result.put("url",url);

return ResponseGenerator.generateCreatedResponse(result);

}

@GetMapping(path = "/download")

public ResponseEntity<ResponseDTO<String>> generateUrl(@RequestParam String fileName, @RequestParam String folderName) {

String s = s3Factory.generatePresignedUrl(fileName, folderName);

return ResponseGenerator.generateCreatedResponse(s);

}

}

The controller API’s can be further used according to requirement.

We can also specify further the file size required to upload the file and the type of file.

The futher dependicies reuired to add in the builde.gradle file

compile group: 'com.amazonaws', name: 'aws-java-sdk-s3', version: '1.11.667'

You can make use of Google Cloud to upload documents as an alternative to the Amazon S3 bucket. At Oodles, we are an ERP development company that specializes in augmenting businesses with best-in-class software services. Our dedicated team of ERP developers uses an extensive technology stack of java, spring boot, angular, MongoDB, maven, groovy &grails, and more. Contact our experts to upgrade to systems with revolutionary new technologies.