storagehub/src/main/java/org/gcube/data/access/storagehub/storage/backend/impl/S3Backend.java

143 lines
4.5 KiB
Java
Raw Normal View History

2021-11-26 17:49:35 +01:00
package org.gcube.data.access.storagehub.storage.backend.impl;
import java.io.InputStream;
2021-12-03 16:55:54 +01:00
import java.util.HashMap;
2021-11-26 17:49:35 +01:00
import java.util.Map;
import java.util.function.Function;
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
import org.gcube.common.storagehub.model.items.nodes.Content;
2021-12-03 16:55:54 +01:00
import org.gcube.common.storagehub.model.items.nodes.PayloadBackend;
2021-11-26 17:49:35 +01:00
import org.gcube.common.storagehub.model.storages.MetaInfo;
import org.gcube.common.storagehub.model.storages.StorageBackend;
2021-12-03 16:55:54 +01:00
import io.minio.CopyObjectArgs;
import io.minio.CopySource;
import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import io.minio.StatObjectArgs;
import io.minio.StatObjectResponse;
public class S3Backend extends StorageBackend{
2021-11-26 17:49:35 +01:00
Function<Void, String> keyGenerator;
String bucketName;
2021-12-03 16:55:54 +01:00
MinioClient client;
private static final long PART_SIZE = 100000000;
2021-11-26 17:49:35 +01:00
2021-12-03 16:55:54 +01:00
public S3Backend(PayloadBackend payloadConfiguration, Function<Void, String> keyGenerator) {
super(payloadConfiguration);
2021-11-26 17:49:35 +01:00
this.keyGenerator = keyGenerator;
2021-12-03 16:55:54 +01:00
Map<String, Object> parameters = payloadConfiguration.getParameters();
2021-11-26 17:49:35 +01:00
this.bucketName = (String)parameters.get("bucketName");
2021-12-03 16:55:54 +01:00
String accessKey = (String)parameters.get("key");
String secret = (String)parameters.get("secret");
String url = (String)parameters.get("url");
client =
MinioClient.builder()
.endpoint(url)
.credentials(accessKey, secret)
.build();
2021-11-26 17:49:35 +01:00
}
@Override
public MetaInfo onCopy(Content content, String newParentPath, String newName) {
String sourceKey = content.getStorageId();
String destinationKey = keyGenerator.apply(null);
try {
2021-12-03 16:55:54 +01:00
CopySource source = CopySource.builder().bucket(bucketName).object(sourceKey).build();
2021-11-26 17:49:35 +01:00
// Copy the object into a new object in the same bucket.
2021-12-03 16:55:54 +01:00
CopyObjectArgs copyObjRequest = CopyObjectArgs.builder().source(source).bucket(bucketName).object(destinationKey).build();
client.copyObject(copyObjRequest);
2021-11-26 17:49:35 +01:00
} catch (Exception e) {
2021-12-03 16:55:54 +01:00
throw new RuntimeException("error copying file on s3", e);
2021-11-26 17:49:35 +01:00
}
2021-12-03 16:55:54 +01:00
return new MetaInfo(content.getSize(), destinationKey, null, getPayloadConfiguration());
2021-11-26 17:49:35 +01:00
}
@Override
public MetaInfo onMove(Content content, String newParentPath) {
//new contentPath can be set as remotePath to the storage backend ?
2021-12-03 16:55:54 +01:00
return new MetaInfo(content.getSize(),content.getStorageId(), content.getRemotePath(), getPayloadConfiguration());
2021-11-26 17:49:35 +01:00
}
@Override
public void onDelete(Content content) {
2021-12-03 16:55:54 +01:00
try {
String storageId = content.getStorageId();
client.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(storageId).build());
} catch (Exception e) {
throw new RuntimeException("error deleting file on s3", e);
}
2021-11-26 17:49:35 +01:00
}
2021-12-03 16:55:54 +01:00
2021-11-26 17:49:35 +01:00
@Override
public MetaInfo upload(InputStream stream, String relativePath, String name) {
2021-12-03 16:55:54 +01:00
return this.upload(stream, relativePath, name, null);
}
@Override
public MetaInfo upload(InputStream stream, String relativePath, String name, Long size) {
2021-11-26 17:49:35 +01:00
try {
String storageId = keyGenerator.apply(null);
2021-12-03 16:55:54 +01:00
Map<String, String> headers = new HashMap<>();
headers.put("X-Amz-Storage-Class", "REDUCED_REDUNDANCY");
Map<String, String> userMetadata = new HashMap<>();
userMetadata.put("user", AuthorizationProvider.instance.get().getClient().getId());
client.putObject(
PutObjectArgs.builder().bucket(bucketName).object(storageId).stream(
stream, size == null || size<=0?-1:size, size == null || size<=0?PART_SIZE:-1)
.headers(headers).tags(userMetadata)
.build());
2021-11-26 17:49:35 +01:00
2021-12-03 16:55:54 +01:00
long fileSize;
if (size != null && size>0)
fileSize = size;
else {
StatObjectResponse resp = client.statObject(StatObjectArgs.builder().bucket(bucketName).object(storageId).build());
fileSize = resp.size();
}
2021-11-26 17:49:35 +01:00
2021-12-03 16:55:54 +01:00
return new MetaInfo(fileSize,storageId, null, getPayloadConfiguration());
2021-11-26 17:49:35 +01:00
} catch (Exception e) {
2021-12-03 16:55:54 +01:00
throw new RuntimeException("error uploading file on s3", e);
2021-11-26 17:49:35 +01:00
}
}
@Override
2021-12-03 16:55:54 +01:00
public InputStream download(Content content) {
2021-11-26 17:49:35 +01:00
try {
2021-12-03 16:55:54 +01:00
String storageId = content.getStorageId();
InputStream inputStream = client.getObject(GetObjectArgs.builder().bucket(bucketName).object(storageId).build());
2021-11-26 17:49:35 +01:00
return inputStream;
} catch (Exception e) {
2021-12-03 16:55:54 +01:00
throw new RuntimeException("error downloading file from s3",e);
2021-11-26 17:49:35 +01:00
}
}
@Override
public String getTotalSizeStored() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getTotalItemsCount() {
// TODO Auto-generated method stub
return null;
}
}