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

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

Introduction

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

Prototype

String AES_256_SERVER_SIDE_ENCRYPTION

To view the source code for com.amazonaws.services.s3.model ObjectMetadata AES_256_SERVER_SIDE_ENCRYPTION.

Click Source Link

Usage

From source file:sample.S3EmitterWithMetadata.java

License:Open Source License

@Override
public List<byte[]> emit(final UnmodifiableBuffer<byte[]> buffer) throws IOException {
    List<byte[]> records = buffer.getRecords();
    // Write all of the records to a compressed output stream
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    for (byte[] record : records) {
        try {/*from   ww w.  ja  v  a2  s. co m*/
            baos.write(record);
        } catch (Exception e) {
            LOG.error("Error writing record to output stream. Failing this emit attempt. Record: "
                    + Arrays.toString(record), e);
            return buffer.getRecords();
        }
    }
    // Get the Amazon S3 filename
    String s3FileName = getS3FileName(buffer.getFirstSequenceNumber(), buffer.getLastSequenceNumber());
    String s3URI = getS3URI(s3FileName);
    try {
        ByteArrayInputStream object = new ByteArrayInputStream(baos.toByteArray());
        LOG.debug("Starting upload of file " + s3URI + " to Amazon S3 containing " + records.size()
                + " records.");
        ObjectMetadata meta = new ObjectMetadata();
        Date date = new Date();
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 14);
        meta.setExpirationTime(calendar.getTime());
        meta.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
        meta.setContentLength(baos.size());
        s3client.putObject(s3Bucket, s3FileName, object, meta);
        LOG.info("Successfully emitted " + buffer.getRecords().size() + " records to Amazon S3 in " + s3URI);
        return Collections.emptyList();
    } catch (Exception e) {
        LOG.error("Caught exception when uploading file " + s3URI + "to Amazon S3. Failing this emit attempt.",
                e);
        return buffer.getRecords();
    }
}