Skip to content

Commit

Permalink
ALS-7014: Move s3 configs to property files
Browse files Browse the repository at this point in the history
  • Loading branch information
ramari16 committed Aug 29, 2024
1 parent e6786cd commit 2cb69bc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion service/src/main/resources/application-bdc-auth.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ SMALL_TASK_THREADS = 1
LARGE_TASK_THREADS = 1

hpds.genomicProcessor.impl=localPatientDistributed
HPDS_GENOMIC_DATA_DIRECTORY=/opt/local/hpds/all/
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

0 comments on commit 2cb69bc

Please sign in to comment.