diff --git a/arc-core/src/main/java/fr/insee/arc/core/service/s3/ArcS3.java b/arc-core/src/main/java/fr/insee/arc/core/service/s3/ArcS3.java new file mode 100644 index 000000000..5c763d998 --- /dev/null +++ b/arc-core/src/main/java/fr/insee/arc/core/service/s3/ArcS3.java @@ -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() // + ); + +} diff --git a/arc-utils/src/main/java/fr/insee/arc/utils/minio/S3Template.java b/arc-utils/src/main/java/fr/insee/arc/utils/minio/S3Template.java new file mode 100644 index 000000000..bb2f9693e --- /dev/null +++ b/arc-utils/src/main/java/fr/insee/arc/utils/minio/S3Template.java @@ -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); + } + } + + +} diff --git a/arc-utils/src/main/java/fr/insee/arc/utils/ressourceUtils/PropertiesHandler.java b/arc-utils/src/main/java/fr/insee/arc/utils/ressourceUtils/PropertiesHandler.java index 2d0dc980c..f868de260 100644 --- a/arc-utils/src/main/java/fr/insee/arc/utils/ressourceUtils/PropertiesHandler.java +++ b/arc-utils/src/main/java/fr/insee/arc/utils/ressourceUtils/PropertiesHandler.java @@ -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;