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

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

Introduction

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

Prototype

public void setSSECustomerKey(SSECustomerKey sseKey) 

Source Link

Document

Sets the optional customer-provided server-side encryption key to use to decrypt this object.

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
 * // w  w w  . j av  a 2s .com
 * @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: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  ww.j av  a 2 s. c  o  m*/
            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 {/* www.  j  a v  a  2  s  .com*/

            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.apache.beam.sdk.io.aws.s3.S3ReadableSeekableByteChannel.java

License:Apache License

@Override
public int read(ByteBuffer destinationBuffer) throws IOException {
    if (!isOpen()) {
        throw new ClosedChannelException();
    }//from   w w  w .  jav a2 s . co m
    if (!destinationBuffer.hasRemaining()) {
        return 0;
    }
    if (position == contentLength) {
        return -1;
    }

    if (s3Object == null) {
        GetObjectRequest request = new GetObjectRequest(path.getBucket(), path.getKey());
        request.setSSECustomerKey(options.getSSECustomerKey());
        if (position > 0) {
            request.setRange(position, contentLength);
        }
        try {
            s3Object = amazonS3.getObject(request);
        } catch (AmazonClientException e) {
            throw new IOException(e);
        }
        s3ObjectContentChannel = Channels
                .newChannel(new BufferedInputStream(s3Object.getObjectContent(), 1024 * 1024));
    }

    int totalBytesRead = 0;
    int bytesRead = 0;

    do {
        totalBytesRead += bytesRead;
        try {
            bytesRead = s3ObjectContentChannel.read(destinationBuffer);
        } catch (AmazonClientException e) {
            // TODO replace all catch AmazonServiceException with client exception
            throw new IOException(e);
        }
    } while (bytesRead > 0);

    position += totalBytesRead;
    return totalBytesRead;
}

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

License:Apache License

@Override
public void configureGetObjectRequest(GetObjectRequest request, ObjectMetadata objectMetadata,
        String keyValue) {/*from w w w . j  a va 2s .  c o m*/
    SSECustomerKey customerKey = new SSECustomerKey(keyValue);
    request.setSSECustomerKey(customerKey);
}