diff --git a/build.gradle b/build.gradle index 472be3b5..1f87dd43 100644 --- a/build.gradle +++ b/build.gradle @@ -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") @@ -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") diff --git a/src/main/java/edu/ucdavis/dss/ipa/api/components/workloadSummaryReport/WorkloadSummaryReportController.java b/src/main/java/edu/ucdavis/dss/ipa/api/components/workloadSummaryReport/WorkloadSummaryReportController.java index d87c3845..3ba1099a 100644 --- a/src/main/java/edu/ucdavis/dss/ipa/api/components/workloadSummaryReport/WorkloadSummaryReportController.java +++ b/src/main/java/edu/ucdavis/dss/ipa/api/components/workloadSummaryReport/WorkloadSummaryReportController.java @@ -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 { @@ -156,7 +157,7 @@ public ResponseEntity generateMultipleDepartments(@PathVariable long workgroupId @RequestBody Optional>> 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]); @@ -281,21 +282,21 @@ public Map> getDownloadStatus(@PathVariable long year) Map> 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 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 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); } diff --git a/src/main/java/edu/ucdavis/dss/ipa/config/AWSConfiguration.java b/src/main/java/edu/ucdavis/dss/ipa/config/AWSConfiguration.java index b7060888..a5a3030c 100644 --- a/src/main/java/edu/ucdavis/dss/ipa/config/AWSConfiguration.java +++ b/src/main/java/edu/ucdavis/dss/ipa/config/AWSConfiguration.java @@ -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 { @@ -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(); } } diff --git a/src/main/java/edu/ucdavis/dss/ipa/utilities/S3Service.java b/src/main/java/edu/ucdavis/dss/ipa/utilities/S3Service.java index 42975810..ad29d98b 100644 --- a/src/main/java/edu/ucdavis/dss/ipa/utilities/S3Service.java +++ b/src/main/java/edu/ucdavis/dss/ipa/utilities/S3Service.java @@ -1,51 +1,58 @@ 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; } @@ -53,6 +60,9 @@ public ObjectMetadata getMetadata(String filename) { @Async public void delete(String filename) { - s3client.deleteObject(s3Bucket, filename); + DeleteObjectRequest deleteObjectRequest = + DeleteObjectRequest.builder().bucket(bucketName).key(filename).build(); + + s3.deleteObject(deleteObjectRequest); } }