use of software.amazon.awssdk.services.s3.model.CopyObjectRequest in project jackrabbit by apache.
the class S3Backend method touch.
@Override
public void touch(DataIdentifier identifier, long minModifiedDate) throws DataStoreException {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
try {
final long start = System.currentTimeMillis();
final String key = getKeyName(identifier);
if (minModifiedDate > 0 && minModifiedDate > getLastModified(identifier)) {
CopyObjectRequest copReq = new CopyObjectRequest(bucket, key, bucket, key);
copReq.setNewObjectMetadata(new ObjectMetadata());
Copy copy = tmx.copy(s3ReqDecorator.decorate(copReq));
copy.waitForCompletion();
LOG.debug("[{}] touched. time taken [{}] ms ", new Object[] { identifier, (System.currentTimeMillis() - start) });
} else {
LOG.trace("[{}] touch not required. time taken [{}] ms ", new Object[] { identifier, (System.currentTimeMillis() - start) });
}
} catch (Exception e) {
throw new DataStoreException("Error occured in touching key [" + identifier.toString() + "]", e);
} finally {
if (contextClassLoader != null) {
Thread.currentThread().setContextClassLoader(contextClassLoader);
}
}
}
use of software.amazon.awssdk.services.s3.model.CopyObjectRequest in project jackrabbit by apache.
the class S3Backend method exists.
@Override
public boolean exists(DataIdentifier identifier, boolean touch) throws DataStoreException {
long start = System.currentTimeMillis();
String key = getKeyName(identifier);
ObjectMetadata objectMetaData = null;
boolean retVal = false;
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
objectMetaData = s3service.getObjectMetadata(bucket, key);
if (objectMetaData != null) {
retVal = true;
if (touch) {
CopyObjectRequest copReq = new CopyObjectRequest(bucket, key, bucket, key);
copReq.setNewObjectMetadata(objectMetaData);
Copy copy = tmx.copy(s3ReqDecorator.decorate(copReq));
copy.waitForCopyResult();
LOG.debug("[{}] touched took [{}] ms. ", identifier, (System.currentTimeMillis() - start));
}
} else {
retVal = false;
}
} catch (AmazonServiceException e) {
if (e.getStatusCode() == 404 || e.getStatusCode() == 403) {
retVal = false;
} else {
throw new DataStoreException("Error occured to find exists for key [" + identifier.toString() + "]", e);
}
} catch (Exception e) {
throw new DataStoreException("Error occured to find exists for key " + identifier.toString(), e);
} finally {
if (contextClassLoader != null) {
Thread.currentThread().setContextClassLoader(contextClassLoader);
}
}
LOG.debug("exists [{}]: [{}] took [{}] ms.", new Object[] { identifier, retVal, (System.currentTimeMillis() - start) });
return retVal;
}
use of software.amazon.awssdk.services.s3.model.CopyObjectRequest in project spring-integration-aws by spring-projects.
the class S3MessageHandler method copy.
private Transfer copy(Message<?> requestMessage) {
String sourceBucketName = obtainBucket(requestMessage);
String sourceKey = null;
if (this.keyExpression != null) {
sourceKey = this.keyExpression.getValue(this.evaluationContext, requestMessage, String.class);
}
Assert.state(sourceKey != null, "The 'keyExpression' must not be null for 'copy' operation " + "and 'keyExpression' can't evaluate to null. " + "Root object is: " + requestMessage);
String destinationBucketName = null;
if (this.destinationBucketExpression != null) {
destinationBucketName = this.destinationBucketExpression.getValue(this.evaluationContext, requestMessage, String.class);
}
if (this.resourceIdResolver != null) {
destinationBucketName = this.resourceIdResolver.resolveToPhysicalResourceId(destinationBucketName);
}
Assert.state(destinationBucketName != null, "The 'destinationBucketExpression' must not be null for 'copy' operation and can't evaluate to null. " + "Root object is: " + requestMessage);
String destinationKey = null;
if (this.destinationKeyExpression != null) {
destinationKey = this.destinationKeyExpression.getValue(this.evaluationContext, requestMessage, String.class);
}
Assert.state(destinationKey != null, "The 'destinationKeyExpression' must not be null for 'copy' operation and can't evaluate to null. " + "Root object is: " + requestMessage);
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(sourceBucketName, sourceKey, destinationBucketName, destinationKey);
return this.transferManager.copy(copyObjectRequest);
}
use of software.amazon.awssdk.services.s3.model.CopyObjectRequest in project spring-integration-aws by spring-projects.
the class S3Session method rename.
@Override
public void rename(String pathFrom, String pathTo) throws IOException {
String[] bucketKeyFrom = splitPathToBucketAndKey(pathFrom, true);
String[] bucketKeyTo = splitPathToBucketAndKey(pathTo, true);
CopyObjectRequest copyRequest = new CopyObjectRequest(bucketKeyFrom[0], bucketKeyFrom[1], bucketKeyTo[0], bucketKeyTo[1]);
this.amazonS3.copyObject(copyRequest);
// Delete the source
this.amazonS3.deleteObject(bucketKeyFrom[0], bucketKeyFrom[1]);
}
use of software.amazon.awssdk.services.s3.model.CopyObjectRequest in project druid by druid-io.
the class S3DataSegmentMover method selfCheckingMove.
/**
* Copies an object and after that checks that the object is present at the target location, via a separate API call.
* If it is not, an exception is thrown, and the object is not deleted at the old location. This "paranoic" check
* is added after it was observed that S3 may report a successful move, and the object is not found at the target
* location.
*/
private void selfCheckingMove(String s3Bucket, String targetS3Bucket, String s3Path, String targetS3Path, String copyMsg) throws IOException, SegmentLoadingException {
if (s3Bucket.equals(targetS3Bucket) && s3Path.equals(targetS3Path)) {
log.info("No need to move file[s3://%s/%s] onto itself", s3Bucket, s3Path);
return;
}
final ServerSideEncryptingAmazonS3 s3Client = this.s3ClientSupplier.get();
if (s3Client.doesObjectExist(s3Bucket, s3Path)) {
final ListObjectsV2Result listResult = s3Client.listObjectsV2(new ListObjectsV2Request().withBucketName(s3Bucket).withPrefix(s3Path).withMaxKeys(1));
// keyCount is still zero.
if (listResult.getObjectSummaries().size() == 0) {
// should never happen
throw new ISE("Unable to list object [s3://%s/%s]", s3Bucket, s3Path);
}
final S3ObjectSummary objectSummary = listResult.getObjectSummaries().get(0);
if (objectSummary.getStorageClass() != null && StorageClass.fromValue(StringUtils.toUpperCase(objectSummary.getStorageClass())).equals(StorageClass.Glacier)) {
throw new AmazonServiceException(StringUtils.format("Cannot move file[s3://%s/%s] of storage class glacier, skipping.", s3Bucket, s3Path));
} else {
log.info("Moving file %s", copyMsg);
final CopyObjectRequest copyRequest = new CopyObjectRequest(s3Bucket, s3Path, targetS3Bucket, targetS3Path);
if (!config.getDisableAcl()) {
copyRequest.setAccessControlList(S3Utils.grantFullControlToBucketOwner(s3Client, targetS3Bucket));
}
s3Client.copyObject(copyRequest);
if (!s3Client.doesObjectExist(targetS3Bucket, targetS3Path)) {
throw new IOE("After copy was reported as successful the file doesn't exist in the target location [%s]", copyMsg);
}
deleteWithRetriesSilent(s3Bucket, s3Path);
log.debug("Finished moving file %s", copyMsg);
}
} else {
// ensure object exists in target location
if (s3Client.doesObjectExist(targetS3Bucket, targetS3Path)) {
log.info("Not moving file [s3://%s/%s], already present in target location [s3://%s/%s]", s3Bucket, s3Path, targetS3Bucket, targetS3Path);
} else {
throw new SegmentLoadingException("Unable to move file %s, not present in either source or target location", copyMsg);
}
}
}
Aggregations