Example usage for com.amazonaws.services.s3.model SSECustomerKey SSECustomerKey

List of usage examples for com.amazonaws.services.s3.model SSECustomerKey SSECustomerKey

Introduction

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

Prototype

public SSECustomerKey(SecretKey key) 

Source Link

Document

Constructs a new customer provided server-side encryption key using the specified SecretKey.

Usage

From source file:com.naryx.tagfusion.cfm.tag.cfCONTENT.java

License:Open Source License

/**
 * Fetchs a remote object from S3; datasource, bucket, key, aes256key supported
 * /*from w  w  w.  jav  a  2s .  co m*/
 * @param props
 * @param _Session
 * @throws cfmRunTimeException
 */
private void remoteFetchS3(cfStructData props, cfSession _Session) throws cfmRunTimeException {

    if (!props.containsKey("datasource") || !props.containsKey("bucket") || !props.containsKey("key"))
        throw newRunTimeException("'remote'.type=s3; minimum keys are datasource, bucket and key");

    String datasource = props.getData("datasource").getString();
    String bucket = props.getData("bucket").getString();
    String key = props.getData("key").getString();

    // Get the Amazon datasource
    AmazonKey amazonKey = AmazonKeyFactory.getDS(datasource);
    if (amazonKey == null)
        throw newRunTimeException("Amazon Datasource [" + datasource
                + "] has not been registered; use AmazonRegisterDataSource()");

    amazonKey.setDataSource(datasource);

    AmazonS3 s3Client = new AmazonBase().getAmazonS3(amazonKey);

    GetObjectRequest gor = new GetObjectRequest(bucket, key);
    if (props.containsKey("aes256key")) {
        String aes256key = props.getData("aes256key").getString();

        if (!aes256key.isEmpty())
            gor.setSSECustomerKey(new SSECustomerKey(aes256key));
    }

    // Get the object
    try {

        S3Object s3object = s3Client.getObject(gor);

        _Session.setContentType(s3object.getObjectMetadata().getContentType());

        InputStream in = s3object.getObjectContent();

        byte[] buffer = new byte[65536];
        int readCount = 0;

        while ((readCount = in.read(buffer)) != -1) {
            _Session.write(buffer, 0, readCount);
            _Session.pageFlush();
        }

    } catch (Exception e) {

        if (e.getMessage().indexOf("404") != -1) {
            _Session.setStatus(404);
            return;
        } else {
            cfEngine.log(e.getMessage());
            throw newRunTimeException(e.getMessage() + "; key=" + key + "; bucket=" + bucket);
        }
    }
}

From source file:com.pinterest.secor.uploader.S3UploadManager.java

License:Apache License

private void enableCustomerEncryption(PutObjectRequest uploadRequest) {
    SSECustomerKey sseKey = new SSECustomerKey(mConfig.getAwsSseCustomerKey());
    uploadRequest.withSSECustomerKey(sseKey);
}

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

License:Open Source License

/**
* Initialize any state for the storage.//from  ww w . j a  v a2 s . c  om
* Called once per S3 instance; If the client is not null it is re-used.
*/
@Override
public void init() throws DBException {
    final int count = INIT_COUNT.incrementAndGet();
    synchronized (S3Client.class) {
        Properties propsCL = getProperties();
        int recordcount = Integer.parseInt(propsCL.getProperty("recordcount"));
        int operationcount = Integer.parseInt(propsCL.getProperty("operationcount"));
        int numberOfOperations = 0;
        if (recordcount > 0) {
            if (recordcount > operationcount) {
                numberOfOperations = recordcount;
            } else {
                numberOfOperations = operationcount;
            }
        } else {
            numberOfOperations = operationcount;
        }
        if (count <= numberOfOperations) {
            String accessKeyId = null;
            String secretKey = null;
            String endPoint = null;
            String region = null;
            String maxErrorRetry = null;
            String maxConnections = null;
            String protocol = null;
            BasicAWSCredentials s3Credentials;
            ClientConfiguration clientConfig;
            if (s3Client != null) {
                System.out.println("Reusing the same client");
                return;
            }
            try {
                InputStream propFile = S3Client.class.getClassLoader().getResourceAsStream("s3.properties");
                Properties props = new Properties(System.getProperties());
                props.load(propFile);
                accessKeyId = props.getProperty("s3.accessKeyId");
                if (accessKeyId == null) {
                    accessKeyId = propsCL.getProperty("s3.accessKeyId");
                }
                System.out.println(accessKeyId);
                secretKey = props.getProperty("s3.secretKey");
                if (secretKey == null) {
                    secretKey = propsCL.getProperty("s3.secretKey");
                }
                System.out.println(secretKey);
                endPoint = props.getProperty("s3.endPoint");
                if (endPoint == null) {
                    endPoint = propsCL.getProperty("s3.endPoint", "s3.amazonaws.com");
                }
                System.out.println(endPoint);
                region = props.getProperty("s3.region");
                if (region == null) {
                    region = propsCL.getProperty("s3.region", "us-east-1");
                }
                System.out.println(region);
                maxErrorRetry = props.getProperty("s3.maxErrorRetry");
                if (maxErrorRetry == null) {
                    maxErrorRetry = propsCL.getProperty("s3.maxErrorRetry", "15");
                }
                maxConnections = props.getProperty("s3.maxConnections");
                if (maxConnections == null) {
                    maxConnections = propsCL.getProperty("s3.maxConnections");
                }
                protocol = props.getProperty("s3.protocol");
                if (protocol == null) {
                    protocol = propsCL.getProperty("s3.protocol", "HTTPS");
                }
                sse = props.getProperty("s3.sse");
                if (sse == null) {
                    sse = propsCL.getProperty("s3.sse", "false");
                }
                String ssec = props.getProperty("s3.ssec");
                if (ssec == null) {
                    ssec = propsCL.getProperty("s3.ssec", null);
                } else {
                    ssecKey = new SSECustomerKey(ssec);
                }
            } catch (Exception e) {
                System.err.println("The file properties doesn't exist " + e.toString());
                e.printStackTrace();
            }
            try {
                System.out.println("Inizializing the S3 connection");
                s3Credentials = new BasicAWSCredentials(accessKeyId, secretKey);
                clientConfig = new ClientConfiguration();
                clientConfig.setMaxErrorRetry(Integer.parseInt(maxErrorRetry));
                if (protocol.equals("HTTP")) {
                    clientConfig.setProtocol(Protocol.HTTP);
                } else {
                    clientConfig.setProtocol(Protocol.HTTPS);
                }
                if (maxConnections != null) {
                    clientConfig.setMaxConnections(Integer.parseInt(maxConnections));
                }
                s3Client = new AmazonS3Client(s3Credentials, clientConfig);
                s3Client.setRegion(Region.getRegion(Regions.fromName(region)));
                s3Client.setEndpoint(endPoint);
                System.out.println("Connection successfully initialized");
            } catch (Exception e) {
                System.err.println("Could not connect to S3 storage: " + e.toString());
                e.printStackTrace();
                throw new DBException(e);
            }
        } else {
            System.err.println("The number of threads must be less or equal than the operations");
            throw new DBException(new Error("The number of threads must be less or equal than the operations"));
        }
    }
}

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

License:Open Source License

public S3OutputStream(String key, S3SinkConnectorConfig conf, AmazonS3 s3) {
    this.s3 = s3;
    this.bucket = conf.getBucketName();
    this.key = key;
    this.ssea = conf.getSsea();
    final String sseCustomerKeyConfig = conf.getSseCustomerKey();
    this.sseCustomerKey = (SSEAlgorithm.AES256.toString().equalsIgnoreCase(ssea)
            && StringUtils.isNotBlank(sseCustomerKeyConfig)) ? new SSECustomerKey(sseCustomerKeyConfig) : null;
    this.sseKmsKeyId = conf.getSseKmsKeyId();
    this.partSize = conf.getPartSize();
    this.cannedAcl = conf.getCannedAcl();
    this.closed = false;
    this.buffer = ByteBuffer.allocate(this.partSize);
    this.progressListener = new ConnectProgressListener();
    this.multiPartUpload = null;
    this.compressionType = conf.getCompressionType();
    log.debug("Create S3OutputStream for bucket '{}' key '{}'", bucket, key);
}

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

License:Apache License

@JsonCreator
CustomServerSideEncryption(@JacksonInject S3SSECustomConfig config) {
    this.key = new SSECustomerKey(config.getBase64EncodedKey());
}

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 www .j a v a  2 s  .co  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.Copy.java

License:Open Source License

public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException {

    AmazonKey amazonKey = getAmazonKey(_session, argStruct);
    AmazonS3 s3Client = getAmazonS3(amazonKey);

    String srcbucket = getNamedStringParam(argStruct, "srcbucket", null);
    String srckey = getNamedStringParam(argStruct, "srckey", null);
    String srcaes256key = getNamedStringParam(argStruct, "srcaes256key", null);

    String destbucket = getNamedStringParam(argStruct, "destbucket", null);
    String deskey = getNamedStringParam(argStruct, "destkey", null);
    String destaes256key = getNamedStringParam(argStruct, "destaes256key", null);
    String deststorageclass = getNamedStringParam(argStruct, "deststorageclass", null);
    String destacl = getNamedStringParam(argStruct, "destacl", null);

    if (srckey != null && srckey.charAt(0) == '/')
        srckey = srckey.substring(1);/*  ww w .  ja  va 2s . c o  m*/

    if (deskey != null && deskey.charAt(0) == '/')
        deskey = deskey.substring(1);

    CopyObjectRequest cor = new CopyObjectRequest(srcbucket, srckey, destbucket, deskey);

    if (srcaes256key != null && !srcaes256key.isEmpty())
        cor.setSourceSSECustomerKey(new SSECustomerKey(srcaes256key));

    if (destaes256key != null && !destaes256key.isEmpty())
        cor.setDestinationSSECustomerKey(new SSECustomerKey(destaes256key));

    if (deststorageclass != null && !deststorageclass.isEmpty())
        cor.setStorageClass(amazonKey.getAmazonStorageClass(deststorageclass));

    if (destacl != null && !destacl.isEmpty())
        cor.setCannedAccessControlList(amazonKey.getAmazonCannedAcl(destacl));

    try {
        s3Client.copyObject(cor);
        return cfBooleanData.TRUE;
    } catch (Exception e) {
        throwException(_session, "AmazonS3: " + e.getMessage());
        return cfBooleanData.FALSE;
    }
}

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

License:Open Source License

private cfData readToFile(AmazonS3 s3Client, String bucket, String key, String localpath, boolean overwrite,
        String aes256key, int retry, int retryseconds) throws Exception {
    File localFile = new File(localpath);
    if (localFile.isFile()) {
        if (!overwrite)
            throw new Exception("The file specified exists: " + localpath);
        else//from w w  w  . j  a  v a 2s .c  om
            localFile.delete();
    }

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

            GetObjectRequest gor = new GetObjectRequest(bucket, key);
            if (aes256key != null && !aes256key.isEmpty())
                gor.setSSECustomerKey(new SSECustomerKey(aes256key));

            S3Object s3object = s3Client.getObject(gor);

            FileOutputStream outStream = null;
            try {
                outStream = new FileOutputStream(localFile);
                StreamUtil.copyTo(s3object.getObjectContent(), outStream, false);
            } finally {
                StreamUtil.closeStream(outStream);
            }

            return new cfStringData(localFile.toString());

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

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

    return null; // should never             
}

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

License:Open Source License

private cfData readToMemory(AmazonS3 s3Client, String bucket, String key, String aes256key, int retry,
        int retryseconds) throws Exception {

    // Let us run around the number of attempts
    int attempts = 0;
    while (attempts < retry) {
        try {/* w w w . ja  v a2 s. c o m*/

            GetObjectRequest gor = new GetObjectRequest(bucket, key);
            if (aes256key != null && !aes256key.isEmpty())
                gor.setSSECustomerKey(new SSECustomerKey(aes256key));

            S3Object s3object = s3Client.getObject(gor);
            String contentType = s3object.getObjectMetadata().getContentType();

            ByteArrayOutputStream baos = new ByteArrayOutputStream(32000);
            StreamUtil.copyTo(s3object.getObjectContent(), baos, false);

            if (contentType.indexOf("text") != -1 || contentType.indexOf("javascript") != -1) {
                return new cfStringData(baos.toString());
            } else {
                return new cfBinaryData(baos.toByteArray());
            }

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

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

    return null; // should never 
}

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

License:Open Source License

public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException {
    AmazonKey amazonKey = getAmazonKey(_session, argStruct);
    AmazonS3 s3Client = getAmazonS3(amazonKey);

    String bucket = getNamedStringParam(argStruct, "bucket", null);
    String srckey = getNamedStringParam(argStruct, "srckey", null);
    String deskey = getNamedStringParam(argStruct, "destkey", null);
    String aes256key = getNamedStringParam(argStruct, "aes256key", null);

    if (srckey != null && srckey.charAt(0) == '/')
        srckey = srckey.substring(1);/* ww  w  .j a v  a 2 s. c  o m*/

    if (deskey != null && deskey.charAt(0) == '/')
        deskey = deskey.substring(1);

    CopyObjectRequest cor = new CopyObjectRequest(bucket, srckey, bucket, deskey);

    if (aes256key != null && !aes256key.isEmpty()) {
        cor.setSourceSSECustomerKey(new SSECustomerKey(aes256key));
        cor.setDestinationSSECustomerKey(new SSECustomerKey(aes256key));
    }

    try {
        s3Client.copyObject(cor);
        s3Client.deleteObject(new DeleteObjectRequest(bucket, srckey));
        return cfBooleanData.TRUE;
    } catch (Exception e) {
        throwException(_session, "AmazonS3: " + e.getMessage());
        return cfBooleanData.FALSE;
    }
}