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:org.exem.flamingo.web.filesystem.s3.S3BrowserController.java

License:Apache License

@RequestMapping(value = "download", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)/* w  ww. j av  a  2  s  .  c o m*/
public Response download(HttpServletResponse res, @RequestParam String bucketName, @RequestParam String key)
        throws IOException {
    S3Object object = s3BrowserService.getObject(bucketName, key);
    InputStream objectData = object.getObjectContent();

    res.setHeader("Content-Length", "" + object.getObjectMetadata().getContentLength());
    res.setHeader("Content-Transfer-Encoding", "binary");
    res.setHeader("Content-Type", "application/force-download");
    res.setHeader("Content-Disposition",
            MessageFormatter.format("attachment; filename={};", getName(key)).getMessage());
    res.setStatus(200);

    FileCopyUtils.copy(objectData, res.getOutputStream());
    res.flushBuffer();
    objectData.close();

    Response response = new Response();
    response.setSuccess(true);
    return response;
}

From source file:org.exem.flamingo.web.filesystem.s3.S3BrowserServiceImpl.java

License:Apache License

@Override
public String getObjectAsString(String bucketName, String key, long size) throws IOException {
    // MAX_PREVIEW_SIZE ?  
    if (size <= S3Constansts.MAX_PREVIEW_SIZE) {
        return this.s3.getObjectAsString(bucketName, key);
    }/*from www  . j  a  v a 2  s  .c o  m*/

    byte[] buffer = new byte[S3Constansts.MAX_PREVIEW_SIZE];
    ByteArrayOutputStream output = new ByteArrayOutputStream();

    S3Object object = this.s3.getObject(bucketName, key);
    InputStream is = object.getObjectContent();

    try {
        int readCount;
        int totalReadCount = 0;
        int len = S3Constansts.MAX_PREVIEW_SIZE;
        while (totalReadCount < S3Constansts.MAX_PREVIEW_SIZE) {
            readCount = is.read(buffer, 0, len);
            output.write(buffer, 0, readCount);
            totalReadCount += readCount;
            len = S3Constansts.MAX_PREVIEW_SIZE - totalReadCount;
        }
        output.toByteArray();
    } finally {
        object.close();
    }

    return new String(output.toByteArray());
}

From source file:org.fcrepo.modeshape.binary.S3BinaryStore.java

License:Apache License

@Override
public InputStream getInputStream(BinaryKey key) throws BinaryStoreException {
    try {//from w w w.ja v a  2s.  c o m
        S3Object s3Object = s3Client.getObject(bucketName, key.toString());
        return s3Object.getObjectContent();
    } catch (AmazonClientException e) {
        throw new BinaryStoreException(e);
    }
}

From source file:org.finra.dm.dao.impl.S3DaoImpl.java

License:Apache License

@Override
public Properties getProperties(String bucketName, String key,
        S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto) {
    GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
    S3Object s3Object = getS3Object(getObjectRequest, s3FileTransferRequestParamsDto);
    try (S3ObjectInputStream s3ObjectInputStream = s3Object.getObjectContent()) {
        return javaPropertiesHelper.getProperties(s3ObjectInputStream);
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException(
                "The properties file in S3 bucket '" + bucketName + "' and key '" + key + "' is invalid.", e);
    } catch (IOException e) {
        // This exception can come when the try-with-resources attempts to close the stream.
        // Not covered as JUnit, unfortunately no way of producing an IO exception.
        throw new IllegalStateException("Error closing S3 object input stream. See cause for details.", e);
    }//from  ww w. ja va 2 s  .c  om
}

From source file:org.finra.herd.dao.impl.S3DaoImpl.java

License:Apache License

@Override
public Properties getProperties(String bucketName, String key,
        S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto) {
    AmazonS3Client s3Client = getAmazonS3(s3FileTransferRequestParamsDto);

    try {//  w ww  . j  a v  a 2  s  .c o  m
        S3Object s3Object = getS3Object(s3Client, bucketName, key, true);
        return javaPropertiesHelper.getProperties(s3Object.getObjectContent());
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException(
                "The properties file in S3 bucket '" + bucketName + "' and key '" + key + "' is invalid.", e);
    } finally {
        s3Client.shutdown();
    }
}

From source file:org.force66.aws.utils.S3Utils.java

License:Apache License

public static Properties loadProperties(String bucketName, String key) throws IOException {
    Properties props = new Properties();
    AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider());
    S3Object s3object = s3Client.getObject(new GetObjectRequest(bucketName, key));
    props.load(s3object.getObjectContent());
    return props;
}

From source file:org.geowebcache.s3.S3BlobStore.java

License:Open Source License

@Override
public boolean get(TileObject obj) throws StorageException {
    final String key = keyBuilder.forTile(obj);
    final S3Object object = s3Ops.getObject(key);
    if (object == null) {
        return false;
    }//from w  w  w . ja v a2  s.co m
    try (S3ObjectInputStream in = object.getObjectContent()) {
        byte[] bytes = ByteStreams.toByteArray(in);
        obj.setBlobSize(bytes.length);
        obj.setBlob(new ByteArrayResource(bytes));
        obj.setCreated(object.getObjectMetadata().getLastModified().getTime());
    } catch (IOException e) {
        throw new StorageException("Error getting " + key, e);
    }
    return true;
}

From source file:org.geowebcache.s3.S3Ops.java

License:Open Source License

@Nullable
public byte[] getBytes(String key) throws StorageException {
    S3Object object = getObject(key);
    if (object == null) {
        return null;
    }//  w w w . jav a  2  s .  c om
    try (S3ObjectInputStream in = object.getObjectContent()) {
        byte[] bytes = IOUtils.toByteArray(in);
        return bytes;
    } catch (IOException e) {
        throw new StorageException("Error getting " + key, e);
    }

}

From source file:org.gridgain.grid.spi.checkpoint.s3.GridS3CheckpointSpi.java

License:Open Source License

/**
 * Reads checkpoint data./*from w w w . j  av  a2  s .c  om*/
 *
 * @param key Key name to read data from.
 * @return Checkpoint data object.
 * @throws GridException Thrown if an error occurs while unmarshalling.
 * @throws AmazonClientException If an error occurs while querying Amazon S3.
 */
@Nullable
private GridS3CheckpointData read(String key) throws GridException, 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 marsh.unmarshal(in, U.gridClassLoader());
        } finally {
            U.closeQuiet(in);
        }
    } catch (AmazonServiceException e) {
        if (e.getStatusCode() != 404)
            throw e;
    }

    return null;
}

From source file:org.gytheio.content.handler.s3.S3ContentReferenceHandlerImpl.java

License:Open Source License

@Override
public InputStream getInputStream(ContentReference contentReference, boolean waitForAvailability)
        throws ContentIOException {
    if (!isContentReferenceSupported(contentReference)) {
        throw new ContentIOException("ContentReference not supported");
    }/*from  w  w w. j  a  v a2s.co  m*/
    try {
        String s3Url = getS3UrlFromHttpUrl(contentReference.getUri());

        if (logger.isDebugEnabled()) {
            logger.debug("Getting remote input stream for reference: " + s3Url);
        }
        // Get the object and retrieve the input stream
        S3Object object = s3.getObject(new GetObjectRequest(s3BucketName, getRelativePath(s3Url)));
        return object.getObjectContent();
    } catch (Throwable t) {
        throw new ContentIOException("Failed to read content", t);
    }
}