Skip to content

Commit

Permalink
fix: add changes to S3 and file upload.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyckoka committed Oct 21, 2024
1 parent f971003 commit df8200c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}

Expand All @@ -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 -> {
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit df8200c

Please sign in to comment.