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

143 lines
4.5 KiB
Java

package org.gcube.data.access.storagehub.storage.backend.impl;
import java.io.InputStream;
import java.util.HashMap;
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;
import org.gcube.common.storagehub.model.items.nodes.PayloadBackend;
import org.gcube.common.storagehub.model.storages.MetaInfo;
import org.gcube.common.storagehub.model.storages.StorageBackend;
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{
Function<Void, String> keyGenerator;
String bucketName;
MinioClient client;
private static final long PART_SIZE = 100000000;
public S3Backend(PayloadBackend payloadConfiguration, Function<Void, String> keyGenerator) {
super(payloadConfiguration);
this.keyGenerator = keyGenerator;
Map<String, Object> parameters = payloadConfiguration.getParameters();
this.bucketName = (String)parameters.get("bucketName");
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();
}
@Override
public MetaInfo onCopy(Content content, String newParentPath, String newName) {
String sourceKey = content.getStorageId();
String destinationKey = keyGenerator.apply(null);
try {
CopySource source = CopySource.builder().bucket(bucketName).object(sourceKey).build();
// Copy the object into a new object in the same bucket.
CopyObjectArgs copyObjRequest = CopyObjectArgs.builder().source(source).bucket(bucketName).object(destinationKey).build();
client.copyObject(copyObjRequest);
} catch (Exception e) {
throw new RuntimeException("error copying file on s3", e);
}
return new MetaInfo(content.getSize(), destinationKey, null, getPayloadConfiguration());
}
@Override
public MetaInfo onMove(Content content, String newParentPath) {
//new contentPath can be set as remotePath to the storage backend ?
return new MetaInfo(content.getSize(),content.getStorageId(), content.getRemotePath(), getPayloadConfiguration());
}
@Override
public void onDelete(Content content) {
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);
}
}
@Override
public MetaInfo upload(InputStream stream, String relativePath, String name) {
return this.upload(stream, relativePath, name, null);
}
@Override
public MetaInfo upload(InputStream stream, String relativePath, String name, Long size) {
try {
String storageId = keyGenerator.apply(null);
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());
long fileSize;
if (size != null && size>0)
fileSize = size;
else {
StatObjectResponse resp = client.statObject(StatObjectArgs.builder().bucket(bucketName).object(storageId).build());
fileSize = resp.size();
}
return new MetaInfo(fileSize,storageId, null, getPayloadConfiguration());
} catch (Exception e) {
throw new RuntimeException("error uploading file on s3", e);
}
}
@Override
public InputStream download(Content content) {
try {
String storageId = content.getStorageId();
InputStream inputStream = client.getObject(GetObjectArgs.builder().bucket(bucketName).object(storageId).build());
return inputStream;
} catch (Exception e) {
throw new RuntimeException("error downloading file from s3",e);
}
}
@Override
public String getTotalSizeStored() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getTotalItemsCount() {
// TODO Auto-generated method stub
return null;
}
}