Search in sources :

Example 16 with CopyObjectRequest

use of software.amazon.awssdk.services.s3.model.CopyObjectRequest in project herd by FINRAOS.

the class S3DaoImpl method copyFile.

@Override
public S3FileTransferResultsDto copyFile(final S3FileCopyRequestParamsDto params) throws InterruptedException {
    LOGGER.info("Copying S3 object... sourceS3Key=\"{}\" sourceS3BucketName=\"{}\" targetS3Key=\"{}\" targetS3BucketName=\"{}\"", params.getSourceObjectKey(), params.getSourceBucketName(), params.getTargetObjectKey(), params.getTargetBucketName());
    // Perform the copy.
    S3FileTransferResultsDto results = performTransfer(params, new Transferer() {

        @Override
        public Transfer performTransfer(TransferManager transferManager) {
            // Create a copy request.
            CopyObjectRequest copyObjectRequest = new CopyObjectRequest(params.getSourceBucketName(), params.getSourceObjectKey(), params.getTargetBucketName(), params.getTargetObjectKey());
            // If KMS Key ID is specified, set the AWS Key Management System parameters to be used to encrypt the object.
            if (StringUtils.isNotBlank(params.getKmsKeyId())) {
                copyObjectRequest.withSSEAwsKeyManagementParams(new SSEAwsKeyManagementParams(params.getKmsKeyId()));
            } else // Otherwise, specify the server-side encryption algorithm for encrypting the object using AWS-managed keys.
            {
                ObjectMetadata metadata = new ObjectMetadata();
                metadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
                copyObjectRequest.setNewObjectMetadata(metadata);
            }
            return s3Operations.copyFile(copyObjectRequest, transferManager);
        }
    });
    LOGGER.info("Copied S3 object. sourceS3Key=\"{}\" sourceS3BucketName=\"{}\" targetS3Key=\"{}\" targetS3BucketName=\"{}\" " + "totalBytesTransferred={} transferDuration=\"{}\"", params.getSourceObjectKey(), params.getSourceBucketName(), params.getTargetObjectKey(), params.getTargetBucketName(), results.getTotalBytesTransferred(), HerdDateUtils.formatDuration(results.getDurationMillis()));
    logOverallTransferRate(results);
    return results;
}
Also used : TransferManager(com.amazonaws.services.s3.transfer.TransferManager) CopyObjectRequest(com.amazonaws.services.s3.model.CopyObjectRequest) Transfer(com.amazonaws.services.s3.transfer.Transfer) S3FileTransferResultsDto(org.finra.herd.model.dto.S3FileTransferResultsDto) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) SSEAwsKeyManagementParams(com.amazonaws.services.s3.model.SSEAwsKeyManagementParams)

Example 17 with CopyObjectRequest

use of software.amazon.awssdk.services.s3.model.CopyObjectRequest in project beam by apache.

the class S3FileSystemTest method testCopy.

private void testCopy(S3FileSystem s3FileSystem, SSECustomerKey sseCustomerKey) throws IOException {
    S3ResourceId sourcePath = S3ResourceId.fromUri(s3FileSystem.getScheme() + "://bucket/from");
    S3ResourceId destinationPath = S3ResourceId.fromUri(s3FileSystem.getScheme() + "://bucket/to");
    HeadObjectResponse.Builder builder = HeadObjectResponse.builder().contentLength(0L);
    String sseCustomerKeyMd5 = toMd5(sseCustomerKey);
    if (sseCustomerKeyMd5 != null) {
        builder.sseCustomerKeyMD5(sseCustomerKeyMd5);
    }
    HeadObjectResponse headObjectResponse = builder.build();
    assertGetObjectHead(s3FileSystem, createObjectHeadRequest(sourcePath, sseCustomerKey), sseCustomerKeyMd5, headObjectResponse);
    s3FileSystem.copy(sourcePath, destinationPath);
    verify(s3FileSystem.getS3Client(), times(1)).copyObject(any(CopyObjectRequest.class));
    // we simulate a big object >= 5GB so it takes the multiPart path
    HeadObjectResponse bigHeadObjectResponse = headObjectResponse.toBuilder().contentLength(5_368_709_120L).build();
    assertGetObjectHead(s3FileSystem, createObjectHeadRequest(sourcePath, sseCustomerKey), sseCustomerKeyMd5, bigHeadObjectResponse);
    try {
        s3FileSystem.copy(sourcePath, destinationPath);
    } catch (NullPointerException e) {
    // ignore failing unmocked path, this is covered by testMultipartCopy test
    }
    verify(s3FileSystem.getS3Client(), never()).copyObject((CopyObjectRequest) null);
}
Also used : CopyObjectRequest(software.amazon.awssdk.services.s3.model.CopyObjectRequest) HeadObjectResponse(software.amazon.awssdk.services.s3.model.HeadObjectResponse)

Example 18 with CopyObjectRequest

use of software.amazon.awssdk.services.s3.model.CopyObjectRequest in project beam by apache.

the class S3FileSystemTest method testAtomicCopy.

private void testAtomicCopy(S3FileSystem s3FileSystem, SSECustomerKey sseCustomerKey) {
    S3ResourceId sourcePath = S3ResourceId.fromUri(s3FileSystem.getScheme() + "://bucket/from");
    S3ResourceId destinationPath = S3ResourceId.fromUri(s3FileSystem.getScheme() + "://bucket/to");
    CopyObjectResult copyObjectResult = new CopyObjectResult();
    String sseCustomerKeyMd5 = toMd5(sseCustomerKey);
    if (sseCustomerKeyMd5 != null) {
        copyObjectResult.setSSECustomerKeyMd5(sseCustomerKeyMd5);
    }
    CopyObjectRequest copyObjectRequest = new CopyObjectRequest(sourcePath.getBucket(), sourcePath.getKey(), destinationPath.getBucket(), destinationPath.getKey());
    copyObjectRequest.setSourceSSECustomerKey(sseCustomerKey);
    copyObjectRequest.setDestinationSSECustomerKey(sseCustomerKey);
    when(s3FileSystem.getAmazonS3Client().copyObject(any(CopyObjectRequest.class))).thenReturn(copyObjectResult);
    assertEquals(sseCustomerKeyMd5, s3FileSystem.getAmazonS3Client().copyObject(copyObjectRequest).getSSECustomerKeyMd5());
    ObjectMetadata sourceS3ObjectMetadata = new ObjectMetadata();
    s3FileSystem.atomicCopy(sourcePath, destinationPath, sourceS3ObjectMetadata);
    verify(s3FileSystem.getAmazonS3Client(), times(2)).copyObject(any(CopyObjectRequest.class));
}
Also used : CopyObjectRequest(com.amazonaws.services.s3.model.CopyObjectRequest) CopyObjectResult(com.amazonaws.services.s3.model.CopyObjectResult) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata)

Example 19 with CopyObjectRequest

use of software.amazon.awssdk.services.s3.model.CopyObjectRequest in project beam by apache.

the class S3FileSystem method atomicCopy.

@VisibleForTesting
CopyObjectResult atomicCopy(S3ResourceId sourcePath, S3ResourceId destinationPath, ObjectMetadata sourceObjectMetadata) throws AmazonClientException {
    CopyObjectRequest copyObjectRequest = new CopyObjectRequest(sourcePath.getBucket(), sourcePath.getKey(), destinationPath.getBucket(), destinationPath.getKey());
    copyObjectRequest.setNewObjectMetadata(sourceObjectMetadata);
    copyObjectRequest.setStorageClass(config.getS3StorageClass());
    copyObjectRequest.setSourceSSECustomerKey(config.getSSECustomerKey());
    copyObjectRequest.setDestinationSSECustomerKey(config.getSSECustomerKey());
    return amazonS3.get().copyObject(copyObjectRequest);
}
Also used : CopyObjectRequest(com.amazonaws.services.s3.model.CopyObjectRequest) VisibleForTesting(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting)

Example 20 with CopyObjectRequest

use of software.amazon.awssdk.services.s3.model.CopyObjectRequest in project stocator by SparkTC.

the class COSAPIClient method copyFile.

/**
 * Copy a single object in the bucket via a COPY operation.
 * @param srcKey source object path
 * @param dstKey destination object path
 * @param size object size
 * @throws AmazonClientException on failures inside the AWS SDK
 * @throws InterruptedIOException the operation was interrupted
 * @throws IOException Other IO problems
 */
private void copyFile(String srcKey, String dstKey, long size) throws IOException, InterruptedIOException, AmazonClientException {
    LOG.debug("copyFile {} -> {} ", srcKey, dstKey);
    CopyObjectRequest copyObjectRequest = new CopyObjectRequest(mBucket, srcKey, mBucket, dstKey);
    try {
        ObjectMetadata srcmd = getObjectMetadata(srcKey);
        if (srcmd != null) {
            copyObjectRequest.setNewObjectMetadata(srcmd);
        }
        ProgressListener progressListener = new ProgressListener() {

            public void progressChanged(ProgressEvent progressEvent) {
                switch(progressEvent.getEventType()) {
                    case TRANSFER_PART_COMPLETED_EVENT:
                        break;
                    default:
                        break;
                }
            }
        };
        Copy copy = transfers.copy(copyObjectRequest);
        copy.addProgressListener(progressListener);
        try {
            copy.waitForCopyResult();
        } catch (InterruptedException e) {
            throw new InterruptedIOException("Interrupted copying " + srcKey + " to " + dstKey + ", cancelling");
        }
    } catch (AmazonClientException e) {
        throw translateException("copyFile(" + srcKey + ", " + dstKey + ")", srcKey, e);
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) CopyObjectRequest(com.amazonaws.services.s3.model.CopyObjectRequest) ProgressListener(com.amazonaws.event.ProgressListener) Copy(com.amazonaws.services.s3.transfer.Copy) AmazonClientException(com.amazonaws.AmazonClientException) ProgressEvent(com.amazonaws.event.ProgressEvent) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata)

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