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:io.dockstore.common.FileProvisioning.java

License:Apache License

private void downloadFromS3(String path, String targetFilePath) {
    AmazonS3 s3Client = getAmazonS3Client(config);
    String trimmedPath = path.replace("s3://", "");
    List<String> splitPathList = Lists.newArrayList(trimmedPath.split("/"));
    String bucketName = splitPathList.remove(0);

    S3Object object = s3Client.getObject(new GetObjectRequest(bucketName, Joiner.on("/").join(splitPathList)));
    try {//from  ww  w. j  av  a2  s .  c  o  m
        FileOutputStream outputStream = new FileOutputStream(new File(targetFilePath));
        S3ObjectInputStream inputStream = object.getObjectContent();
        long inputSize = object.getObjectMetadata().getContentLength();
        copyFromInputStreamToOutputStream(inputStream, inputSize, outputStream);
    } catch (IOException e) {
        LOG.error(e.getMessage());
        throw new RuntimeException("Could not provision input files from S3", e);
    }
}

From source file:io.dockstore.webservice.core.tooltester.ToolTesterS3Client.java

License:Apache License

public String getToolTesterLog(String toolId, String versionName, String testFilePath, String runner,
        String filename) throws IOException {
    String key = generateKey(toolId, versionName, testFilePath, runner, filename);
    S3Object s3Object = s3.getObject(bucketName, key);
    InputStream objectContent = s3Object.getObjectContent();
    return IOUtils.toString(objectContent, StandardCharsets.UTF_8);

}

From source file:io.druid.firehose.s3.StaticS3FirehoseFactory.java

License:Apache License

@Override
protected InputStream openObjectStream(S3ObjectSummary object) throws IOException {
    try {// w w  w. j av  a  2  s  .com
        // Get data of the given object and open an input stream
        final S3Object s3Object = s3Client.getObject(object.getBucketName(), object.getKey());
        if (s3Object == null) {
            throw new ISE("Failed to get an s3 object for bucket[%s] and key[%s]", object.getBucketName(),
                    object.getKey());
        }
        return s3Object.getObjectContent();
    } catch (AmazonS3Exception e) {
        throw new IOException(e);
    }
}

From source file:io.druid.firehose.s3.StaticS3FirehoseFactory.java

License:Apache License

@Override
protected InputStream openObjectStream(S3ObjectSummary object, long start) throws IOException {
    final GetObjectRequest request = new GetObjectRequest(object.getBucketName(), object.getKey());
    request.setRange(start);//from   w w w .j a  va  2 s. co 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]",
                    object.getBucketName(), object.getKey(), start);
        }
        return s3Object.getObjectContent();
    } catch (AmazonS3Exception e) {
        throw new IOException(e);
    }
}

From source file:io.klerch.alexa.state.handler.AWSS3StateHandler.java

License:Open Source License

private String getS3FileContentsAsString(final String filePath) throws AlexaStateException {
    final S3Object file = awsClient.getObject(bucketName, filePath);
    final BufferedReader reader = new BufferedReader(new InputStreamReader(file.getObjectContent()));
    final StringBuilder sb = new StringBuilder();
    String line;/* w ww.j a v  a2 s  .  c  o  m*/
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
    } catch (IOException e) {
        final String error = String.format("Could not read from S3-file '%1$s' from Bucket '%2$s'.", filePath,
                bucketName);
        log.error(error, e);
        throw AlexaStateException.create(error).withCause(e).withHandler(this).build();
    }
    final String fileContents = sb.toString();
    return fileContents.isEmpty() ? "{}" : fileContents;
}

From source file:io.klerch.alexa.tellask.util.resource.S3UtteranceReader.java

License:Open Source License

/**
 * {@inheritDoc}//from  ww  w .  j ava  2s . com
 */
@Override
public InputStream read(final String locale) {
    Validate.notNull(locale, "Locale must not be blank.");

    final String resourcePath = getLeadingPath() + locale + getResourceLocation();

    final S3Object s3Object = s3Client.getObject(bucketName, resourcePath);

    Validate.notNull(s3Object,
            "Resource " + resourcePath + " does not exist in bucket with name " + bucketName);
    return s3Object.getObjectContent();
}

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();//from  w  w  w  . j a va  2s  .c  om

    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:io.milton.s3.AmazonS3ManagerImpl.java

License:Open Source License

@Override
public InputStream downloadEntity(String bucketName, String keyName) {
    LOG.info("Gets the object stored in Amazon S3 under the specified bucket " + bucketName + " and key "
            + keyName);/*  w w w  .j  a  v  a2 s  .  c om*/
    try {
        S3Object s3Object = amazonS3Client.getObject(bucketName, keyName);
        if (s3Object != null) {
            return s3Object.getObjectContent();
        }
    } catch (AmazonServiceException ase) {
        LOG.warn(ase.getMessage(), ase);
    } catch (AmazonClientException ace) {
        LOG.warn(ace.getMessage(), ace);
    }
    return null;
}

From source file:io.minio.awssdk.tests.S3TestUtils.java

License:Apache License

void downloadObject(String bucketName, String keyName, SSECustomerKey sseKey, String expectedMD5, int start,
        int length) throws Exception, IOException {
    GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, keyName).withSSECustomerKey(sseKey);

    if (start >= 0 && length >= 0) {
        getObjectRequest.setRange(start, start + length - 1);
    }/*from   www  . j  ava  2s  .  c om*/

    S3Object s3Object = s3Client.getObject(getObjectRequest);

    int size = 0;
    int c;

    S3ObjectInputStream input = s3Object.getObjectContent();

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    String data = "";
    while ((c = input.read()) != -1) {
        output.write((byte) c);
        size++;
    }

    if (length >= 0 && size != length) {
        throw new Exception(
                "downloaded object has unexpected size, expected: " + length + ", received: " + size);
    }

    String calculatedMD5 = Utils.getBufferMD5(output.toByteArray());

    if (!expectedMD5.equals("") && !calculatedMD5.equals(expectedMD5)) {
        throw new Exception("downloaded object has unexpected md5sum, expected: " + expectedMD5 + ", found: "
                + calculatedMD5);

    }
}

From source file:io.seldon.resources.external.S3FileStreamer.java

License:Apache License

public InputStream getResourceStream(String reference) throws IOException {
    logger.info("Reading file from s3://" + reference);
    AmazonS3Client client;/*from   ww  w.j  a v  a2 s . c  o m*/
    if (creds != null) {
        client = new AmazonS3Client(creds);
    } else {
        client = new AmazonS3Client();
    }
    String[] bucketAndFile = reference.split("/", 2);
    if (bucketAndFile.length != 2) {
        return null;
    }
    S3Object object = client.getObject(new GetObjectRequest(bucketAndFile[0], bucketAndFile[1]));
    if (reference.endsWith(".gz")) {
        return new S3ObjectInputStreamWrapper(new GZIPInputStream(object.getObjectContent()), client);

    } else {
        return new S3ObjectInputStreamWrapper(object.getObjectContent(), client);
    }
}