Skip to content

Commit

Permalink
wip: S3 api
Browse files Browse the repository at this point in the history
  • Loading branch information
lmanelphe committed Feb 9, 2024
1 parent a28a902 commit 01bc923
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
22 changes: 22 additions & 0 deletions arc-core/src/main/java/fr/insee/arc/core/service/s3/ArcS3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package fr.insee.arc.core.service.s3;

import fr.insee.arc.utils.minio.S3Template;
import fr.insee.arc.utils.ressourceUtils.PropertiesHandler;

public class ArcS3 {

public static final S3Template INPUT_BUCKET = new S3Template( //
PropertiesHandler.getInstance().getS3InputApiUri(), //
PropertiesHandler.getInstance().getS3InputBucket(), //
PropertiesHandler.getInstance().getS3InputAccess(), //
PropertiesHandler.getInstance().getS3InputSecret() //
);

public static final S3Template OUTPUT_BUCKET = new S3Template( //
PropertiesHandler.getInstance().getS3OutputApiUri(), //
PropertiesHandler.getInstance().getS3OutputBucket(), //
PropertiesHandler.getInstance().getS3OutputAccess(), //
PropertiesHandler.getInstance().getS3OutputSecret() //
);

}
77 changes: 77 additions & 0 deletions arc-utils/src/main/java/fr/insee/arc/utils/minio/S3Template.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package fr.insee.arc.utils.minio;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

import fr.insee.arc.utils.exception.ArcException;
import fr.insee.arc.utils.exception.ArcExceptionMessage;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.InvalidResponseException;
import io.minio.errors.ServerException;
import io.minio.errors.XmlParserException;

public class S3Template {

public S3Template(String s3ApiUri, String bucket, String accessKey, String secretKey) {
super();
this.s3ApiUri = s3ApiUri;
this.bucket = bucket;
this.accessKey = accessKey;
this.secretKey = secretKey;
}

private String s3ApiUri;
private String bucket;
private String accessKey;
private String secretKey;

private MinioClient minioClient;


public MinioClient getMinioClient() {
if (this.minioClient == null) {
try {
buildMinioClient();
} catch (KeyManagementException | NoSuchAlgorithmException e) {
e.printStackTrace();
}
}

return minioClient;
}

private void buildMinioClient() throws KeyManagementException, NoSuchAlgorithmException {


this.minioClient = MinioClient.builder().endpoint(s3ApiUri).credentials(accessKey, secretKey).build();
this.minioClient.ignoreCertCheck();
}

/**
* Crée un nouveau répertoire dans le bucket à l'emplacement donné
*
* @param path le chemin du répertoire à créer (emplacement et nom)
* @throws ArcException
*/
public void createDirectory(String path) throws ArcException {
try {
getMinioClient()
.putObject(PutObjectArgs.builder().bucket(bucket).object(path + (path.endsWith("/") ? "" : "/"))
.stream(new ByteArrayInputStream(new byte[] {}), 0, -1).build());
} catch (InvalidKeyException | ErrorResponseException | InsufficientDataException | InternalException
| InvalidResponseException | NoSuchAlgorithmException | ServerException | XmlParserException
| IllegalArgumentException | IOException e) {

throw new ArcException(ArcExceptionMessage.FILE_WRITE_FAILED, path);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.List;
import java.util.Map;

import org.checkerframework.common.returnsreceiver.qual.This;
import org.springframework.stereotype.Service;

import fr.insee.arc.utils.exception.ArcException;
Expand Down

0 comments on commit 01bc923

Please sign in to comment.