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

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

Introduction

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

Prototype

public void setExpirationTime(Date expirationTime) 

Source Link

Document

For internal use only.

Usage

From source file:org.apache.streams.s3.S3OutputStreamWrapper.java

License:Apache License

private void addFile() throws Exception {

    InputStream is = new ByteArrayInputStream(this.outputStream.toByteArray());
    int contentLength = outputStream.size();

    TransferManager transferManager = new TransferManager(amazonS3Client);
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setExpirationTime(DateTime.now().plusDays(365 * 3).toDate());
    metadata.setContentLength(contentLength);

    metadata.addUserMetadata("writer", "org.apache.streams");

    for (String s : metaData.keySet())
        metadata.addUserMetadata(s, metaData.get(s));

    String fileNameToWrite = path + fileName;
    Upload upload = transferManager.upload(bucketName, fileNameToWrite, is, metadata);
    try {/*from www.  j  a  v a  2 s  .  co m*/
        upload.waitForUploadResult();

        is.close();
        transferManager.shutdownNow(false);
        LOGGER.info("S3 File Close[{} kb] - {}", contentLength / 1024, path + fileName);
    } catch (Exception e) {
        // No Op
    }

}

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  w  ww .j a  v a2s  .  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();
    }
}