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.dspace.storage.bitstore.S3BitStoreService.java

License:BSD License

/**
  * Retrieve the bits for the asset with ID. If the asset does not
  * exist, returns null.//  ww w .j  a  va2s.c om
  * 
  * @param bitstream
  *            The ID of the asset to retrieve
  * @exception java.io.IOException
  *                If a problem occurs while retrieving the bits
  *
  * @return The stream of bits, or null
  */
public InputStream get(Bitstream bitstream) throws IOException {
    String key = getFullKey(bitstream.getInternalId());
    try {
        S3Object object = s3Service.getObject(new GetObjectRequest(bucketName, key));
        return (object != null) ? object.getObjectContent() : null;
    } catch (Exception e) {
        log.error("get(" + key + ")", e);
        throw new IOException(e);
    }
}

From source file:org.duracloud.common.rest.spring.XmlWebApplicationContext.java

License:Apache License

@Override
protected Resource getResourceByPath(String path) {
    if (path.startsWith("s3://")) {
        AmazonS3 client = AmazonS3ClientBuilder.standard().build();
        AmazonS3URI s3Uri = new AmazonS3URI(path);
        S3Object s3Obj = client.getObject(new GetObjectRequest(s3Uri.getBucket(), s3Uri.getKey()));
        s3Obj.getObjectContent();

        return new InputStreamResource(s3Obj.getObjectContent());
    }// w  ww  .  ja v  a  2  s .c  o  m
    return super.getResourceByPath(path);
}

From source file:org.duracloud.s3storage.S3ProviderUtil.java

License:Apache License

/**
 * @param s3Url using the s3://bucket/object syntax.
 * @return//from w  ww. j a v a2s  . c  o  m
 * @throws IOException
 */
public static Resource getS3ObjectByUrl(String s3Url) throws IOException {
    AmazonS3 client = AmazonS3ClientBuilder.standard().build();
    AmazonS3URI s3Uri = new AmazonS3URI(s3Url);
    S3Object s3Obj = client.getObject(new GetObjectRequest(s3Uri.getBucket(), s3Uri.getKey()));
    s3Obj.getObjectContent();
    Resource resource = new InputStreamResource(s3Obj.getObjectContent());
    return resource;
}

From source file:org.duracloud.s3storage.S3StorageProvider.java

License:Apache License

/**
 * {@inheritDoc}//from  ww  w . j  a  v  a2s  . c o m
 */
public RetrievedContent getContent(String spaceId, String contentId, String range) {
    log.debug("getContent(" + spaceId + ", " + contentId + ", " + range + ")");

    // Will throw if bucket does not exist
    String bucketName = getBucketName(spaceId);

    try {
        GetObjectRequest getRequest = new GetObjectRequest(bucketName, contentId);
        if (StringUtils.isNotEmpty(range)) {
            ContentByteRange byteRange = new ContentByteRange(range);
            if (null == byteRange.getRangeStart()) {
                // While this should be a valid setting, it is not currently
                // supported due to a limitation of the AWS S3 client
                // see: https://github.com/aws/aws-sdk-java/issues/1551
                throw new IllegalArgumentException(byteRange.getUsage(range));
            } else if (null == byteRange.getRangeEnd()) {
                getRequest.setRange(byteRange.getRangeStart());
            } else {
                getRequest.setRange(byteRange.getRangeStart(), byteRange.getRangeEnd());
            }
        }

        S3Object contentItem = s3Client.getObject(getRequest);

        RetrievedContent retrievedContent = new RetrievedContent();
        retrievedContent.setContentStream(contentItem.getObjectContent());
        retrievedContent.setContentProperties(prepContentProperties(contentItem.getObjectMetadata()));

        return retrievedContent;
    } catch (AmazonClientException e) {
        throwIfContentNotExist(bucketName, contentId);
        String err = "Could not retrieve content " + contentId + " in S3 bucket " + bucketName
                + " due to error: " + e.getMessage();
        throw new StorageException(err, e, RETRY);
    }
}

From source file:org.ecocean.media.S3AssetStore.java

License:Open Source License

public boolean cacheLocal(MediaAsset ma, boolean force) throws IOException {
    Path lpath = localPath(ma);// www  . ja  va 2 s .  c  o m
    if (lpath == null)
        return false; //TODO or throw Exception?
    if (!force && Files.exists(lpath))
        return true; //we assume if we have it, then we should be cool
    System.out.println("S3.cacheLocal() trying to write to " + lpath);
    S3Object s3obj = getS3Object(ma);
    if (s3obj == null)
        return false;
    InputStream data = s3obj.getObjectContent();
    Files.copy(data, lpath, REPLACE_EXISTING);
    data.close();
    return true;
}

From source file:org.elasticsearch.cloud.aws.blobstore.AbstarctS3BlobContainer.java

License:Apache License

@Override
public void readBlob(final String blobName, final ReadBlobListener listener) {
    blobStore.executor().execute(new Runnable() {
        @Override/* ww  w. jav a 2 s .  c  o m*/
        public void run() {
            InputStream is;
            try {
                S3Object object = blobStore.client().getObject(blobStore.bucket(), buildKey(blobName));
                is = object.getObjectContent();
            } catch (Exception e) {
                listener.onFailure(e);
                return;
            }
            byte[] buffer = new byte[blobStore.bufferSizeInBytes()];
            try {
                int bytesRead;
                while ((bytesRead = is.read(buffer)) != -1) {
                    listener.onPartial(buffer, 0, bytesRead);
                }
                listener.onCompleted();
            } catch (Exception e) {
                try {
                    is.close();
                } catch (IOException e1) {
                    // ignore
                }
                listener.onFailure(e);
            }
        }
    });
}

From source file:org.elasticsearch.cloud.aws.blobstore.S3BlobContainer.java

License:Apache License

@Override
public InputStream openInput(String blobName) throws IOException {
    int retry = 0;
    while (retry <= blobStore.numberOfRetries()) {
        try {//from  ww  w.java  2s  . c o  m
            S3Object s3Object = blobStore.client().getObject(blobStore.bucket(), buildKey(blobName));
            return s3Object.getObjectContent();
        } catch (AmazonClientException e) {
            if (blobStore.shouldRetry(e) && (retry < blobStore.numberOfRetries())) {
                retry++;
            } else {
                if (e instanceof AmazonS3Exception) {
                    if (404 == ((AmazonS3Exception) e).getStatusCode()) {
                        throw new FileNotFoundException(
                                "Blob object [" + blobName + "] not found: " + e.getMessage());
                    }
                }
                throw e;
            }
        }
    }
    throw new BlobStoreException("retries exhausted while attempting to access blob object [name:" + blobName
            + ", bucket:" + blobStore.bucket() + "]");
}

From source file:org.elasticsearch.repositories.s3.S3BlobContainer.java

License:Apache License

@Override
public InputStream readBlob(String blobName) throws IOException {
    try {/*from w w w  . ja v  a  2  s.co  m*/
        S3Object s3Object = SocketAccess
                .doPrivileged(() -> blobStore.client().getObject(blobStore.bucket(), buildKey(blobName)));
        return s3Object.getObjectContent();
    } catch (AmazonClientException e) {
        if (e instanceof AmazonS3Exception) {
            if (404 == ((AmazonS3Exception) e).getStatusCode()) {
                throw new NoSuchFileException("Blob object [" + blobName + "] not found: " + e.getMessage());
            }
        }
        throw e;
    }
}

From source file:org.entando.entando.plugins.jps3awsclient.aps.system.services.storage.AmazonS3StorageManager.java

License:Open Source License

@Override
public InputStream getStream(String subPath, boolean isProtectedResource) throws ApsSystemException {
    if (!this.isActive()) {
        return null;
    }/*  ww w.  ja v a 2s  .  c  om*/
    String key = this.getKey(subPath, isProtectedResource);
    S3Object object = this.getS3Object(this.getBucketName(), key, true);
    if (null != object) {
        return object.getObjectContent();
    }
    return null;
}

From source file:org.entando.entando.plugins.jps3awsclient.aps.system.services.storage.AmazonS3StorageManager.java

License:Open Source License

public InputStream loadInputStream(String bucketName, String key, boolean canBeNull) throws ApsSystemException {
    InputStream stream = null;//from   w  ww. ja v  a 2 s .  c  o  m
    try {
        AmazonS3Client client = this.getS3Client();
        S3Object s3Object = client.getObject(bucketName, key);
        InputStream mainIs = s3Object.getObjectContent();
        byte[] bytes = this.getBytes(mainIs);
        stream = new ByteArrayInputStream(bytes);
    } catch (Throwable t) {
        if (!canBeNull) {
            _logger.error("Error loading inputstream : bucket {} - key {}", bucketName, key, t);
            throw new ApsSystemException("Error loading inputstream : bucket " + bucketName + " - key " + key,
                    t);
        }
    }
    return stream;
}