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

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

Introduction

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

Prototype

public S3ObjectInputStream getObjectContent() 

Source Link

Document

Gets the input stream containing the contents of this object.

Usage

From source file:onl.area51.filesystem.s3.S3Retriever.java

License:Apache License

@Override
public void retrieve(char[] path) throws IOException {
    String pathValue = String.valueOf(path);
    try {// w  w  w . j a  v a 2  s .  c o  m
        LOG.log(Level.FINE, () -> "Retrieving " + getBucketName() + ":" + pathValue);
        S3Object obj = getS3().getObject(new GetObjectRequest(getBucketName(), pathValue));
        FileSystemUtils.copyFromRemote(() -> obj.getObjectContent(), getDelegate(), path);
        LOG.log(Level.FINE, () -> "Retrieved " + getBucketName() + ":" + pathValue);
    } catch (AmazonS3Exception ex) {
        LOG.log(Level.FINE, () -> "Error " + ex.getStatusCode() + " " + getBucketName() + ":" + pathValue);
        if (ex.getStatusCode() == 404) {
            throw new FileNotFoundException(pathValue);
        }
        throw new IOException("Cannot access " + pathValue, ex);
    }
}

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//  w  w w.  j ava 2s  . 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 {//  w w w .  j a  v a2 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.alfresco.provision.AWSService.java

License:Open Source License

public void get(String key, String filename) throws IOException {
    S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
    InputStream in = new BufferedInputStream(object.getObjectContent());
    OutputStream out = new BufferedOutputStream(new FileOutputStream(filename));
    try {/*w  w  w .ja  v a 2  s  . c  o m*/
        IOUtils.copy(in, out);
    } finally {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }
}

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  www  . j a v a 2s . c  om
    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.apache.druid.firehose.s3.StaticS3FirehoseFactory.java

License:Apache License

@Override
protected InputStream openObjectStream(URI object) throws IOException {
    try {//from   w  ww.  j a  v  a2  s  .c o  m
        // Get data of the given object and open an input stream
        final String bucket = object.getAuthority();
        final String key = S3Utils.extractS3Key(object);

        final S3Object s3Object = s3Client.getObject(bucket, key);
        if (s3Object == null) {
            throw new ISE("Failed to get an s3 object for bucket[%s] and key[%s]", bucket, key);
        }
        return s3Object.getObjectContent();
    } catch (AmazonS3Exception e) {
        throw new IOException(e);
    }
}

From source file:org.apache.druid.firehose.s3.StaticS3FirehoseFactory.java

License:Apache License

@Override
protected InputStream openObjectStream(URI object, long start) throws IOException {
    final String bucket = object.getAuthority();
    final String key = S3Utils.extractS3Key(object);

    final GetObjectRequest request = new GetObjectRequest(bucket, key);
    request.setRange(start);/* ww  w  . j  av  a  2s.c o  m*/
    try {
        final S3Object s3Object = s3Client.getObject(request);
        if (s3Object == null) {
            throw new ISE("Failed to get an s3 object for bucket[%s], key[%s], and start[%d]", bucket, key,
                    start);
        }
        return s3Object.getObjectContent();
    } catch (AmazonS3Exception e) {
        throw new IOException(e);
    }
}

From source file:org.apache.fineract.infrastructure.documentmanagement.contentrepository.S3ContentRepository.java

License:Apache License

@Override
public ImageData fetchImage(final ImageData imageData) {
    final S3Object s3object = this.s3Client
            .getObject(new GetObjectRequest(this.s3BucketName, imageData.location()));
    imageData.updateContent(s3object.getObjectContent());
    return imageData;
}

From source file:org.apache.ignite.spi.checkpoint.s3.S3CheckpointSpi.java

License:Apache License

/**
 * Reads checkpoint data.//from  www  . j  av  a2s. c om
 *
 * @param key Key name to read data from.
 * @return Checkpoint data object.
 * @throws IgniteCheckedException Thrown if an error occurs while unmarshalling.
 * @throws AmazonClientException If an error occurs while querying Amazon S3.
 */
@Nullable
private S3CheckpointData read(String key) throws IgniteCheckedException, AmazonClientException {
    assert !F.isEmpty(key);

    if (log.isDebugEnabled())
        log.debug("Reading data from S3 [bucket=" + bucketName + ", key=" + key + ']');

    try {
        S3Object obj = s3.getObject(bucketName, key);

        InputStream in = obj.getObjectContent();

        try {
            return S3CheckpointData.fromStream(in);
        } catch (IOException e) {
            throw new IgniteCheckedException(
                    "Failed to unmarshal S3CheckpointData [bucketName=" + bucketName + ", key=" + key + ']', e);
        } finally {
            U.closeQuiet(in);
        }
    } catch (AmazonServiceException e) {
        if (e.getStatusCode() != 404)
            throw e;
    }

    return null;
}

From source file:org.apache.jackrabbit.aws.ext.ds.S3Backend.java

License:Apache License

@Override
public InputStream read(DataIdentifier identifier) throws DataStoreException {
    long start = System.currentTimeMillis();
    String key = getKeyName(identifier);
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {/*  w  w w .  jav  a  2  s.com*/
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        S3Object object = s3service.getObject(bucket, key);
        InputStream in = object.getObjectContent();
        LOG.debug("[{}] read took [{}]ms", identifier, (System.currentTimeMillis() - start));
        return in;
    } catch (AmazonServiceException e) {
        throw new DataStoreException("Object not found: " + key, e);
    } finally {
        if (contextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
}