diff --git a/src/backend/microservices/shared/src/main/java/org/phyloviz/pwp/shared/repository/data/S3FileRepository.java b/src/backend/microservices/shared/src/main/java/org/phyloviz/pwp/shared/repository/data/S3FileRepository.java index 222f0b11..eaff9d62 100644 --- a/src/backend/microservices/shared/src/main/java/org/phyloviz/pwp/shared/repository/data/S3FileRepository.java +++ b/src/backend/microservices/shared/src/main/java/org/phyloviz/pwp/shared/repository/data/S3FileRepository.java @@ -28,11 +28,4 @@ public interface S3FileRepository { * @return true if the file was deleted successfully, false otherwise */ boolean delete(String url); - - /** - * Get location of the repository. - * - * @return location of the repository - */ - String getLocation(); } diff --git a/src/backend/microservices/shared/src/main/java/org/phyloviz/pwp/shared/repository/data/S3FileRepositoryImpl.java b/src/backend/microservices/shared/src/main/java/org/phyloviz/pwp/shared/repository/data/S3FileRepositoryImpl.java index 5416e35d..e5ad8c7f 100644 --- a/src/backend/microservices/shared/src/main/java/org/phyloviz/pwp/shared/repository/data/S3FileRepositoryImpl.java +++ b/src/backend/microservices/shared/src/main/java/org/phyloviz/pwp/shared/repository/data/S3FileRepositoryImpl.java @@ -6,7 +6,6 @@ import org.springframework.stereotype.Repository; import org.springframework.web.multipart.MultipartFile; import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; -import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; import software.amazon.awssdk.core.BytesWrapper; import software.amazon.awssdk.core.async.AsyncRequestBody; import software.amazon.awssdk.core.async.AsyncResponseTransformer; @@ -28,36 +27,34 @@ @Repository public class S3FileRepositoryImpl implements S3FileRepository { - public static final Region REGION = Region.of("eu-west-3"); private final String bucketName; - private final String objectStorageEndpoint; private final S3AsyncClient s3Client; private final S3TransferManager transferManager; private final ExecutorService executorService = Executors.newCachedThreadPool(); public S3FileRepositoryImpl( @Value("${s3.endpoint}") - String objectStorageEndpoint, + String s3endpoint, @Value("${s3.access-key-id}") String accessKeyId, @Value("${s3.secret-access-key}") String secretAccessKey, @Value("${s3.bucket}") - String bucketName + String bucketName, + @Value("${s3.region}") + String region ) { - AwsBasicCredentials awsCredentials = AwsBasicCredentials.create(accessKeyId, secretAccessKey); - S3AsyncClient newS3Client = S3AsyncClient.builder() - .credentialsProvider(StaticCredentialsProvider.create(awsCredentials)) - .endpointOverride(URI.create(objectStorageEndpoint)) - .region(REGION) + .forcePathStyle(true) + .endpointOverride(URI.create(s3endpoint)) + .credentialsProvider(() -> AwsBasicCredentials.create(accessKeyId, secretAccessKey)) + .region(Region.of(region)) .build(); newS3Client.createBucket(r -> r.bucket(bucketName)); this.s3Client = newS3Client; this.bucketName = bucketName; - this.objectStorageEndpoint = objectStorageEndpoint; this.transferManager = S3TransferManager.builder().s3Client(newS3Client).build(); } @@ -81,15 +78,14 @@ public boolean upload(String url, MultipartFile multipartFile) { upload.completionFuture().join(); + // s3Client.listBuckets().join() -> lists all buckets, currently only one with name "phyloviz-web-platform" + return true; } @Override public String download(String url) { - if (!url.startsWith(getLocation())) - throw new IllegalArgumentException("URL does not start with the object storage endpoint"); - - String key = url.substring(getLocation().length()); + String key = url; /*s3Client.listObjectsV2(ListObjectsV2Request.builder().bucket(bucketName).build()) .thenAccept(response -> { @@ -131,21 +127,13 @@ public String download(String url) { @Override public boolean delete(String url) { - if (!url.startsWith(getLocation())) - throw new IllegalArgumentException("URL does not start with the object storage endpoint"); - DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder() .bucket(bucketName) - .key(url.substring(getLocation().length())) + .key(url) .build(); s3Client.deleteObject(deleteObjectRequest); // TODO throw exception if not successful return true; } - - @Override - public String getLocation() { - return objectStorageEndpoint + "/" + bucketName.substring(0, bucketName.length() - 1); - } } diff --git a/src/backend/microservices/shared/src/main/java/org/phyloviz/pwp/shared/repository/data/isolate_data/repository/IsolateDataS3DataRepository.java b/src/backend/microservices/shared/src/main/java/org/phyloviz/pwp/shared/repository/data/isolate_data/repository/IsolateDataS3DataRepository.java index 3a1a521c..d6105eab 100644 --- a/src/backend/microservices/shared/src/main/java/org/phyloviz/pwp/shared/repository/data/isolate_data/repository/IsolateDataS3DataRepository.java +++ b/src/backend/microservices/shared/src/main/java/org/phyloviz/pwp/shared/repository/data/isolate_data/repository/IsolateDataS3DataRepository.java @@ -20,7 +20,7 @@ public IsolateDataDataRepositorySpecificData uploadIsolateData(String projectId, s3FileRepository.upload(url, multipartFile); - return new IsolateDataS3DataRepositorySpecificData(s3FileRepository.getLocation() + "/" + url, multipartFile.getOriginalFilename()); + return new IsolateDataS3DataRepositorySpecificData(url, multipartFile.getOriginalFilename()); } @Override diff --git a/src/backend/microservices/shared/src/main/java/org/phyloviz/pwp/shared/repository/data/typing_data/repository/TypingDataS3DataRepository.java b/src/backend/microservices/shared/src/main/java/org/phyloviz/pwp/shared/repository/data/typing_data/repository/TypingDataS3DataRepository.java index ce802019..2b6a566f 100644 --- a/src/backend/microservices/shared/src/main/java/org/phyloviz/pwp/shared/repository/data/typing_data/repository/TypingDataS3DataRepository.java +++ b/src/backend/microservices/shared/src/main/java/org/phyloviz/pwp/shared/repository/data/typing_data/repository/TypingDataS3DataRepository.java @@ -21,7 +21,7 @@ public TypingDataDataRepositorySpecificData uploadTypingData(String projectId, S s3FileRepository.upload(url, multipartFile); - return new TypingDataS3DataRepositorySpecificData(s3FileRepository.getLocation() + "/" + url, multipartFile.getOriginalFilename()); + return new TypingDataS3DataRepositorySpecificData(url, multipartFile.getOriginalFilename()); } @Override