Professional Documents
Culture Documents
Task 3
Task 3
Task3: Create a application using spring boot and upload file into s3 bucket
Project has been created
Added dependencies in pom.xml
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());
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);
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")
@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)
Upload the documents to ec2 instance and run the docker container by using environment variables
File has been uploaded to s3 bucket