Skip to content

Commit

Permalink
Initial work on S3 integration.
Browse files Browse the repository at this point in the history
  • Loading branch information
dds14002 committed Feb 3, 2020
1 parent d2e6a47 commit a46f8ed
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 0 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}

}
4 changes: 4 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
server.servlet.contextPath=/3dsysnavbackend

app.s3.credentialsProfile=s3
app.s3.bucket=3dsysnavstorage
app.s3.region=us-west-2

0 comments on commit a46f8ed

Please sign in to comment.