added facility for copy operation.
fix issue on delete operation
This commit is contained in:
parent
64d3f995b7
commit
a63c5e1874
|
@ -2,16 +2,22 @@ package org.gcube.contentmanagement.blobstorage.transport.plugin;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import software.amazon.awssdk.core.sync.RequestBody;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.s3.model.DeleteObjectResponse;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.internal.resource.S3ObjectResource;
|
||||
import software.amazon.awssdk.services.s3.model.CopyObjectRequest;
|
||||
import software.amazon.awssdk.services.s3.model.CopyObjectResponse;
|
||||
import software.amazon.awssdk.services.s3.model.CreateBucketConfiguration;
|
||||
import software.amazon.awssdk.services.s3.model.CreateBucketRequest;
|
||||
import software.amazon.awssdk.services.s3.model.DeleteBucketRequest;
|
||||
|
@ -30,6 +36,7 @@ public class BucketOperator {
|
|||
|
||||
private static BucketOperator single_instance;
|
||||
private static S3Client client;
|
||||
private Logger logger = LoggerFactory.getLogger(BucketOperator.class);
|
||||
|
||||
private BucketOperator(S3Client client) {
|
||||
if(Objects.isNull(client))
|
||||
|
@ -68,7 +75,7 @@ public class BucketOperator {
|
|||
}
|
||||
|
||||
/**
|
||||
* Before you can delete an Amazon S3 bucket, you must ensure that the bucket
|
||||
* Before you can delete a S3 bucket, you must ensure that the bucket
|
||||
* is empty or the service will return an error. If you have a versioned
|
||||
* bucket, you must also delete any versioned objects that are in the bucket.
|
||||
*
|
||||
|
@ -109,6 +116,24 @@ public class BucketOperator {
|
|||
System.out.println(s3.deleteBucket(req).toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy an object from a bucket to another. If the destination bucket is the same, the key will be copied in the same bucket.
|
||||
* @param s3
|
||||
* @param objectKey
|
||||
* @param fromBucket
|
||||
* @param toBucket
|
||||
*/
|
||||
public static String copyObject(final S3Client s3, String sourceKey, String fromBucket, String destKey, String toBucket) {
|
||||
String encodedUrl = null;
|
||||
try {
|
||||
encodedUrl = URLEncoder.encode(fromBucket + "/" + sourceKey, StandardCharsets.UTF_8.toString());
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
System.out.println("URL could not be encoded: " + e.getMessage());
|
||||
}
|
||||
CopyObjectResponse response = s3.copyObject(CopyObjectRequest.builder().copySource(encodedUrl).destinationBucket(toBucket).destinationKey(destKey).build());
|
||||
return response.copyObjectResult().toString();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param bucket
|
||||
|
|
|
@ -98,7 +98,7 @@ public class S3PluginManager extends TransportManager {
|
|||
|
||||
@Override
|
||||
public void removeRemoteFile(String bucket, MyFile resource) throws UnknownHostException {
|
||||
BucketOperator.getInstance(s3).deleteObject(bucket, Utils.convertToS3Format(resource.getRemotePath()));
|
||||
BucketOperator.getInstance(s3).deleteObject(this.bucket, Utils.convertToS3Format(resource.getRemotePath()));
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue