Search in sources :

Example 6 with CopyObjectRequest

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

the class S3Producer method copyObject.

private void copyObject(AmazonS3 s3Client, Exchange exchange) {
    String bucketNameDestination;
    String destinationKey;
    String sourceKey;
    String bucketName;
    String versionId;
    bucketName = exchange.getIn().getHeader(S3Constants.BUCKET_NAME, String.class);
    if (ObjectHelper.isEmpty(bucketName)) {
        bucketName = getConfiguration().getBucketName();
    }
    sourceKey = exchange.getIn().getHeader(S3Constants.KEY, String.class);
    destinationKey = exchange.getIn().getHeader(S3Constants.DESTINATION_KEY, String.class);
    bucketNameDestination = exchange.getIn().getHeader(S3Constants.BUCKET_DESTINATION_NAME, String.class);
    versionId = exchange.getIn().getHeader(S3Constants.VERSION_ID, String.class);
    if (ObjectHelper.isEmpty(bucketName)) {
        throw new IllegalArgumentException("Bucket Name must be specified for copyObject Operation");
    }
    if (ObjectHelper.isEmpty(bucketNameDestination)) {
        throw new IllegalArgumentException("Bucket Name Destination must be specified for copyObject Operation");
    }
    if (ObjectHelper.isEmpty(sourceKey)) {
        throw new IllegalArgumentException("Source Key must be specified for copyObject Operation");
    }
    if (ObjectHelper.isEmpty(destinationKey)) {
        throw new IllegalArgumentException("Destination Key must be specified for copyObject Operation");
    }
    CopyObjectRequest copyObjectRequest;
    if (ObjectHelper.isEmpty(versionId)) {
        copyObjectRequest = new CopyObjectRequest(bucketName, sourceKey, bucketNameDestination, destinationKey);
    } else {
        copyObjectRequest = new CopyObjectRequest(bucketName, sourceKey, versionId, bucketNameDestination, destinationKey);
    }
    CopyObjectResult copyObjectResult = s3Client.copyObject(copyObjectRequest);
    Message message = getMessageForResponse(exchange);
    message.setHeader(S3Constants.E_TAG, copyObjectResult.getETag());
    if (copyObjectResult.getVersionId() != null) {
        message.setHeader(S3Constants.VERSION_ID, copyObjectResult.getVersionId());
    }
}
Also used : CopyObjectRequest(com.amazonaws.services.s3.model.CopyObjectRequest) Message(org.apache.camel.Message) CopyObjectResult(com.amazonaws.services.s3.model.CopyObjectResult)

Example 7 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) throws IOException {
    S3ResourceId sourcePath = S3ResourceId.fromUri(s3FileSystem.getScheme() + "://bucket/from");
    S3ResourceId destinationPath = S3ResourceId.fromUri(s3FileSystem.getScheme() + "://bucket/to");
    CopyObjectResponse.Builder builder = CopyObjectResponse.builder();
    String sseCustomerKeyMd5 = toMd5(sseCustomerKey);
    if (sseCustomerKeyMd5 != null) {
        builder.sseCustomerKeyMD5(sseCustomerKeyMd5);
    }
    CopyObjectResponse copyObjectResponse = builder.build();
    CopyObjectRequest copyObjectRequest = CopyObjectRequest.builder().copySource(sourcePath.getBucket() + "/" + sourcePath.getKey()).destinationBucket(destinationPath.getBucket()).destinationBucket(destinationPath.getKey()).sseCustomerKey(sseCustomerKey.getKey()).copySourceSSECustomerAlgorithm(sseCustomerKey.getAlgorithm()).build();
    when(s3FileSystem.getS3Client().copyObject(any(CopyObjectRequest.class))).thenReturn(copyObjectResponse);
    assertEquals(sseCustomerKeyMd5, s3FileSystem.getS3Client().copyObject(copyObjectRequest).sseCustomerKeyMD5());
    HeadObjectResponse headObjectResponse = HeadObjectResponse.builder().build();
    s3FileSystem.atomicCopy(sourcePath, destinationPath, headObjectResponse);
    verify(s3FileSystem.getS3Client(), times(2)).copyObject(any(CopyObjectRequest.class));
}
Also used : CopyObjectRequest(software.amazon.awssdk.services.s3.model.CopyObjectRequest) HeadObjectResponse(software.amazon.awssdk.services.s3.model.HeadObjectResponse) CopyObjectResponse(software.amazon.awssdk.services.s3.model.CopyObjectResponse)

Example 8 with CopyObjectRequest

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

the class S3Backend method write.

private void write(DataIdentifier identifier, File file, boolean asyncUpload, AsyncUploadCallback callback) throws DataStoreException {
    String key = getKeyName(identifier);
    ObjectMetadata objectMetaData = null;
    long start = System.currentTimeMillis();
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        // check if the same record already exists
        try {
            objectMetaData = s3service.getObjectMetadata(bucket, key);
        } catch (AmazonServiceException ase) {
            if (!(ase.getStatusCode() == 404 || ase.getStatusCode() == 403)) {
                throw ase;
            }
        }
        if (objectMetaData != null) {
            long l = objectMetaData.getContentLength();
            if (l != file.length()) {
                throw new DataStoreException("Collision: " + key + " new length: " + file.length() + " old length: " + l);
            }
            LOG.debug("[{}]'s exists, lastmodified = [{}]", key, objectMetaData.getLastModified().getTime());
            CopyObjectRequest copReq = new CopyObjectRequest(bucket, key, bucket, key);
            copReq.setNewObjectMetadata(objectMetaData);
            Copy copy = tmx.copy(s3ReqDecorator.decorate(copReq));
            try {
                copy.waitForCopyResult();
                LOG.debug("lastModified of [{}] updated successfully.", identifier);
                if (callback != null) {
                    callback.onSuccess(new AsyncUploadResult(identifier, file));
                }
            } catch (Exception e2) {
                AsyncUploadResult asyncUpRes = new AsyncUploadResult(identifier, file);
                asyncUpRes.setException(e2);
                if (callback != null) {
                    callback.onAbort(asyncUpRes);
                }
                throw new DataStoreException("Could not upload " + key, e2);
            }
        }
        if (objectMetaData == null) {
            try {
                // start multipart parallel upload using amazon sdk
                Upload up = tmx.upload(s3ReqDecorator.decorate(new PutObjectRequest(bucket, key, file)));
                // wait for upload to finish
                if (asyncUpload) {
                    up.addProgressListener(new S3UploadProgressListener(up, identifier, file, callback));
                    LOG.debug("added upload progress listener to identifier [{}]", identifier);
                } else {
                    up.waitForUploadResult();
                    LOG.debug("synchronous upload to identifier [{}] completed.", identifier);
                    if (callback != null) {
                        callback.onSuccess(new AsyncUploadResult(identifier, file));
                    }
                }
            } catch (Exception e2) {
                AsyncUploadResult asyncUpRes = new AsyncUploadResult(identifier, file);
                asyncUpRes.setException(e2);
                if (callback != null) {
                    callback.onAbort(asyncUpRes);
                }
                throw new DataStoreException("Could not upload " + key, e2);
            }
        }
    } finally {
        if (contextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
    LOG.debug("write of [{}], length=[{}], in async mode [{}], in [{}]ms", new Object[] { identifier, file.length(), asyncUpload, (System.currentTimeMillis() - start) });
}
Also used : AsyncUploadResult(org.apache.jackrabbit.core.data.AsyncUploadResult) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) Upload(com.amazonaws.services.s3.transfer.Upload) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonClientException(com.amazonaws.AmazonClientException) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) IOException(java.io.IOException) CopyObjectRequest(com.amazonaws.services.s3.model.CopyObjectRequest) Copy(com.amazonaws.services.s3.transfer.Copy) AmazonServiceException(com.amazonaws.AmazonServiceException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 9 with CopyObjectRequest

use of software.amazon.awssdk.services.s3.model.CopyObjectRequest in project aws-doc-sdk-examples by awsdocs.

the class CopyObject method copyBucketObject.

// snippet-start:[s3.java2.copy_object.main]
public static String copyBucketObject(S3Client s3, String fromBucket, String objectKey, String toBucket) {
    String encodedUrl = null;
    try {
        encodedUrl = URLEncoder.encode(fromBucket + "/" + objectKey, StandardCharsets.UTF_8.toString());
    } catch (UnsupportedEncodingException e) {
        System.out.println("URL could not be encoded: " + e.getMessage());
    }
    CopyObjectRequest copyReq = CopyObjectRequest.builder().copySource(encodedUrl).destinationBucket(toBucket).destinationKey(objectKey).build();
    try {
        CopyObjectResponse copyRes = s3.copyObject(copyReq);
        return copyRes.copyObjectResult().toString();
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return "";
}
Also used : CopyObjectRequest(software.amazon.awssdk.services.s3.model.CopyObjectRequest) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CopyObjectResponse(software.amazon.awssdk.services.s3.model.CopyObjectResponse)

Example 10 with CopyObjectRequest

use of software.amazon.awssdk.services.s3.model.CopyObjectRequest in project aws-doc-sdk-examples by awsdocs.

the class ServerSideEncryptionCopyObjectUsingHLwithSSEC method main.

public static void main(String[] args) throws Exception {
    Regions clientRegion = Regions.DEFAULT_REGION;
    String bucketName = "*** Bucket name ***";
    String fileToUpload = "*** File path ***";
    String keyName = "*** New object key name ***";
    String targetKeyName = "*** Key name for object copy ***";
    try {
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion(clientRegion).withCredentials(new ProfileCredentialsProvider()).build();
        TransferManager tm = TransferManagerBuilder.standard().withS3Client(s3Client).build();
        // Create an object from a file.
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyName, new File(fileToUpload));
        // Create an encryption key.
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(256, new SecureRandom());
        SSECustomerKey sseCustomerEncryptionKey = new SSECustomerKey(keyGenerator.generateKey());
        // Upload the object. TransferManager uploads asynchronously, so this call returns immediately.
        putObjectRequest.setSSECustomerKey(sseCustomerEncryptionKey);
        Upload upload = tm.upload(putObjectRequest);
        // Optionally, wait for the upload to finish before continuing.
        upload.waitForCompletion();
        System.out.println("Object created.");
        // Copy the object and store the copy using SSE-C with a new key.
        CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucketName, keyName, bucketName, targetKeyName);
        SSECustomerKey sseTargetObjectEncryptionKey = new SSECustomerKey(keyGenerator.generateKey());
        copyObjectRequest.setSourceSSECustomerKey(sseCustomerEncryptionKey);
        copyObjectRequest.setDestinationSSECustomerKey(sseTargetObjectEncryptionKey);
        // Copy the object. TransferManager copies asynchronously, so this call returns immediately.
        Copy copy = tm.copy(copyObjectRequest);
        // Optionally, wait for the upload to finish before continuing.
        copy.waitForCompletion();
        System.out.println("Copy complete.");
    } catch (AmazonServiceException e) {
        // The call was transmitted successfully, but Amazon S3 couldn't process
        // it, so it returned an error response.
        e.printStackTrace();
    } catch (SdkClientException e) {
        // Amazon S3 couldn't be contacted for a response, or the client
        // couldn't parse the response from Amazon S3.
        e.printStackTrace();
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) TransferManager(com.amazonaws.services.s3.transfer.TransferManager) SecureRandom(java.security.SecureRandom) Upload(com.amazonaws.services.s3.transfer.Upload) Regions(com.amazonaws.regions.Regions) SSECustomerKey(com.amazonaws.services.s3.model.SSECustomerKey) CopyObjectRequest(com.amazonaws.services.s3.model.CopyObjectRequest) SdkClientException(com.amazonaws.SdkClientException) Copy(com.amazonaws.services.s3.transfer.Copy) AmazonServiceException(com.amazonaws.AmazonServiceException) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) File(java.io.File) KeyGenerator(javax.crypto.KeyGenerator) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

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