Skip to content

Commit

Permalink
migrate to AWS SDK v2
Browse files Browse the repository at this point in the history
  • Loading branch information
jaroldwong committed Mar 14, 2024
1 parent d656c5d commit 02d9717
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 48 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ dependencies {
implementation("io.jsonwebtoken:jjwt-api:0.12.5")
runtimeOnly("io.jsonwebtoken:jjwt-impl:0.12.5")
runtimeOnly("io.jsonwebtoken:jjwt-jackson:0.12.5")
implementation("commons-io:commons-io:2.15.1")
implementation("org.apache.poi:poi:5.2.5")
implementation("org.apache.poi:poi-ooxml:5.2.5")
implementation("jakarta.inject:jakarta.inject-api:2.0.1")
Expand All @@ -53,6 +52,7 @@ dependencies {
implementation("org.javers:javers-core:7.3.7")
implementation("com.mysql:mysql-connector-j:8.3.0")
implementation("com.amazonaws:aws-java-sdk-s3:1.12.651")
implementation("software.amazon.awssdk:s3:2.25.7")

testImplementation("junit:junit")
testImplementation("com.h2database:h2:1.4.199")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.View;
import software.amazon.awssdk.services.s3.model.HeadObjectResponse;

@RestController
public class WorkloadSummaryReportController {
Expand Down Expand Up @@ -156,7 +157,7 @@ public ResponseEntity generateMultipleDepartments(@PathVariable long workgroupId
@RequestBody Optional<Map<Long, List<Long>>> departmentSnapshots
) {
authorizer.isDeansOffice();
final String fileName = year + (departmentSnapshots.isPresent() ? "_Workload_Snapshots" : "_Workload_Summary_Report") + ".xlsx";
final String fileName = year + (departmentSnapshots.isPresent() ? "_Workload_Snapshots" : "_Workload_Summary_Report") + "_TEST.xlsx";

// overwrite with empty file to update modified time
s3Service.upload(fileName, new byte[0]);
Expand Down Expand Up @@ -281,21 +282,21 @@ public Map<String, Map<String, Long>> getDownloadStatus(@PathVariable long year)

Map<String, Map<String, Long>> status = new HashMap<>();

ObjectMetadata workloadSummaries = s3Service.getMetadata(year + "_Workload_Summary_Report.xlsx");
ObjectMetadata workloadSnapshots = s3Service.getMetadata(year + "_Workload_Snapshots.xlsx");
HeadObjectResponse workloadSummaries = s3Service.getMetadata(year + "_Workload_Summary_Report.xlsx");
HeadObjectResponse workloadSnapshots = s3Service.getMetadata(year + "_Workload_Snapshots.xlsx");
if (workloadSummaries != null) {
Map<String, Long> md = new HashMap<>();

md.put("lastModified", workloadSummaries.getLastModified().getTime());
md.put("contentLength", workloadSummaries.getContentLength());
md.put("lastModified", workloadSummaries.lastModified().toEpochMilli());
md.put("contentLength", workloadSummaries.contentLength());

status.put("workloadSummaries", md);
}

if (workloadSnapshots != null) {
Map<String, Long> md = new HashMap<>();
md.put("lastModified", workloadSnapshots.getLastModified().getTime());
md.put("contentLength", workloadSnapshots.getContentLength());
md.put("lastModified", workloadSnapshots.lastModified().toEpochMilli());
md.put("contentLength", workloadSnapshots.contentLength());

status.put("workloadSnapshots", md);
}
Expand Down
23 changes: 8 additions & 15 deletions src/main/java/edu/ucdavis/dss/ipa/config/AWSConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package edu.ucdavis.dss.ipa.config;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;

@Configuration
public class AWSConfiguration {
Expand All @@ -22,15 +20,10 @@ public class AWSConfiguration {
String awsRegion;

@Bean
public AmazonS3 s3client() {
AWSCredentials credentials = new BasicAWSCredentials(
awsAccessKey, awsSecretKey
);
public S3Client s3Client() {
StaticCredentialsProvider credentialsProvider =
StaticCredentialsProvider.create(AwsBasicCredentials.create(awsAccessKey, awsSecretKey));

AmazonS3 s3Client =
AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(Regions.fromName(awsRegion)).build();

return s3Client;
return S3Client.builder().credentialsProvider(credentialsProvider).region(Region.of(awsRegion)).build();
}
}
60 changes: 35 additions & 25 deletions src/main/java/edu/ucdavis/dss/ipa/utilities/S3Service.java
Original file line number Diff line number Diff line change
@@ -1,58 +1,68 @@
package edu.ucdavis.dss.ipa.utilities;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.AmazonS3Exception;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.S3Object;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.HeadObjectRequest;
import software.amazon.awssdk.services.s3.model.HeadObjectResponse;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.S3Exception;

@Service
public class S3Service {
private final AmazonS3 s3client;
private final S3Client s3;
@Value("${AWS_S3_BUCKET}")
String s3Bucket;
String bucketName;

public S3Service(AmazonS3 s3client) {
this.s3client = s3client;
public S3Service(S3Client s3) {
this.s3 = s3;
}

public void upload(String filename, byte[] bytes) {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType("application/vnd.openxmlformats-officedocumsent.spreadsheetml.sheet");
metadata.setContentLength(Long.valueOf(bytes.length));
PutObjectRequest objectRequest = PutObjectRequest.builder()
.bucket(bucketName)
.key(filename)
.contentType("application/vnd.openxmlformats-officedocumsent.spreadsheetml.sheet")
.contentLength((long) bytes.length)
.build();

ByteArrayInputStream is = new ByteArrayInputStream(bytes);

s3client.putObject(s3Bucket, filename, is, metadata);
s3.putObject(objectRequest, RequestBody.fromBytes(bytes));
}

public byte[] download(String filename) {
S3Object object = s3client.getObject(s3Bucket, filename);
GetObjectRequest getObjectRequest = GetObjectRequest.builder()
.bucket(bucketName)
.key(filename)
.build();

try {
return IOUtils.toByteArray(object.getObjectContent());
} catch (IOException e) {
return s3.getObjectAsBytes(getObjectRequest).asByteArray();
} catch (S3Exception e) {
e.printStackTrace();
}
return null;
}

public ObjectMetadata getMetadata(String filename) {
public HeadObjectResponse getMetadata(String filename) {
try {
ObjectMetadata omd = s3client.getObjectMetadata(s3Bucket, filename);
return omd;
} catch (AmazonS3Exception e) {
HeadObjectRequest headObjectRequest = HeadObjectRequest.builder().bucket(bucketName).key(filename).build();

return s3.headObject(headObjectRequest);
} catch (S3Exception e) {
// File not found
return null;
}
}

@Async
public void delete(String filename) {
s3client.deleteObject(s3Bucket, filename);
DeleteObjectRequest deleteObjectRequest =
DeleteObjectRequest.builder().bucket(bucketName).key(filename).build();

s3.deleteObject(deleteObjectRequest);
}
}

0 comments on commit 02d9717

Please sign in to comment.