Example usage for com.amazonaws.services.s3.model InitiateMultipartUploadRequest setSSECustomerKey

List of usage examples for com.amazonaws.services.s3.model InitiateMultipartUploadRequest setSSECustomerKey

Introduction

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

Prototype

public void setSSECustomerKey(SSECustomerKey sseKey) 

Source Link

Document

Sets the optional customer-provided server-side encryption key to use to encrypt the upload being started.

Usage

From source file:io.confluent.connect.s3.storage.S3OutputStream.java

License:Open Source License

private MultipartUpload newMultipartUpload() throws IOException {
    InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(bucket, key,
            newObjectMetadata()).withCannedACL(cannedAcl);

    if (SSEAlgorithm.KMS.toString().equalsIgnoreCase(ssea) && StringUtils.isNotBlank(sseKmsKeyId)) {
        initRequest.setSSEAwsKeyManagementParams(new SSEAwsKeyManagementParams(sseKmsKeyId));
    } else if (sseCustomerKey != null) {
        initRequest.setSSECustomerKey(sseCustomerKey);
    }//from w w w.ja  v a  2  s  .  c om

    try {
        return new MultipartUpload(s3.initiateMultipartUpload(initRequest).getUploadId());
    } catch (AmazonClientException e) {
        // TODO: elaborate on the exception interpretation. If this is an AmazonServiceException,
        // there's more info to be extracted.
        throw new IOException("Unable to initiate MultipartUpload: " + e, e);
    }
}

From source file:io.minio.awssdk.tests.S3TestUtils.java

License:Apache License

void uploadMultipartObject(String bucketName, String keyName, String filePath, SSECustomerKey sseKey)
        throws IOException {

    File file = new File(filePath);

    List<PartETag> partETags = new ArrayList<PartETag>();

    // Step 1: Initialize.
    InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(bucketName, keyName);

    if (sseKey != null) {
        initRequest.setSSECustomerKey(sseKey);
    }/*  w  w  w . j  a v a2s.com*/

    InitiateMultipartUploadResult initResponse = s3Client.initiateMultipartUpload(initRequest);

    long contentLength = file.length();
    long partSize = 5242880; // Set part size to 5 MB.

    // Step 2: Upload parts.
    long filePosition = 0;
    for (int i = 1; filePosition < contentLength; i++) {
        // Last part can be less than 5 MB. Adjust part size.
        partSize = Math.min(partSize, (contentLength - filePosition));

        // Create request to upload a part.
        UploadPartRequest uploadRequest = new UploadPartRequest().withBucketName(bucketName).withKey(keyName)
                .withUploadId(initResponse.getUploadId()).withPartNumber(i).withFileOffset(filePosition)
                .withFile(file).withPartSize(partSize);

        if (sseKey != null) {
            uploadRequest.withSSECustomerKey(sseKey);
        }

        // Upload part and add response to our list.
        partETags.add(s3Client.uploadPart(uploadRequest).getPartETag());

        filePosition += partSize;
    }

    // Step 3: Complete.
    CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(bucketName, keyName,
            initResponse.getUploadId(), partETags);

    s3Client.completeMultipartUpload(compRequest);
}

From source file:org.apache.beam.sdk.io.aws.s3.S3FileSystem.java

License:Apache License

@VisibleForTesting
CompleteMultipartUploadResult multipartCopy(S3ResourceId sourcePath, S3ResourceId destinationPath,
        ObjectMetadata sourceObjectMetadata) throws AmazonClientException {
    InitiateMultipartUploadRequest initiateUploadRequest = new InitiateMultipartUploadRequest(
            destinationPath.getBucket(), destinationPath.getKey()).withStorageClass(options.getS3StorageClass())
                    .withObjectMetadata(sourceObjectMetadata);
    initiateUploadRequest.setSSECustomerKey(options.getSSECustomerKey());

    InitiateMultipartUploadResult initiateUploadResult = amazonS3.get()
            .initiateMultipartUpload(initiateUploadRequest);
    final String uploadId = initiateUploadResult.getUploadId();

    List<PartETag> eTags = new ArrayList<>();

    final long objectSize = sourceObjectMetadata.getContentLength();
    // extra validation in case a caller calls directly S3FileSystem.multipartCopy
    // without using S3FileSystem.copy in the future
    if (objectSize == 0) {
        final CopyPartRequest copyPartRequest = new CopyPartRequest()
                .withSourceBucketName(sourcePath.getBucket()).withSourceKey(sourcePath.getKey())
                .withDestinationBucketName(destinationPath.getBucket())
                .withDestinationKey(destinationPath.getKey()).withUploadId(uploadId).withPartNumber(1);
        copyPartRequest.setSourceSSECustomerKey(options.getSSECustomerKey());
        copyPartRequest.setDestinationSSECustomerKey(options.getSSECustomerKey());

        CopyPartResult copyPartResult = amazonS3.get().copyPart(copyPartRequest);
        eTags.add(copyPartResult.getPartETag());
    } else {/*from w  w w  . ja  v  a2 s  .c  o  m*/
        long bytePosition = 0;
        Integer uploadBufferSizeBytes = options.getS3UploadBufferSizeBytes();
        // Amazon parts are 1-indexed, not zero-indexed.
        for (int partNumber = 1; bytePosition < objectSize; partNumber++) {
            final CopyPartRequest copyPartRequest = new CopyPartRequest()
                    .withSourceBucketName(sourcePath.getBucket()).withSourceKey(sourcePath.getKey())
                    .withDestinationBucketName(destinationPath.getBucket())
                    .withDestinationKey(destinationPath.getKey()).withUploadId(uploadId)
                    .withPartNumber(partNumber).withFirstByte(bytePosition)
                    .withLastByte(Math.min(objectSize - 1, bytePosition + uploadBufferSizeBytes - 1));
            copyPartRequest.setSourceSSECustomerKey(options.getSSECustomerKey());
            copyPartRequest.setDestinationSSECustomerKey(options.getSSECustomerKey());

            CopyPartResult copyPartResult = amazonS3.get().copyPart(copyPartRequest);
            eTags.add(copyPartResult.getPartETag());

            bytePosition += uploadBufferSizeBytes;
        }
    }

    CompleteMultipartUploadRequest completeUploadRequest = new CompleteMultipartUploadRequest()
            .withBucketName(destinationPath.getBucket()).withKey(destinationPath.getKey())
            .withUploadId(uploadId).withPartETags(eTags);
    return amazonS3.get().completeMultipartUpload(completeUploadRequest);
}

From source file:org.apache.beam.sdk.io.aws.s3.S3WritableByteChannel.java

License:Apache License

S3WritableByteChannel(AmazonS3 amazonS3, S3ResourceId path, String contentType, S3Options options)
        throws IOException {
    this.amazonS3 = checkNotNull(amazonS3, "amazonS3");
    this.options = checkNotNull(options);
    this.path = checkNotNull(path, "path");
    checkArgument(//w  w  w .j a va 2  s.  c o m
            atMostOne(options.getSSECustomerKey() != null, options.getSSEAlgorithm() != null,
                    options.getSSEAwsKeyManagementParams() != null),
            "Either SSECustomerKey (SSE-C) or SSEAlgorithm (SSE-S3)"
                    + " or SSEAwsKeyManagementParams (SSE-KMS) must not be set at the same time.");
    // Amazon S3 API docs: Each part must be at least 5 MB in size, except the last part.
    checkArgument(options
            .getS3UploadBufferSizeBytes() >= S3UploadBufferSizeBytesFactory.MINIMUM_UPLOAD_BUFFER_SIZE_BYTES,
            "S3UploadBufferSizeBytes must be at least %s bytes",
            S3UploadBufferSizeBytesFactory.MINIMUM_UPLOAD_BUFFER_SIZE_BYTES);
    this.uploadBuffer = ByteBuffer.allocate(options.getS3UploadBufferSizeBytes());
    eTags = new ArrayList<>();

    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setContentType(contentType);
    if (options.getSSEAlgorithm() != null) {
        objectMetadata.setSSEAlgorithm(options.getSSEAlgorithm());
    }
    InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(path.getBucket(), path.getKey())
            .withStorageClass(options.getS3StorageClass()).withObjectMetadata(objectMetadata);
    request.setSSECustomerKey(options.getSSECustomerKey());
    request.setSSEAwsKeyManagementParams(options.getSSEAwsKeyManagementParams());
    InitiateMultipartUploadResult result;
    try {
        result = amazonS3.initiateMultipartUpload(request);
    } catch (AmazonClientException e) {
        throw new IOException(e);
    }
    uploadId = result.getUploadId();
}

From source file:org.apache.nifi.processors.aws.s3.encryption.ServerSideCEKEncryptionStrategy.java

License:Apache License

@Override
public void configureInitiateMultipartUploadRequest(InitiateMultipartUploadRequest request,
        ObjectMetadata objectMetadata, String keyValue) {
    SSECustomerKey customerKey = new SSECustomerKey(keyValue);
    request.setSSECustomerKey(customerKey);
}

From source file:org.apache.nifi.processors.aws.s3.encryption.service.StandardS3ServerSideEncryptionService.java

License:Apache License

public void encrypt(InitiateMultipartUploadRequest initiateMultipartUploadRequest) {
    if (encryptionMethod == null)
        return;/*from   w ww  .  j a v  a 2 s .  c  o  m*/

    if (encryptionMethod.equals(METHOD_SSE_S3)) {
        getLogger().info("Encrypting multipart object using SSE-S3");
        initiateMultipartUploadRequest.getObjectMetadata()
                .setSSEAlgorithm(algorithm == null ? ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION : algorithm);
    }

    if (encryptionMethod.equals(METHOD_SSE_KMS)) {
        getLogger().info("Encrypting multipart object using SSE-KMS");
        initiateMultipartUploadRequest.setSSEAwsKeyManagementParams(
                kmsKeyId == null ? new SSEAwsKeyManagementParams() : new SSEAwsKeyManagementParams(kmsKeyId));
    }

    if (encryptionMethod.equals(METHOD_SSE_C)) {
        getLogger().info("Encrypting multipart object using SSE-C");
        if (StringUtils.isNotBlank(customerKey)) {
            initiateMultipartUploadRequest.setSSECustomerKey(new SSECustomerKey(customerKey));
        }

        String sseCustomerAlgorithm = customerAlgorithm == null ? ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION
                : customerAlgorithm;
        initiateMultipartUploadRequest.getObjectMetadata().setSSECustomerAlgorithm(sseCustomerAlgorithm);

        if (StringUtils.isNotBlank(customerKeyMD5)) {
            initiateMultipartUploadRequest.getObjectMetadata().setSSECustomerKeyMd5(customerKeyMD5);
        }
    }
}