Example usage for com.amazonaws.services.s3.model CopyObjectRequest getNewObjectMetadata

List of usage examples for com.amazonaws.services.s3.model CopyObjectRequest getNewObjectMetadata

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.model CopyObjectRequest getNewObjectMetadata.

Prototype

public ObjectMetadata getNewObjectMetadata() 

Source Link

Document

Gets the optional object metadata to set for the new, copied object.

Usage

From source file:io.druid.storage.s3.S3ServerSideEncryption.java

License:Apache License

@Override
public CopyObjectRequest decorate(CopyObjectRequest request) {
    final ObjectMetadata objectMetadata = request.getNewObjectMetadata() == null ? new ObjectMetadata()
            : request.getNewObjectMetadata().clone();
    objectMetadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
    return request.withNewObjectMetadata(objectMetadata);
}

From source file:org.apache.jackrabbit.oak.blob.cloud.aws.s3.S3RequestDecorator.java

License:Apache License

/**
 * Set encryption in {@link CopyObjectRequest}
 *//*from ww w.ja va  2 s  .  c o m*/
public CopyObjectRequest decorate(CopyObjectRequest request) {
    switch (getDataEncryption()) {
    case SSE_S3:
        ObjectMetadata metadata = request.getNewObjectMetadata() == null ? new ObjectMetadata()
                : request.getNewObjectMetadata();
        metadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
        request.setNewObjectMetadata(metadata);
        break;
    case NONE:
        break;
    }
    return request;
}

From source file:org.finra.herd.dao.impl.MockS3OperationsImpl.java

License:Apache License

/**
 * {@inheritDoc} <p/> <p> This implementation simulates a copyFile operation. </p> <p> This method copies files in-memory. </p> <p> The result {@link Copy}
 * has the following properties: <dl> <p/> <dt>description</dt> <dd>"MockTransfer"</dd> <p/> <dt>state</dt> <dd>{@link TransferState#Completed}</dd> <p/>
 * <dt>transferProgress.totalBytesToTransfer</dt> <dd>1024</dd> <p/> <dt>transferProgress.updateProgress</dt> <dd>1024</dd> <p/> </dl> <p/> All other
 * properties are set as default. </p> <p> This operation takes the following hints when suffixed in copyObjectRequest.sourceKey: <dl> <p/>
 * <dt>MOCK_S3_FILE_NAME_SERVICE_EXCEPTION</dt> <dd>Throws a AmazonServiceException</dd> <p/> </dl> </p>
 *//*from   w  ww. ja va 2  s .  c om*/
@Override
public Copy copyFile(final CopyObjectRequest copyObjectRequest, TransferManager transferManager) {
    LOGGER.debug("copyFile(): copyObjectRequest.getSourceBucketName() = "
            + copyObjectRequest.getSourceBucketName() + ", copyObjectRequest.getSourceKey() = "
            + copyObjectRequest.getSourceKey() + ", copyObjectRequest.getDestinationBucketName() = "
            + copyObjectRequest.getDestinationBucketName() + ", copyObjectRequest.getDestinationKey() = "
            + copyObjectRequest.getDestinationKey());

    if (copyObjectRequest.getSourceKey().endsWith(MOCK_S3_FILE_NAME_SERVICE_EXCEPTION)) {
        throw new AmazonServiceException(null);
    }

    String sourceBucketName = copyObjectRequest.getSourceBucketName();
    String sourceKey = copyObjectRequest.getSourceKey();

    MockS3Bucket mockSourceS3Bucket = getOrCreateBucket(sourceBucketName);
    MockS3Object mockSourceS3Object = mockSourceS3Bucket.getObjects().get(sourceKey);

    if (mockSourceS3Object == null) {
        AmazonServiceException amazonServiceException = new AmazonServiceException(
                S3Operations.ERROR_CODE_NO_SUCH_KEY);
        amazonServiceException.setErrorCode(S3Operations.ERROR_CODE_NO_SUCH_KEY);
        throw amazonServiceException;
    }

    // Set the result CopyImpl and TransferProgress.
    TransferProgress transferProgress = new TransferProgress();
    transferProgress.setTotalBytesToTransfer(mockSourceS3Object.getObjectMetadata().getContentLength());
    transferProgress.updateProgress(mockSourceS3Object.getObjectMetadata().getContentLength());
    CopyImpl copy = new CopyImpl(MOCK_TRANSFER_DESCRIPTION, transferProgress, null, null);
    copy.setState(TransferState.Completed);

    // If an invalid KMS Id was passed in, mark the transfer as failed and return an exception via the transfer monitor.
    if (copyObjectRequest.getSSEAwsKeyManagementParams() != null) {
        final String kmsId = copyObjectRequest.getSSEAwsKeyManagementParams().getAwsKmsKeyId();
        if (kmsId.startsWith(MOCK_KMS_ID_FAILED_TRANSFER)) {
            copy.setState(TransferState.Failed);
            copy.setMonitor(new TransferMonitor() {
                @Override
                public Future<?> getFuture() {
                    if (!kmsId.equals(MOCK_KMS_ID_FAILED_TRANSFER_NO_EXCEPTION)) {
                        throw new AmazonServiceException("Key '"
                                + copyObjectRequest.getSSEAwsKeyManagementParams().getAwsKmsKeyId()
                                + "' does not exist (Service: Amazon S3; Status Code: 400; Error Code: KMS.NotFoundException; Request ID: 1234567890123456)");
                    }

                    // We don't want an exception to be thrown so return a basic future that won't throw an exception.
                    BasicFuture<?> future = new BasicFuture<Void>(null);
                    future.completed(null);
                    return future;
                }

                @Override
                public boolean isDone() {
                    return true;
                }
            });
        } else if (kmsId.startsWith(MOCK_KMS_ID_CANCELED_TRANSFER)) {
            // If the KMS indicates a cancelled transfer, just update the state to canceled.
            copy.setState(TransferState.Canceled);
        }
    }

    // If copy operation is marked as completed, perform the actual file copy in memory.
    if (copy.getState().equals(TransferState.Completed)) {
        String destinationBucketName = copyObjectRequest.getDestinationBucketName();
        String destinationObjectKey = copyObjectRequest.getDestinationKey();
        String destinationObjectVersion = MOCK_S3_BUCKET_NAME_VERSIONING_ENABLED.equals(destinationBucketName)
                ? UUID.randomUUID().toString()
                : null;
        String destinationObjectKeyVersion = destinationObjectKey
                + (destinationObjectVersion != null ? destinationObjectVersion : "");

        ObjectMetadata objectMetadata = copyObjectRequest.getNewObjectMetadata();
        MockS3Object mockDestinationS3Object = new MockS3Object();
        mockDestinationS3Object.setKey(destinationObjectKey);
        mockDestinationS3Object.setVersion(destinationObjectVersion);
        mockDestinationS3Object
                .setData(Arrays.copyOf(mockSourceS3Object.getData(), mockSourceS3Object.getData().length));
        mockDestinationS3Object.setObjectMetadata(objectMetadata);

        MockS3Bucket mockDestinationS3Bucket = getOrCreateBucket(destinationBucketName);
        mockDestinationS3Bucket.getObjects().put(destinationObjectKey, mockDestinationS3Object);
        mockDestinationS3Bucket.getVersions().put(destinationObjectKeyVersion, mockDestinationS3Object);
    }

    return copy;
}

From source file:org.mule.module.s3.simpleapi.SimpleAmazonS3AmazonDevKitImpl.java

License:Open Source License

public String copyObject(@NotNull S3ObjectId source, @NotNull S3ObjectId destination,
        @NotNull ConditionalConstraints conditionalConstraints, CannedAccessControlList acl,
        StorageClass storageClass, Map<String, String> userMetadata, String encryption) {
    Validate.notNull(source);//from   ww w . j  ava  2  s  .  com
    Validate.notNull(destination);
    Validate.notNull(conditionalConstraints);
    CopyObjectRequest request = new CopyObjectRequest(source.getBucketName(), source.getKey(),
            source.getVersionId(), destination.getBucketName(), destination.getKey());
    request.setCannedAccessControlList(acl);
    if (storageClass != null) {
        request.setStorageClass(storageClass);
    }

    if (encryption != null) {
        request.setNewObjectMetadata(new ObjectMetadata());
        request.getNewObjectMetadata().setServerSideEncryption(encryption);
        if (userMetadata != null) {
            request.getNewObjectMetadata().setUserMetadata(userMetadata);
        }
    } else if (userMetadata != null) {
        request.setNewObjectMetadata(new ObjectMetadata());
        request.getNewObjectMetadata().setUserMetadata(userMetadata);
    }

    conditionalConstraints.populate(request);
    return s3.copyObject(request).getVersionId();
}