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

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

Introduction

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

Prototype

public void setSSECustomerKey(SSECustomerKey sseKey) 

Source Link

Document

Sets the optional customer-provided server-side encryption key to use to encrypt the uploaded object.

Usage

From source file:org.alanwilliamson.amazon.s3.BackgroundUploader.java

License:Open Source License

private void uploadFile(Map<String, Object> jobFile) {

    File localFile = new File((String) jobFile.get("localpath"));
    if (!localFile.isFile()) {
        removeJobFile(jobFile);/*from w  w  w. j a v a 2 s .c o m*/
        callbackCfc(jobFile, false, "local file no longer exists");
        cfEngine.log("AmazonS3Write.BackgroundUploader: file no longer exists=" + localFile.getName());
        return;
    }

    // Setup the object data
    ObjectMetadata omd = new ObjectMetadata();
    if (jobFile.containsKey("metadata"))
        omd.setUserMetadata((Map<String, String>) jobFile.get("metadata"));

    TransferManager tm = null;
    AmazonS3 s3Client = null;
    try {
        AmazonKey amazonKey = (AmazonKey) jobFile.get("amazonkey");
        s3Client = new AmazonBase().getAmazonS3(amazonKey);

        PutObjectRequest por = new PutObjectRequest((String) jobFile.get("bucket"), (String) jobFile.get("key"),
                localFile);
        por.setMetadata(omd);
        por.setStorageClass((StorageClass) jobFile.get("storage"));

        if (jobFile.containsKey("acl"))
            por.setCannedAcl(amazonKey.getAmazonCannedAcl((String) jobFile.get("acl")));

        if (jobFile.containsKey("aes256key"))
            por.setSSECustomerKey(new SSECustomerKey((String) jobFile.get("aes256key")));

        if (jobFile.containsKey("customheaders")) {
            Map<String, String> customheaders = (Map) jobFile.get("customheaders");

            Iterator<String> it = customheaders.keySet().iterator();
            while (it.hasNext()) {
                String k = it.next();
                por.putCustomRequestHeader(k, customheaders.get(k));
            }
        }

        long startTime = System.currentTimeMillis();
        tm = new TransferManager(s3Client);
        Upload upload = tm.upload(por);
        upload.waitForCompletion();

        log(jobFile, "Uploaded; timems=" + (System.currentTimeMillis() - startTime));

        removeJobFile(jobFile);
        callbackCfc(jobFile, true, null);

        if ((Boolean) jobFile.get("deletefile"))
            localFile.delete();

    } catch (Exception e) {
        log(jobFile, "Failed=" + e.getMessage());

        callbackCfc(jobFile, false, e.getMessage());

        int retry = (Integer) jobFile.get("retry");
        int attempt = (Integer) jobFile.get("attempt") + 1;

        if (retry == attempt) {
            removeJobFile(jobFile);
        } else {
            jobFile.put("attempt", attempt);
            jobFile.put("attemptdate", System.currentTimeMillis() + (Long) jobFile.get("retryms"));
            acceptFile(jobFile);
        }

        if (s3Client != null)
            cleanupMultiPartUploads(s3Client, (String) jobFile.get("bucket"));

    } finally {
        if (tm != null)
            tm.shutdownNow(true);
    }

}

From source file:org.alanwilliamson.amazon.s3.Write.java

License:Open Source License

private void writeData(AmazonKey amazonKey, String bucket, String key, Map<String, String> metadata,
        StorageClass storage, String mimetype, cfData data, int retry, int retryseconds, String acl,
        String aes256key, Map<String, String> customheaders) throws Exception {
    if (mimetype == null) {
        if (data.getDataType() == cfData.CFBINARYDATA)
            mimetype = "application/unknown";
        else if (cfData.isSimpleValue(data))
            mimetype = "text/plain";
        else/*  w  w  w .jav a 2  s.  c  o  m*/
            mimetype = "application/json";

        // Check to see if the mime type is in the metadata
        if (metadata != null && metadata.containsKey("Content-Type"))
            mimetype = metadata.get("Content-Type");
    }

    InputStream ios = null;
    long size = 0;
    if (data.getDataType() == cfData.CFSTRINGDATA) {
        ios = new java.io.ByteArrayInputStream(data.getString().getBytes());
        size = data.getString().length();
    } else if (data.getDataType() == cfData.CFBINARYDATA) {
        ios = new java.io.ByteArrayInputStream(((cfBinaryData) data).getByteArray());
        size = ((cfBinaryData) data).getLength();
    } else {
        serializejson json = new serializejson();
        StringBuilder out = new StringBuilder();
        json.encodeJSON(out, data, false, CaseType.MAINTAIN, DateType.LONG);
        size = out.length();
        mimetype = "application/json";
        ios = new java.io.ByteArrayInputStream(out.toString().getBytes());
    }

    // Setup the object data
    ObjectMetadata omd = new ObjectMetadata();
    if (metadata != null)
        omd.setUserMetadata(metadata);

    omd.setContentType(mimetype);
    omd.setContentLength(size);

    AmazonS3 s3Client = getAmazonS3(amazonKey);

    // Let us run around the number of attempts
    int attempts = 0;
    while (attempts < retry) {
        try {

            PutObjectRequest por = new PutObjectRequest(bucket, key, ios, omd);
            por.setStorageClass(storage);

            if (aes256key != null && !aes256key.isEmpty())
                por.setSSECustomerKey(new SSECustomerKey(aes256key));

            if (acl != null && !acl.isEmpty())
                por.setCannedAcl(amazonKey.getAmazonCannedAcl(acl));

            if (customheaders != null && !customheaders.isEmpty()) {
                Iterator<String> it = customheaders.keySet().iterator();
                while (it.hasNext()) {
                    String k = it.next();
                    por.putCustomRequestHeader(k, customheaders.get(k));
                }
            }

            s3Client.putObject(por);
            break;

        } catch (Exception e) {
            cfEngine.log("Failed: AmazonS3Write(bucket=" + bucket + "; key=" + key + "; attempt="
                    + (attempts + 1) + "; exception=" + e.getMessage() + ")");
            attempts++;

            if (attempts == retry)
                throw e;
            else
                Thread.sleep(retryseconds * 1000);
        }
    }
}

From source file:org.alanwilliamson.amazon.s3.Write.java

License:Open Source License

private void writeFile(AmazonKey amazonKey, String bucket, String key, Map<String, String> metadata,
        StorageClass storage, String localpath, int retry, int retryseconds, boolean deletefile,
        boolean background, String callback, String callbackdata, String appname, String acl, String aes256key,
        Map<String, String> customheaders) throws Exception {
    File localFile = new File(localpath);
    if (!localFile.isFile())
        throw new Exception("The file specified does not exist: " + localpath);

    // Push this to the background loader to handle and return immediately
    if (background) {
        BackgroundUploader.acceptFile(amazonKey, bucket, key, metadata, storage, localpath, retry, retryseconds,
                deletefile, callback, callbackdata, appname, acl, aes256key, customheaders);
        return;//from   w w w  .jav a2  s . c  o  m
    }

    // Setup the object data
    ObjectMetadata omd = new ObjectMetadata();
    if (metadata != null)
        omd.setUserMetadata(metadata);

    AmazonS3 s3Client = getAmazonS3(amazonKey);

    // Let us run around the number of attempts
    int attempts = 0;
    while (attempts < retry) {
        try {

            PutObjectRequest por = new PutObjectRequest(bucket, key, localFile);
            por.setMetadata(omd);
            por.setStorageClass(storage);

            if (acl != null && !acl.isEmpty())
                por.setCannedAcl(amazonKey.getAmazonCannedAcl(acl));

            if (aes256key != null && !aes256key.isEmpty())
                por.setSSECustomerKey(new SSECustomerKey(aes256key));

            if (customheaders != null && !customheaders.isEmpty()) {
                Iterator<String> it = customheaders.keySet().iterator();
                while (it.hasNext()) {
                    String k = it.next();
                    por.putCustomRequestHeader(k, customheaders.get(k));
                }
            }

            s3Client.putObject(por);
            break;

        } catch (Exception e) {
            cfEngine.log("Failed: AmazonS3Write(bucket=" + bucket + "key=" + key + "; file=" + localFile
                    + "; attempt=" + (attempts + 1) + "; exception=" + e.getMessage() + ")");
            attempts++;

            if (attempts == retry)
                throw e;
            else
                Thread.sleep(retryseconds * 1000);
        }
    }

    // delete the file now that it is a success
    if (deletefile)
        localFile.delete();
}

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

License:Apache License

@Override
public void configurePutObjectRequest(PutObjectRequest request, ObjectMetadata objectMetadata,
        String keyValue) {//from   ww  w  .j  av  a 2s.  co  m
    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(PutObjectRequest putObjectRequest) {
    if (encryptionMethod == null)
        return;//w  w w .  j a v a 2 s  .  c o  m

    if (encryptionMethod.equals(METHOD_SSE_S3)) {
        getLogger().info("Encrypting single part object using SSE-S3");
        putObjectRequest.getMetadata()
                .setSSEAlgorithm(algorithm == null ? ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION : algorithm);
    }

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

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

        String sseCustomerAlgorithm = customerAlgorithm == null ? ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION
                : customerAlgorithm;
        putObjectRequest.getMetadata().setSSECustomerAlgorithm(sseCustomerAlgorithm);

        if (StringUtils.isNotBlank(customerKeyMD5)) {
            putObjectRequest.getMetadata().setSSECustomerKeyMd5(customerKeyMD5);
        }
    }
}