Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

,

Task3: Create a application using spring boot and upload file into s3 bucket
Project has been created
Added dependencies in pom.xml

Edit the application.properties with following properties

Added the env variables


Create fileuploadservice.java interface inside src/main/java and add the following contents

Create AWSS3Service.java class inside src/main/java and the following contents. This class


implements FileService so we need to override one of the method uploadFile() and provide
the implementation for that
package com.fileupload.service;

import java.io.IOException;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.server.ResponseStatusException;

import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ObjectMetadata;

@Service
public class AWSS3Service implements fileuploadservice {
@Autowired
private AmazonS3Client awsS3Client;

@Override
public String uploadFile(MultipartFile file) {

String filenameExtension =
StringUtils.getFilenameExtension(file.getOriginalFilename());

String key = UUID.randomUUID().toString() + "."


+filenameExtension;

ObjectMetadata metaData = new ObjectMetadata();


metaData.setContentLength(file.getSize());
metaData.setContentType(file.getContentType());

try {
awsS3Client.putObject("myfirst2bucket", key,
file.getInputStream(), metaData);
} catch (IOException e) {
throw new
ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "An exception
occured while uploading the file");
}

awsS3Client.setObjectAcl("myfirst2bucket", key,
CannedAccessControlList.PublicRead);

return awsS3Client.getResourceUrl("myfirst2bucket", key);


}
}

Create UploadFileController.java class inside src/main/java and add the following contents


package com.fileupload.controller;

import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.fileupload.service.fileuploadservice;

@RestController
@RequestMapping("/api/file")

public class UploadFileController {


@Autowired
private fileuploadservice fileservice;

@PostMapping
public ResponseEntity<Map<String, String>>
uploadFile(@RequestParam("file") MultipartFile file) {
String publicURL = fileservice.uploadFile(file);
Map<String, String> response = new HashMap<>();
response.put("publicURL", publicURL);
return new ResponseEntity<> (response, HttpStatus.CREATED);
}

}
2)

Create an AWS s3 bucket

3)Create an IAM user to access the S3 bucket

Created IAM user “coder” with amazons3full access


4)

Upload the documents to ec2 instance and run the docker container by using environment variables
File has been uploaded to s3 bucket

You might also like