Example usage for com.amazonaws.services.s3.model ObjectMetadata setSSEAlgorithm

List of usage examples for com.amazonaws.services.s3.model ObjectMetadata setSSEAlgorithm

Introduction

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

Prototype

@Override
public void setSSEAlgorithm(String algorithm) 

Source Link

Document

Sets the server-side encryption algorithm when encrypting the object using AWS-managed keys.

Usage

From source file:com.streamsets.pipeline.stage.destination.s3.FileHelper.java

License:Apache License

protected ObjectMetadata getObjectMetadata() throws StageException {
    ObjectMetadata metadata = null;
    if (s3TargetConfigBean.sseConfig.useSSE) {
        metadata = new ObjectMetadata();
        switch (s3TargetConfigBean.sseConfig.encryption) {
        case S3://www. j  a va  2 s  .c o  m
            metadata.setSSEAlgorithm(SSEAlgorithm.AES256.getAlgorithm());
            break;
        case KMS:
            metadata.setSSEAlgorithm(SSEAlgorithm.KMS.getAlgorithm());
            metadata.setHeader(Headers.SERVER_SIDE_ENCRYPTION_AWS_KMS_KEYID,
                    s3TargetConfigBean.sseConfig.kmsKeyId.get());
            if (!s3TargetConfigBean.sseConfig.encryptionContext.isEmpty()) {
                metadata.setHeader("x-amz-server-side-encryption-context",
                        s3TargetConfigBean.sseConfig.resolveEncryptionContext());
            }
            break;
        case CUSTOMER:
            metadata.setSSECustomerAlgorithm(SSEAlgorithm.AES256.getAlgorithm());
            metadata.setHeader(Headers.SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY,
                    s3TargetConfigBean.sseConfig.customerKey.get());
            metadata.setHeader(Headers.COPY_SOURCE_SERVER_SIDE_ENCRYPTION_CUSTOMER_KEY_MD5,
                    s3TargetConfigBean.sseConfig.customerKeyMd5.get());
            break;
        default:
            throw new IllegalStateException(
                    Utils.format("Unknown encryption option: ", s3TargetConfigBean.sseConfig.encryption));
        }
    }
    return metadata;
}

From source file:com.universal.storage.UniversalS3Storage.java

License:Open Source License

/**
 * This method uploads a file with a length greater than PART_SIZE (5Mb).
 * /*  ww w.  ja  v a  2 s  .c  om*/
 * @param file to be stored within the storage.
 * @param path is the path for this new file within the root.
 * @throws UniversalIOException when a specific IO error occurs.
 */
private void uploadFile(File file, String path) throws UniversalIOException {
    // Create a list of UploadPartResponse objects. You get one of these
    // for each part upload.
    List<PartETag> partETags = new ArrayList<PartETag>();

    // Step 1: Initialize.
    InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(this.settings.getRoot(),
            file.getName());
    InitiateMultipartUploadResult initResponse = this.s3client.initiateMultipartUpload(initRequest);

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

    ObjectMetadata objectMetadata = new ObjectMetadata();
    if (this.settings.getEncryption()) {
        objectMetadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
    }

    List<Tag> tags = new ArrayList<Tag>();
    for (String key : this.settings.getTags().keySet()) {
        tags.add(new Tag(key, this.settings.getTags().get(key)));
    }

    try {
        this.triggerOnStoreFileListeners();
        // 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(this.settings.getRoot() + ("".equals(path) ? "" : ("/" + path)))
                    .withKey(file.getName()).withUploadId(initResponse.getUploadId()).withPartNumber(i)
                    .withFileOffset(filePosition).withFile(file).withObjectMetadata(objectMetadata)
                    .withPartSize(partSize);

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

            filePosition += partSize;
        }

        // Step 3: Complete.
        CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(
                this.settings.getRoot() + ("".equals(path) ? "" : ("/" + path)), file.getName(),
                initResponse.getUploadId(), partETags);

        CompleteMultipartUploadResult result = this.s3client.completeMultipartUpload(compRequest);

        StorageClass storageClass = getStorageClass();
        if (storageClass != StorageClass.Standard) {
            CopyObjectRequest copyObjectRequest = new CopyObjectRequest(this.settings.getRoot(), file.getName(),
                    this.settings.getRoot(), file.getName()).withStorageClass(storageClass);

            this.s3client.copyObject(copyObjectRequest);
        }

        if (!tags.isEmpty()) {
            this.s3client.setObjectTagging(new SetObjectTaggingRequest(this.settings.getRoot(), file.getName(),
                    new ObjectTagging(tags)));
        }

        this.triggerOnFileStoredListeners(new UniversalStorageData(file.getName(),
                PREFIX_S3_URL + (this.settings.getRoot() + ("".equals(path) ? "" : ("/" + path))) + "/"
                        + file.getName(),
                result.getVersionId(), this.settings.getRoot() + ("".equals(path) ? "" : ("/" + path))));
    } catch (Exception e) {
        this.s3client.abortMultipartUpload(new AbortMultipartUploadRequest(this.settings.getRoot(),
                file.getName(), initResponse.getUploadId()));

        UniversalIOException error = new UniversalIOException(e.getMessage());
        this.triggerOnErrorListeners(error);
        throw error;
    }
}

From source file:com.universal.storage.UniversalS3Storage.java

License:Open Source License

/**
 * This method uploads a file with a length lesser than PART_SIZE (5Mb).
 * /*  w ww  .  j a  v a 2s .c  o m*/
 * @param file to be stored within the storage.
 * @param path is the path for this new file within the root.
 * @throws UniversalIOException when a specific IO error occurs.
 */
private void uploadTinyFile(File file, String path) throws UniversalIOException {
    try {
        ObjectMetadata objectMetadata = new ObjectMetadata();
        if (this.settings.getEncryption()) {
            objectMetadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
        }

        List<Tag> tags = new ArrayList<Tag>();
        for (String key : this.settings.getTags().keySet()) {
            tags.add(new Tag(key, this.settings.getTags().get(key)));
        }

        PutObjectRequest request = new PutObjectRequest(
                this.settings.getRoot() + ("".equals(path) ? "" : ("/" + path)), file.getName(), file);
        request.setMetadata(objectMetadata);
        request.setTagging(new ObjectTagging(tags));
        request.setStorageClass(getStorageClass());
        this.triggerOnStoreFileListeners();

        PutObjectResult result = this.s3client.putObject(request);

        this.triggerOnFileStoredListeners(new UniversalStorageData(file.getName(),
                PREFIX_S3_URL + (this.settings.getRoot() + ("".equals(path) ? "" : ("/" + path))) + "/"
                        + file.getName(),
                result.getVersionId(), this.settings.getRoot() + ("".equals(path) ? "" : ("/" + path))));
    } catch (Exception e) {
        UniversalIOException error = new UniversalIOException(e.getMessage());
        this.triggerOnErrorListeners(error);
        throw error;
    }
}

From source file:com.upplication.s3fs.S3FileSystemProvider.java

License:Open Source License

@Override
public void copy(Path source, Path target, CopyOption... options) throws IOException {
    Preconditions.checkArgument(source instanceof S3Path, "source must be an instance of %s",
            S3Path.class.getName());
    Preconditions.checkArgument(target instanceof S3Path, "target must be an instance of %s",
            S3Path.class.getName());

    if (isSameFile(source, target)) {
        return;//from w  w w  .jav a2 s.  c  o m
    }

    S3Path s3Source = (S3Path) source;
    S3Path s3Target = (S3Path) target;
    /*
     * Preconditions.checkArgument(!s3Source.isDirectory(),
     * "copying directories is not yet supported: %s", source); // TODO
     * Preconditions.checkArgument(!s3Target.isDirectory(),
     * "copying directories is not yet supported: %s", target); // TODO
     */
    ImmutableSet<CopyOption> actualOptions = ImmutableSet.copyOf(options);
    verifySupportedOptions(EnumSet.of(StandardCopyOption.REPLACE_EXISTING), actualOptions);

    if (!actualOptions.contains(StandardCopyOption.REPLACE_EXISTING)) {
        if (exists(s3Target)) {
            throw new FileAlreadyExistsException(format("target already exists: %s", target));
        }
    }

    AmazonS3Client client = s3Source.getFileSystem().getClient();

    final ObjectMetadata sourceObjMetadata = s3Source.getFileSystem().getClient()
            .getObjectMetadata(s3Source.getBucket(), s3Source.getKey());
    final S3MultipartOptions opts = props != null ? new S3MultipartOptions<>(props) : new S3MultipartOptions();
    final int chunkSize = opts.getChunkSize();
    final long length = sourceObjMetadata.getContentLength();

    if (length <= chunkSize) {

        CopyObjectRequest copyObjRequest = new CopyObjectRequest(s3Source.getBucket(), s3Source.getKey(),
                s3Target.getBucket(), s3Target.getKey());
        if (sourceObjMetadata.getSSEAlgorithm() != null) {
            ObjectMetadata targetObjectMetadata = new ObjectMetadata();
            targetObjectMetadata.setSSEAlgorithm(sourceObjMetadata.getSSEAlgorithm());
            copyObjRequest.setNewObjectMetadata(targetObjectMetadata);
        }

        client.copyObject(copyObjRequest);
    } else {
        client.multipartCopyObject(s3Source, s3Target, length, opts);
    }
}

From source file:com.upplication.s3fs.util.S3UploadRequest.java

License:Open Source License

public S3UploadRequest setStorageEncryption(String storageEncryption) {
    if (storageEncryption == null) {
        return this;
    } else if (!"AES256".equals(storageEncryption)) {
        log.warn("Not a valid S3 server-side encryption type: `{}` -- Currently only AES256 is supported",
                storageEncryption);//from   w  w w  .j a v a2  s .  c o  m
    } else {
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
        this.setMetadata(objectMetadata);
    }
    return this;
}

From source file:com.yahoo.ycsb.db.S3Client.java

License:Open Source License

/**
* Upload a new object to S3 or update an object on S3.
*
* @param bucket//w w  w.  j  a v a2  s . co  m
*            The name of the bucket
* @param key
*            The file key of the object to upload/update.
* @param values
*            The data to be written on the object
* @param updateMarker
*            A boolean value. If true a new object will be uploaded
*            to S3. If false an existing object will be re-uploaded
*
*/
protected Status writeToStorage(String bucket, String key, HashMap<String, ByteIterator> values,
        Boolean updateMarker, String sseLocal, SSECustomerKey ssecLocal) {
    int totalSize = 0;
    int fieldCount = values.size(); //number of fields to concatenate
    // getting the first field in the values
    Object keyToSearch = values.keySet().toArray()[0];
    // getting the content of just one field
    byte[] sourceArray = values.get(keyToSearch).toArray();
    int sizeArray = sourceArray.length; //size of each array
    if (updateMarker) {
        totalSize = sizeArray * fieldCount;
    } else {
        try {
            Map.Entry<S3Object, ObjectMetadata> objectAndMetadata = getS3ObjectAndMetadata(bucket, key,
                    ssecLocal);
            int sizeOfFile = (int) objectAndMetadata.getValue().getContentLength();
            fieldCount = sizeOfFile / sizeArray;
            totalSize = sizeOfFile;
            objectAndMetadata.getKey().close();
        } catch (Exception e) {
            System.err.println("Not possible to get the object :" + key);
            e.printStackTrace();
            return Status.ERROR;
        }
    }
    byte[] destinationArray = new byte[totalSize];
    int offset = 0;
    for (int i = 0; i < fieldCount; i++) {
        System.arraycopy(sourceArray, 0, destinationArray, offset, sizeArray);
        offset += sizeArray;
    }
    try (InputStream input = new ByteArrayInputStream(destinationArray)) {
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(totalSize);
        PutObjectRequest putObjectRequest = null;
        if (sseLocal.equals("true")) {
            metadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
            putObjectRequest = new PutObjectRequest(bucket, key, input, metadata);
        } else if (ssecLocal != null) {
            putObjectRequest = new PutObjectRequest(bucket, key, input, metadata).withSSECustomerKey(ssecLocal);
        } else {
            putObjectRequest = new PutObjectRequest(bucket, key, input, metadata);
        }

        try {
            PutObjectResult res = s3Client.putObject(putObjectRequest);
            if (res.getETag() == null) {
                return Status.ERROR;
            } else {
                if (sseLocal.equals("true")) {
                    System.out.println("Uploaded object encryption status is " + res.getSSEAlgorithm());
                } else if (ssecLocal != null) {
                    System.out.println("Uploaded object encryption status is " + res.getSSEAlgorithm());
                }
            }
        } catch (Exception e) {
            System.err.println("Not possible to write object :" + key);
            e.printStackTrace();
            return Status.ERROR;
        }
    } catch (Exception e) {
        System.err.println("Error in the creation of the stream :" + e.toString());
        e.printStackTrace();
        return Status.ERROR;
    }

    return Status.OK;
}

From source file:com.yahoo.ycsb.utils.connection.S3Connection.java

License:Open Source License

public Status insert(String key, byte[] bytes) {
    try (InputStream input = new ByteArrayInputStream(bytes)) {
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(bytes.length);
        PutObjectRequest putObjectRequest = null;
        if (ssecKey != null) {
            if (ssecKey.equals("true")) {
                metadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
                putObjectRequest = new PutObjectRequest(bucket, key, input, metadata);
            } else {
                putObjectRequest = new PutObjectRequest(bucket, key, input, metadata)
                        .withSSECustomerKey(ssecKey);
            }//  ww  w  .  j a v a2s. c  o m
        } else {
            putObjectRequest = new PutObjectRequest(bucket, key, input, metadata);
        }

        try {
            PutObjectResult res = awsClient.putObject(putObjectRequest);
            if (res.getETag() == null) {
                return Status.ERROR;
            } else {
                if (ssecKey != null) {
                    if (ssecKey.equals("true")) {
                        logger.debug("Uploaded object encryption status is " + res.getSSEAlgorithm());
                    } else {
                        logger.debug("Uploaded object encryption status is " + res.getSSEAlgorithm());
                    }
                }
            }
        } catch (Exception e) {
            logger.error("Not possible to write object :" + key);
            System.err.println("Retrying " + key);
            insert(key, bytes);
        }
    } catch (Exception e) {
        logger.error("Error in the creation of the stream :" + e.toString());
        System.err.println("Retrying " + key);
        insert(key, bytes);
        //e.printStackTrace();
        //return Status.ERROR;
    }
    return Status.OK;
}

From source file:com.zero_x_baadf00d.play.module.aws.s3.ebean.BaseS3FileModel.java

License:Open Source License

/**
 * Save the current object. The file will be uploaded to PlayS3 bucket.
 *
 * @since 16.03.13//  w w w  .ja  v  a 2s . c  om
 */
@Override
public void save() {
    if (this.id == null) {
        this.id = Generators.timeBasedGenerator().generate();
    }
    if (!PlayS3.isReady()) {
        Logger.error("Could not save PlayS3 file because amazonS3 variable is null");
        throw new RuntimeException("Could not save");
    } else {
        this.bucket = PlayS3.getBucketName();
        if (this.subDirectory == null) {
            this.subDirectory = "";
        }
        this.subDirectory = this.subDirectory.trim();

        // Set cache control and server side encryption
        final ObjectMetadata objMetaData = new ObjectMetadata();
        objMetaData.setContentType(this.contentType);
        objMetaData.setCacheControl("max-age=315360000, public");
        objMetaData.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
        try {
            objMetaData.setContentLength(this.objectData.available());
        } catch (final IOException ex) {
            Logger.warn("Can't retrieve stream available size", ex);
        } finally {
            try {
                if (this.objectData.markSupported()) {
                    this.objectData.reset();
                }
            } catch (final IOException ex) {
                Logger.error("Can't reset stream position", ex);
            }
        }

        // Upload file to PlayS3
        final PutObjectRequest putObjectRequest = new PutObjectRequest(this.bucket, this.getActualFileName(),
                this.objectData, objMetaData);
        putObjectRequest.withCannedAcl(
                this.isPrivate ? CannedAccessControlList.Private : CannedAccessControlList.PublicRead);

        PlayS3.getAmazonS3().putObject(putObjectRequest);
        try {
            if (this.objectData != null) {
                this.objectData.close();
            }
        } catch (final IOException ignore) {
        }

        // Save object on database
        super.save();
    }
}

From source file:gov.cdc.sdp.cbr.aphl.AphlS3Producer.java

License:Apache License

private ObjectMetadata determineMetadata(final Exchange exchange) {
    ObjectMetadata objectMetadata = new ObjectMetadata();

    Long contentLength = exchange.getIn().getHeader(S3Constants.CONTENT_LENGTH, Long.class);
    if (contentLength != null) {
        objectMetadata.setContentLength(contentLength);
    }//from   www.  j a v a2  s .c  o m

    String contentType = exchange.getIn().getHeader(S3Constants.CONTENT_TYPE, String.class);
    if (contentType != null) {
        objectMetadata.setContentType(contentType);
    }

    String cacheControl = exchange.getIn().getHeader(S3Constants.CACHE_CONTROL, String.class);
    if (cacheControl != null) {
        objectMetadata.setCacheControl(cacheControl);
    }

    String contentDisposition = exchange.getIn().getHeader(S3Constants.CONTENT_DISPOSITION, String.class);
    if (contentDisposition != null) {
        objectMetadata.setContentDisposition(contentDisposition);
    }

    String contentEncoding = exchange.getIn().getHeader(S3Constants.CONTENT_ENCODING, String.class);
    if (contentEncoding != null) {
        objectMetadata.setContentEncoding(contentEncoding);
    }

    String contentMD5 = exchange.getIn().getHeader(S3Constants.CONTENT_MD5, String.class);
    if (contentMD5 != null) {
        objectMetadata.setContentMD5(contentMD5);
    }

    Date lastModified = exchange.getIn().getHeader(S3Constants.LAST_MODIFIED, Date.class);
    if (lastModified != null) {
        objectMetadata.setLastModified(lastModified);
    }

    Map<String, String> userMetadata = CastUtils
            .cast(exchange.getIn().getHeader(S3Constants.USER_METADATA, Map.class));
    if (userMetadata != null) {
        objectMetadata.setUserMetadata(userMetadata);
    }

    Map<String, String> s3Headers = CastUtils
            .cast(exchange.getIn().getHeader(S3Constants.S3_HEADERS, Map.class));
    if (s3Headers != null) {
        for (Map.Entry<String, String> entry : s3Headers.entrySet()) {
            objectMetadata.setHeader(entry.getKey(), entry.getValue());
        }
    }

    String encryption = exchange.getIn().getHeader(S3Constants.SERVER_SIDE_ENCRYPTION,
            getConfiguration().getServerSideEncryption(), String.class);
    if (encryption != null) {
        objectMetadata.setSSEAlgorithm(encryption);
    }

    return objectMetadata;
}

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

License:Open Source License

private ObjectMetadata newObjectMetadata() {
    ObjectMetadata meta = new ObjectMetadata();
    if (StringUtils.isNotBlank(ssea)) {
        meta.setSSEAlgorithm(ssea);
    }/* w  ww.j a  va2s. com*/
    return meta;
}