From 2cb69bc981c7ae360e8aed2532d7b1f425144c36 Mon Sep 17 00:00:00 2001 From: Ryan Amari Date: Thu, 29 Aug 2024 08:17:44 -0400 Subject: [PATCH] ALS-7014: Move s3 configs to property files --- .../hpds/data/upload/SignUrlService.java | 48 +++++++++++-------- .../avillach/hpds/service/PicSureService.java | 4 +- .../resources/application-bdc-auth.properties | 6 ++- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/data/src/main/java/edu/harvard/hms/dbmi/avillach/hpds/data/upload/SignUrlService.java b/data/src/main/java/edu/harvard/hms/dbmi/avillach/hpds/data/upload/SignUrlService.java index f6d4d988..02060b81 100644 --- a/data/src/main/java/edu/harvard/hms/dbmi/avillach/hpds/data/upload/SignUrlService.java +++ b/data/src/main/java/edu/harvard/hms/dbmi/avillach/hpds/data/upload/SignUrlService.java @@ -2,6 +2,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import software.amazon.awssdk.core.sync.RequestBody; import software.amazon.awssdk.regions.Region; @@ -20,18 +22,24 @@ @Component public class SignUrlService { - private static Logger log = LoggerFactory.getLogger(SignUrlService.class); + private final String bucketName; + private final int signedUrlExpiryMinutes; + private final Region region; - public static void main(String[] args) { - SignUrlService signUrlService = new SignUrlService(); + private static Logger log = LoggerFactory.getLogger(SignUrlService.class); - signUrlService.uploadFile(new File("/Users/ryan/Downloads/test.txt"), "ramari-testing", "test2.txt"); - String presignedGetUrl = signUrlService.createPresignedGetUrl("ramari-testing", "test2.txt"); - log.info(presignedGetUrl); + @Autowired + public SignUrlService( + @Value("${data-export.s3.bucket-name}") String bucketName, + @Value("${data-export.s3.region}") String region, + @Value("${data-export.s3.signedUrl-expiry-minutes:60}") int signedUrlExpiryMinutes + ) { + this.bucketName = bucketName; + this.signedUrlExpiryMinutes = signedUrlExpiryMinutes; + this.region = Region.of(region); } - public void uploadFile(File file, String bucketName, String objectKey) { - Region region = Region.US_EAST_1; + public void uploadFile(File file, String objectKey) { S3Client s3 = S3Client.builder() .region(region) .build(); @@ -54,19 +62,21 @@ public void putS3Object(S3Client s3, String bucketName, String objectKey, File f log.info("Successfully placed " + objectKey + " into bucket " + bucketName); } - public String createPresignedGetUrl(String bucketName, String keyName) { - S3Presigner presigner = S3Presigner.builder().region(Region.US_EAST_1).build(); - GetObjectRequest objectRequest = GetObjectRequest.builder() - .bucket(bucketName) - .key(keyName) - .build(); + public String createPresignedGetUrl(String keyName) { + PresignedGetObjectRequest presignedRequest; + try (S3Presigner presigner = S3Presigner.builder().region(region).build()) { + GetObjectRequest objectRequest = GetObjectRequest.builder() + .bucket(bucketName) + .key(keyName) + .build(); - GetObjectPresignRequest presignRequest = GetObjectPresignRequest.builder() - .signatureDuration(Duration.ofMinutes(10)) // The URL will expire in 10 minutes. - .getObjectRequest(objectRequest) - .build(); + GetObjectPresignRequest presignRequest = GetObjectPresignRequest.builder() + .signatureDuration(Duration.ofMinutes(signedUrlExpiryMinutes)) // The URL will expire in 10 minutes. + .getObjectRequest(objectRequest) + .build(); - PresignedGetObjectRequest presignedRequest = presigner.presignGetObject(presignRequest); + presignedRequest = presigner.presignGetObject(presignRequest); + } log.info("Presigned URL: [{}]", presignedRequest.url().toString()); return presignedRequest.url().toExternalForm(); diff --git a/service/src/main/java/edu/harvard/hms/dbmi/avillach/hpds/service/PicSureService.java b/service/src/main/java/edu/harvard/hms/dbmi/avillach/hpds/service/PicSureService.java index 66927b2c..7a0533a3 100644 --- a/service/src/main/java/edu/harvard/hms/dbmi/avillach/hpds/service/PicSureService.java +++ b/service/src/main/java/edu/harvard/hms/dbmi/avillach/hpds/service/PicSureService.java @@ -263,8 +263,8 @@ public ResponseEntity querySignedURL(@PathVariable("resourceQueryId") UUID query } if (result.getStatus() == AsyncResult.Status.SUCCESS) { File file = result.getFile(); - signUrlService.uploadFile(file, "ramari-testing", file.getName()); - String presignedGetUrl = signUrlService.createPresignedGetUrl("ramari-testing", file.getName()); + signUrlService.uploadFile(file, file.getName()); + String presignedGetUrl = signUrlService.createPresignedGetUrl(file.getName()); log.info("Presigned url: " + presignedGetUrl); return ResponseEntity.ok() .contentType(MediaType.TEXT_PLAIN) diff --git a/service/src/main/resources/application-bdc-auth.properties b/service/src/main/resources/application-bdc-auth.properties index 860f5d64..272b84ec 100644 --- a/service/src/main/resources/application-bdc-auth.properties +++ b/service/src/main/resources/application-bdc-auth.properties @@ -3,4 +3,8 @@ SMALL_TASK_THREADS = 1 LARGE_TASK_THREADS = 1 hpds.genomicProcessor.impl=localPatientDistributed -HPDS_GENOMIC_DATA_DIRECTORY=/opt/local/hpds/all/ \ No newline at end of file +HPDS_GENOMIC_DATA_DIRECTORY=/opt/local/hpds/all/ + +data-export.s3.bucket-name=pic-sure-auth-dev-data-export +data-export.s3.region=us-east-1 +data-export.s3.signedUrl-expiry-minutes=30 \ No newline at end of file