Example usage for com.amazonaws.services.s3.model S3Object getBucketName

List of usage examples for com.amazonaws.services.s3.model S3Object getBucketName

Introduction

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

Prototype

public String getBucketName() 

Source Link

Document

Gets the name of the bucket in which this object is contained.

Usage

From source file:com.emc.vipr.s3.sample._02_ReadObject.java

License:Open Source License

public static void main(String[] args) throws Exception {
    // create the ViPR S3 Client
    ViPRS3Client s3 = ViPRS3Factory.getS3Client();

    // retrieve the key value from user
    System.out.println("Enter the object key:");
    String key = new BufferedReader(new InputStreamReader(System.in)).readLine();

    // read the object from the demo bucket
    S3Object object = s3.getObject(ViPRS3Factory.S3_BUCKET, key);

    // convert object to a text string
    BufferedReader reader = new BufferedReader(new InputStreamReader(object.getObjectContent()));
    String content = reader.readLine();

    // print object key/value and content for validation
    System.out.println(/*from   w  w w.jav a2s.  c o m*/
            String.format("object [%s/%s] content: [%s]", object.getBucketName(), object.getKey(), content));
}

From source file:com.smoketurner.pipeline.application.core.AmazonS3Downloader.java

License:Apache License

/**
 * Retrieves a file from S3//from  ww  w  .j av  a2 s  .co  m
 *
 * @param record
 *            S3 event notification record to download
 * @return S3 object
 * @throws AmazonS3ConstraintException
 *             if the etag constraints weren't met
 * @throws AmazonS3ZeroSizeException
 *             if the file size of the object is zero
 */
public S3Object fetch(@Nonnull final S3EventNotificationRecord record)
        throws AmazonS3ConstraintException, AmazonS3ZeroSizeException {
    final AmazonS3Object object = converter.convert(Objects.requireNonNull(record));

    final GetObjectRequest request = new GetObjectRequest(object.getBucketName(), object.getKey());
    object.getVersionId().ifPresent(request::setVersionId);
    object.getETag().ifPresent(etag -> request.setMatchingETagConstraints(Collections.singletonList(etag)));

    LOGGER.debug("Fetching key: {}/{}", object.getBucketName(), object.getKey());

    final S3Object download;
    try {
        download = s3.getObject(request);
    } catch (AmazonServiceException e) {
        LOGGER.error("Service error while fetching object from S3", e);
        throw e;
    } catch (AmazonClientException e) {
        LOGGER.error("Client error while fetching object from S3", e);
        throw e;
    }

    if (download == null) {
        LOGGER.error("eTag from object did not match for key: {}/{}", object.getBucketName(), object.getKey());
        throw new AmazonS3ConstraintException(object.getKey());
    }

    final long contentLength = download.getObjectMetadata().getContentLength();
    if (contentLength < 1) {
        try {
            download.close();
        } catch (IOException e) {
            LOGGER.error(String.format("Failed to close S3 stream for key: %s/%s", download.getBucketName(),
                    download.getKey()), e);
        }

        LOGGER.debug("Object size is zero for key: {}/{}", download.getBucketName(), download.getKey());
        throw new AmazonS3ZeroSizeException(object.getKey());
    }

    LOGGER.debug("Streaming key ({} bytes): {}/{}", contentLength, download.getBucketName(), download.getKey());

    return download;
}

From source file:com.smoketurner.pipeline.application.core.MessageProcessor.java

License:Apache License

/**
 * Process an S3 event notification record by streaming object in
 * {@link streamObject}/*ww  w  .  j a  v a 2 s. c o  m*/
 * 
 * @param record
 *            S3 event notification record
 * @return true if the record was fully processed, otherwise false
 */
private boolean processRecord(@Nonnull final S3EventNotificationRecord record) {
    LOGGER.trace("Event Record: {}", record);

    final S3Object download;
    try {
        download = s3.fetch(record);
    } catch (AmazonS3ConstraintException | AmazonS3ZeroSizeException e) {
        LOGGER.error("Unable to download file from S3, skipping to next record", e);
        return true;
    } catch (AmazonS3Exception e) {
        if (e.getStatusCode() == 404) {
            LOGGER.warn("File does not exist in S3, skipping to next record", e);
            return true;
        }
        LOGGER.error("Amazon S3 exception, skipping remaining records", e);
        return false;
    } catch (Exception e) {
        LOGGER.error("Failed to download file from S3, skipping remaining records", e);
        return false;
    }

    final int eventCount;
    try {
        eventCount = streamObject(download);
    } catch (IOException e) {
        LOGGER.error(String.format("Error streaming key: %s/%s", download.getBucketName(), download.getKey()),
                e);
        return false;
    }

    eventCounts.update(eventCount);

    LOGGER.debug("Broadcast {} events from key: {}/{}", eventCount, download.getBucketName(),
            download.getKey());
    return true;
}

From source file:com.smoketurner.pipeline.application.core.MessageProcessor.java

License:Apache License

/**
 * Stream an {@link S3Object} object and process each line with the
 * processor./*from   w w  w .  j av a2  s  .com*/
 * 
 * @param object
 *            S3Object to download and process
 * @return number of events processed
 * @throws IOException
 *             if unable to stream the object
 */
private int streamObject(@Nonnull final S3Object object) throws IOException {

    final AtomicInteger eventCount = new AtomicInteger(0);
    try (S3ObjectInputStream input = object.getObjectContent()) {

        final BufferedReader reader;
        if (AmazonS3Downloader.isGZipped(object)) {
            reader = new BufferedReader(
                    new InputStreamReader(new StreamingGZIPInputStream(input), StandardCharsets.UTF_8));
        } else {
            reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8));
        }

        // failed will be true if we did not successfully broadcast all
        // of the events because of no consumers
        final boolean failed = reader.lines().peek(event -> eventCount.incrementAndGet())
                .anyMatch(broadcaster::test);

        if (failed) {
            // abort the current S3 download
            input.abort();
            LOGGER.error("Partial events broadcast ({} sent) from key: {}/{}", eventCount.get(),
                    object.getBucketName(), object.getKey());
            throw new IOException("aborting download");
        }
    }
    return eventCount.get();
}

From source file:edu.si.services.beans.cameratrap.AmazonS3ClientMock.java

License:Apache License

@Override
public S3Object getObject(String bucketName, String key) throws AmazonClientException, AmazonServiceException {
    for (S3Object s3Object : objects) {
        if (bucketName.equals(s3Object.getBucketName()) && key.equals(s3Object.getKey())) {
            return s3Object;
        }//from   w ww .j a  v a  2  s.  c o m
    }

    return null;
}

From source file:eu.openg.aws.s3.test.error.ShouldHaveBucketName.java

License:Apache License

private ShouldHaveBucketName(S3Object actual, String expected) {
    super("%nExpecting bucketName of%n  <%s>%nto be:%n  <%s>%nbut was:%n  <%s>", actual, expected,
            actual.getBucketName());
}

From source file:io.konig.camel.aws.s3.DeleteObjectEndpoint.java

License:Apache License

public Exchange createExchange(ExchangePattern pattern, final S3Object s3Object) {
    LOG.trace("Getting object with key [{}] from bucket [{}]...", s3Object.getKey(), s3Object.getBucketName());

    ObjectMetadata objectMetadata = s3Object.getObjectMetadata();

    LOG.trace("Got object [{}]", s3Object);

    Exchange exchange = super.createExchange(pattern);
    Message message = exchange.getIn();/* w w  w  . j  av  a2 s.c o  m*/

    if (configuration.isIncludeBody()) {
        message.setBody(s3Object.getObjectContent());
    } else {
        message.setBody(null);
    }

    message.setHeader(S3Constants.KEY, s3Object.getKey());
    message.setHeader(S3Constants.BUCKET_NAME, s3Object.getBucketName());
    message.setHeader(S3Constants.E_TAG, objectMetadata.getETag());
    message.setHeader(S3Constants.LAST_MODIFIED, objectMetadata.getLastModified());
    message.setHeader(S3Constants.VERSION_ID, objectMetadata.getVersionId());
    message.setHeader(S3Constants.CONTENT_TYPE, objectMetadata.getContentType());
    message.setHeader(S3Constants.CONTENT_MD5, objectMetadata.getContentMD5());
    message.setHeader(S3Constants.CONTENT_LENGTH, objectMetadata.getContentLength());
    message.setHeader(S3Constants.CONTENT_ENCODING, objectMetadata.getContentEncoding());
    message.setHeader(S3Constants.CONTENT_DISPOSITION, objectMetadata.getContentDisposition());
    message.setHeader(S3Constants.CACHE_CONTROL, objectMetadata.getCacheControl());
    message.setHeader(S3Constants.S3_HEADERS, objectMetadata.getRawMetadata());
    message.setHeader(S3Constants.SERVER_SIDE_ENCRYPTION, objectMetadata.getSSEAlgorithm());
    message.setHeader(S3Constants.USER_METADATA, objectMetadata.getUserMetadata());
    message.setHeader(S3Constants.EXPIRATION_TIME, objectMetadata.getExpirationTime());
    message.setHeader(S3Constants.REPLICATION_STATUS, objectMetadata.getReplicationStatus());
    message.setHeader(S3Constants.STORAGE_CLASS, objectMetadata.getStorageClass());

    /**
    * If includeBody != true, it is safe to close the object here. If
    * includeBody == true, the caller is responsible for closing the stream
    * and object once the body has been fully consumed. As of 2.17, the
    * consumer does not close the stream or object on commit.
    */
    if (!configuration.isIncludeBody()) {
        IOHelper.close(s3Object);
    } else {
        if (configuration.isAutocloseBody()) {
            exchange.addOnCompletion(new SynchronizationAdapter() {
                @Override
                public void onDone(Exchange exchange) {
                    IOHelper.close(s3Object);
                }
            });
        }
    }

    return exchange;
}

From source file:nl.nn.adapterframework.filesystem.AmazonS3FileSystem.java

License:Apache License

@Override
public String getCanonicalName(S3Object f) throws FileSystemException {
    return f.getBucketName() + f.getKey();
}

From source file:org.apache.camel.component.aws.s3.S3Endpoint.java

License:Apache License

public Exchange createExchange(ExchangePattern pattern, S3Object s3Object) {
    LOG.trace("Getting object with key [{}] from bucket [{}]...", s3Object.getKey(), s3Object.getBucketName());

    ObjectMetadata objectMetadata = s3Object.getObjectMetadata();

    LOG.trace("Got object [{}]", s3Object);

    Exchange exchange = new DefaultExchange(this, pattern);
    Message message = exchange.getIn();//from   w  w  w  .  j a  v  a  2 s  .c o m
    message.setBody(s3Object.getObjectContent());
    message.setHeader(S3Constants.KEY, s3Object.getKey());
    message.setHeader(S3Constants.BUCKET_NAME, s3Object.getBucketName());
    message.setHeader(S3Constants.E_TAG, objectMetadata.getETag());
    message.setHeader(S3Constants.LAST_MODIFIED, objectMetadata.getLastModified());
    message.setHeader(S3Constants.VERSION_ID, objectMetadata.getVersionId());
    message.setHeader(S3Constants.CONTENT_TYPE, objectMetadata.getContentType());
    message.setHeader(S3Constants.CONTENT_MD5, objectMetadata.getContentMD5());
    message.setHeader(S3Constants.CONTENT_LENGTH, objectMetadata.getContentLength());
    message.setHeader(S3Constants.CONTENT_ENCODING, objectMetadata.getContentEncoding());
    message.setHeader(S3Constants.CONTENT_DISPOSITION, objectMetadata.getContentDisposition());
    message.setHeader(S3Constants.CACHE_CONTROL, objectMetadata.getCacheControl());

    return exchange;
}

From source file:org.cloudifysource.esc.driver.provisioning.privateEc2.AmazonS3Uploader.java

License:Open Source License

/**
 * Returns a pre-signed URL for accessing an Amazon S3 resource.
 * /*  w  ww  .j  a  va2 s  .c om*/
 * @param s3
 *            The S3 object.
 * @return A pre-signed URL for accessing an Amazon S3 resource.
 */
public String generatePresignedURL(final S3Object s3) {
    return this.generatePresignedURL(s3.getBucketName(), s3.getKey());
}