From a46f8ed44c17c471074e03ac4c3f996bee07c2ba Mon Sep 17 00:00:00 2001 From: Daniel Scalzi Date: Mon, 3 Feb 2020 15:31:06 -0500 Subject: [PATCH] Initial work on S3 integration. --- build.gradle | 4 ++ .../controller/ResourceController.java | 29 ++++++++++++ .../service/AmazonClient.java | 45 +++++++++++++++++++ .../service/ResourceService.java | 30 +++++++++++++ src/main/resources/application.properties | 4 ++ 5 files changed, 112 insertions(+) create mode 100644 src/main/java/com/lmco/spectrum/systemnavigation3d/controller/ResourceController.java create mode 100644 src/main/java/com/lmco/spectrum/systemnavigation3d/service/AmazonClient.java create mode 100644 src/main/java/com/lmco/spectrum/systemnavigation3d/service/ResourceService.java diff --git a/build.gradle b/build.gradle index 1754e01..d0dbc3e 100644 --- a/build.gradle +++ b/build.gradle @@ -33,6 +33,10 @@ dependencies { testImplementation('org.springframework.boot:spring-boot-starter-test') { exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' } + + implementation platform('com.amazonaws:aws-java-sdk-bom:1.11.714') + implementation 'com.amazonaws:aws-java-sdk-s3' + } test { diff --git a/src/main/java/com/lmco/spectrum/systemnavigation3d/controller/ResourceController.java b/src/main/java/com/lmco/spectrum/systemnavigation3d/controller/ResourceController.java new file mode 100644 index 0000000..bceb072 --- /dev/null +++ b/src/main/java/com/lmco/spectrum/systemnavigation3d/controller/ResourceController.java @@ -0,0 +1,29 @@ +package com.lmco.spectrum.systemnavigation3d.controller; + +import com.lmco.spectrum.systemnavigation3d.service.ResourceService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +@Controller +@RequestMapping("api/resources") +public class ResourceController { + + @Autowired + ResourceService resourceService; + + @RequestMapping(value = "/pdf/{id}", method = RequestMethod.GET) + public ResponseEntity testEndpoint(@PathVariable("id") Long id){ + // TODO call ResourceService + String x = resourceService.getFileTest(); + if(x == null) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } + return ResponseEntity.ok(x); + } + +} diff --git a/src/main/java/com/lmco/spectrum/systemnavigation3d/service/AmazonClient.java b/src/main/java/com/lmco/spectrum/systemnavigation3d/service/AmazonClient.java new file mode 100644 index 0000000..99b1f40 --- /dev/null +++ b/src/main/java/com/lmco/spectrum/systemnavigation3d/service/AmazonClient.java @@ -0,0 +1,45 @@ +package com.lmco.spectrum.systemnavigation3d.service; + +import com.amazonaws.auth.profile.ProfileCredentialsProvider; +import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.AmazonS3ClientBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import javax.annotation.PostConstruct; + +@Service +public class AmazonClient { + + private static final Logger log = LoggerFactory.getLogger(AmazonClient.class); + + private AmazonS3 amazonS3; + + @Value("${app.s3.credentialsProfile}") + private String s3CredentialsProfile; + @Value("${app.s3.bucket}") + private String s3Bucket; + @Value("${app.s3.region}") + private String s3Region; + + @PostConstruct + private void initializeAmazon() { + log.debug("Connecting to Amazon S3 using credentials profile {} on region {}.", s3CredentialsProfile, s3Region); + log.debug("Using bucket {}.", s3Bucket); + this.amazonS3 = AmazonS3ClientBuilder.standard() + .withCredentials(new ProfileCredentialsProvider(s3CredentialsProfile)) + .withRegion(s3Region) + .build(); + } + + public AmazonS3 getAmazonS3Connection() { + return this.amazonS3; + } + + public String getS3Bucket() { + return this.s3Bucket; + } + +} diff --git a/src/main/java/com/lmco/spectrum/systemnavigation3d/service/ResourceService.java b/src/main/java/com/lmco/spectrum/systemnavigation3d/service/ResourceService.java new file mode 100644 index 0000000..602528d --- /dev/null +++ b/src/main/java/com/lmco/spectrum/systemnavigation3d/service/ResourceService.java @@ -0,0 +1,30 @@ +package com.lmco.spectrum.systemnavigation3d.service; + +import com.amazonaws.services.s3.AmazonS3; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.lang.Nullable; +import org.springframework.stereotype.Service; +import org.springframework.util.StreamUtils; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; + +@Service +public class ResourceService { + + @Autowired + AmazonClient amazonClient; + + @Nullable + public String getFileTest() { + AmazonS3 amazonS3 = amazonClient.getAmazonS3Connection(); + try (InputStream is = amazonS3.getObject(amazonClient.getS3Bucket(), "test.txt").getObjectContent()) { + return StreamUtils.copyToString(is, StandardCharsets.UTF_8); + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 65e8fe9..f0c6e94 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,5 @@ server.servlet.contextPath=/3dsysnavbackend + +app.s3.credentialsProfile=s3 +app.s3.bucket=3dsysnavstorage +app.s3.region=us-west-2