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

140 lines
4.2 KiB
Java

package org.gcube.data.access.storagehub.storage.backend.impl;
import java.io.InputStream;
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.storages.MetaInfo;
import org.gcube.common.storagehub.model.storages.StorageBackend;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.CopyObjectRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectResult;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectInputStream;
public class S3Backend implements StorageBackend{
Map<String, Object> parameters;
Function<Void, String> keyGenerator;
AWSCredentials credentials = new BasicAWSCredentials("user", "password");
final static Regions clientRegion = Regions.DEFAULT_REGION;
String bucketName;
public S3Backend(Map<String, Object> parameters, Function<Void, String> keyGenerator) {
super();
this.parameters = parameters;
this.keyGenerator = keyGenerator;
this.bucketName = (String)parameters.get("bucketName");
}
@Override
public String getName() {
return "s3";
}
@Override
public MetaInfo onCopy(Content content, String newParentPath, String newName) {
String sourceKey = content.getStorageId();
String destinationKey = keyGenerator.apply(null);
try {
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(clientRegion)
.build();
// Copy the object into a new object in the same bucket.
CopyObjectRequest copyObjRequest = new CopyObjectRequest(bucketName, sourceKey, bucketName, destinationKey);
s3Client.copyObject(copyObjRequest);
} catch (Exception e) {
throw new RuntimeException("error copying file on s3");
}
return new MetaInfo(content.getSize(), destinationKey, null, getName());
}
@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(), getName());
}
@Override
public void onDelete(Content content) {
// TODO Auto-generated method stub
}
@Override
public MetaInfo upload(InputStream stream, String relativePath, String name) {
try {
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(clientRegion)
.build();
String storageId = keyGenerator.apply(null);
ObjectMetadata metadata = new ObjectMetadata();
metadata.addUserMetadata("user", AuthorizationProvider.instance.get().getClient().getId());
PutObjectResult result = s3Client.putObject(
bucketName,
keyGenerator.apply(null),
stream,
metadata
);
long size = result.getMetadata().getContentLength();
s3Client.getObjectMetadata(bucketName, );
return new MetaInfo(content.getSize(),storageId, null, getName());
} catch (Exception e) {
throw new RuntimeException("error copying file on s3");
}
}
@Override
public InputStream download(Content item) {
try {
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(clientRegion)
.build();
S3Object s3object = s3Client.getObject(bucketName, item.getStorageId());
S3ObjectInputStream inputStream = s3object.getObjectContent();
return inputStream;
} catch (Exception e) {
throw new RuntimeException("error copying file on s3");
}
}
@Override
public String getTotalSizeStored() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getTotalItemsCount() {
// TODO Auto-generated method stub
return null;
}
}