Search in sources :

Example 21 with CopyObjectRequest

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);
        }
    }
}
Also used : CopyObjectRequest(com.amazonaws.services.s3.model.CopyObjectRequest) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) Copy(com.amazonaws.services.s3.transfer.Copy) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonClientException(com.amazonaws.AmazonClientException) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) IOException(java.io.IOException)

Example 22 with CopyObjectRequest

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;
}
Also used : CopyObjectRequest(com.amazonaws.services.s3.model.CopyObjectRequest) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) Copy(com.amazonaws.services.s3.transfer.Copy) AmazonServiceException(com.amazonaws.AmazonServiceException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonClientException(com.amazonaws.AmazonClientException) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) IOException(java.io.IOException)

Example 23 with CopyObjectRequest

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);
}
Also used : CopyObjectRequest(com.amazonaws.services.s3.model.CopyObjectRequest)

Example 24 with 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]);
}
Also used : CopyObjectRequest(com.amazonaws.services.s3.model.CopyObjectRequest)

Example 25 with CopyObjectRequest

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);
        }
    }
}
Also used : CopyObjectRequest(com.amazonaws.services.s3.model.CopyObjectRequest) ListObjectsV2Result(com.amazonaws.services.s3.model.ListObjectsV2Result) ListObjectsV2Request(com.amazonaws.services.s3.model.ListObjectsV2Request) SegmentLoadingException(org.apache.druid.segment.loading.SegmentLoadingException) AmazonServiceException(com.amazonaws.AmazonServiceException) ISE(org.apache.druid.java.util.common.ISE) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) IOE(org.apache.druid.java.util.common.IOE)

Aggregations

CopyObjectRequest (com.amazonaws.services.s3.model.CopyObjectRequest)22 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)14 AmazonClientException (com.amazonaws.AmazonClientException)11 AmazonServiceException (com.amazonaws.AmazonServiceException)10 Copy (com.amazonaws.services.s3.transfer.Copy)10 IOException (java.io.IOException)9 DataStoreException (org.apache.jackrabbit.core.data.DataStoreException)7 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)4 Upload (com.amazonaws.services.s3.transfer.Upload)4 InterruptedIOException (java.io.InterruptedIOException)3 CopyObjectRequest (software.amazon.awssdk.services.s3.model.CopyObjectRequest)3 SdkClientException (com.amazonaws.SdkClientException)2 ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)2 ProgressEvent (com.amazonaws.event.ProgressEvent)2 ProgressListener (com.amazonaws.event.ProgressListener)2 Regions (com.amazonaws.regions.Regions)2 AmazonS3 (com.amazonaws.services.s3.AmazonS3)2 CopyObjectResult (com.amazonaws.services.s3.model.CopyObjectResult)2 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)2 TransferManager (com.amazonaws.services.s3.transfer.TransferManager)2