2021-12-03 16:55:54 +01:00
|
|
|
package org.gcube.data.access.storagehub.storage.backend.impl;
|
|
|
|
|
2022-03-28 18:27:18 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2021-12-03 16:55:54 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2022-03-28 18:27:18 +02:00
|
|
|
import java.util.Properties;
|
2021-12-03 16:55:54 +01:00
|
|
|
import java.util.UUID;
|
|
|
|
|
2024-03-15 14:26:05 +01:00
|
|
|
import jakarta.annotation.PostConstruct;
|
|
|
|
import jakarta.inject.Singleton;
|
2021-12-03 16:55:54 +01:00
|
|
|
|
|
|
|
import org.gcube.common.storagehub.model.Metadata;
|
|
|
|
import org.gcube.common.storagehub.model.exceptions.InvalidCallParameters;
|
|
|
|
import org.gcube.common.storagehub.model.items.nodes.PayloadBackend;
|
|
|
|
import org.gcube.common.storagehub.model.storages.StorageBackend;
|
|
|
|
import org.gcube.common.storagehub.model.storages.StorageBackendFactory;
|
|
|
|
|
|
|
|
@Singleton
|
2023-06-01 19:00:42 +02:00
|
|
|
public class GcubeMinIOStorageBackendFactory implements StorageBackendFactory {
|
|
|
|
|
2024-01-19 12:20:19 +01:00
|
|
|
private static final String PROP_PREFIX = "default.";
|
2021-12-03 16:55:54 +01:00
|
|
|
|
2023-06-01 19:00:42 +02:00
|
|
|
private Metadata baseParameters;
|
2022-09-01 17:15:03 +02:00
|
|
|
|
2021-12-03 16:55:54 +01:00
|
|
|
@PostConstruct
|
|
|
|
public void init(){
|
2023-06-01 19:00:42 +02:00
|
|
|
baseParameters = getParameters();
|
2021-12-03 16:55:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getName() {
|
2024-01-19 12:20:19 +01:00
|
|
|
return "gcube-default-minio";
|
2023-06-01 19:00:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isSystemStorage() {
|
|
|
|
return true;
|
2021-12-03 16:55:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public StorageBackend create(PayloadBackend payloadConfiguration) throws InvalidCallParameters {
|
2022-03-28 18:27:18 +02:00
|
|
|
if (payloadConfiguration.getParameters().isEmpty())
|
2023-06-01 19:00:42 +02:00
|
|
|
throw new InvalidCallParameters(getName()+": null or empty parameter not allowed");
|
|
|
|
String bucketName = (String)payloadConfiguration.getParameters().get("bucketName");
|
|
|
|
if (bucketName == null || bucketName.isBlank())
|
|
|
|
throw new InvalidCallParameters(getName()+": 'bucketName' cannot be blank or empty");
|
2022-03-28 18:27:18 +02:00
|
|
|
|
2023-06-01 19:00:42 +02:00
|
|
|
Metadata metadata = new Metadata(new HashMap<>(baseParameters.getMap()));
|
|
|
|
metadata.getMap().putAll(payloadConfiguration.getParameters());
|
|
|
|
return new S3Backend(new PayloadBackend(getName(), metadata), (String) -> UUID.randomUUID().toString());
|
2021-12-03 16:55:54 +01:00
|
|
|
}
|
|
|
|
|
2022-03-28 18:27:18 +02:00
|
|
|
private Metadata getParameters(){
|
2024-01-19 12:20:19 +01:00
|
|
|
try (InputStream input = GcubeMinIOStorageBackendFactory.class.getClassLoader().getResourceAsStream("storage-settings.properties")) {
|
2022-03-28 18:27:18 +02:00
|
|
|
|
|
|
|
Properties prop = new Properties();
|
|
|
|
|
|
|
|
prop.load(input);
|
|
|
|
|
|
|
|
Map<String, Object> params = new HashMap<String, Object>();
|
2022-09-01 17:15:03 +02:00
|
|
|
|
2022-03-28 18:27:18 +02:00
|
|
|
|
2023-05-05 15:58:13 +02:00
|
|
|
prop.forEach((k,v) -> { if (k.toString().startsWith(PROP_PREFIX)) params.put(k.toString().replace(PROP_PREFIX, ""), v);});
|
|
|
|
|
|
|
|
return new Metadata(params);
|
2022-03-28 18:27:18 +02:00
|
|
|
|
|
|
|
} catch (IOException ex) {
|
|
|
|
throw new RuntimeException("error initializing MinIO", ex);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2021-12-03 16:55:54 +01:00
|
|
|
}
|