Example usage for com.amazonaws.services.s3 AmazonS3 getObject

List of usage examples for com.amazonaws.services.s3 AmazonS3 getObject

Introduction

In this page you can find the example usage for com.amazonaws.services.s3 AmazonS3 getObject.

Prototype

ObjectMetadata getObject(GetObjectRequest getObjectRequest, File destinationFile)
        throws SdkClientException, AmazonServiceException;

Source Link

Document

Gets the object metadata for the object stored in Amazon S3 under the specified bucket and key, and saves the object contents to the specified file.

Usage

From source file:com.netflix.spinnaker.config.secrets.engines.S3SecretEngine.java

License:Apache License

@Override
protected InputStream downloadRemoteFile(EncryptedSecret encryptedSecret) throws IOException {
    String region = encryptedSecret.getParams().get(STORAGE_REGION);
    String bucket = encryptedSecret.getParams().get(STORAGE_BUCKET);
    String objName = encryptedSecret.getParams().get(STORAGE_FILE_URI);

    AmazonS3ClientBuilder s3ClientBuilder = AmazonS3ClientBuilder.standard().withRegion(region);

    AmazonS3 s3Client = s3ClientBuilder.build();

    try {//w ww.  j ava2  s.  com
        S3Object s3Object = s3Client.getObject(bucket, objName);
        return s3Object.getObjectContent();

    } catch (AmazonClientException ex) {
        String msg = String.format(
                "Error reading contents of S3. Region: %s, Bucket: %s, Object: %s. "
                        + "Check connectivity and permissions to that bucket: %s ",
                region, bucket, objName, ex.toString());
        throw new IOException(msg);
    }
}

From source file:com.netflix.spinnaker.kork.secrets.engines.S3SecretEngine.java

License:Apache License

@Override
protected InputStream downloadRemoteFile(EncryptedSecret encryptedSecret) throws IOException {
    String region = encryptedSecret.getParams().get(STORAGE_REGION);
    String bucket = encryptedSecret.getParams().get(STORAGE_BUCKET);
    String objName = encryptedSecret.getParams().get(STORAGE_FILE_URI);

    AmazonS3ClientBuilder s3ClientBuilder = AmazonS3ClientBuilder.standard().withRegion(region);

    AmazonS3 s3Client = s3ClientBuilder.build();

    try {/*from w  w w  .  j  ava2  s  .c om*/
        if (!s3Client.doesBucketExistV2(bucket)) {
            throw new SecretException(
                    String.format("S3 Bucket does not exist. Bucket: %s, Region: %s", bucket, region));
        }

        S3Object s3Object = s3Client.getObject(bucket, objName);

        return s3Object.getObjectContent();
    } catch (AmazonS3Exception ex) {
        StringBuilder sb = new StringBuilder("Error reading contents of S3 -- ");
        if (403 == ex.getStatusCode()) {
            sb.append(String.format(
                    "Unauthorized access. Check connectivity and permissions to the bucket. -- Bucket: %s, Object: %s, Region: %s.\n"
                            + "Error: %s ",
                    bucket, objName, region, ex.toString()));
        } else if (404 == ex.getStatusCode()) {
            sb.append(String.format(
                    "Not found. Does secret file exist? -- Bucket: %s, Object: %s, Region: %s.\nError: %s",
                    bucket, objName, region, ex.toString()));
        } else {
            sb.append(String.format("Error: %s", ex.toString()));
        }
        throw new SecretException(sb.toString());
    } catch (AmazonClientException ex) {
        throw new SecretException(
                String.format("Error reading contents of S3. Bucket: %s, Object: %s, Region: %s.\nError: %s",
                        bucket, objName, region, ex.toString()));
    }
}

From source file:com.projectlaver.batch.FacebookListingPostingItemProcessor.java

License:Open Source License

void copyS3ObjectToTempFile(File tempFile, String filename) throws IOException, FileNotFoundException {
    AWSCredentials myCredentials = new BasicAWSCredentials(this.s3accessKey, this.s3secretKey);
    AmazonS3 s3 = new AmazonS3Client(myCredentials);
    S3Object object = s3.getObject(this.s3publicBucketName, filename);

    IOUtils.copy(object.getObjectContent(), new FileOutputStream(tempFile));
}

From source file:com.projectlaver.service.ListingService.java

License:Open Source License

void streamAwsContentToResponse(String contentFilename, String bucketName, OutputStream outputStream)
        throws IOException {
    AWSCredentials myCredentials = new BasicAWSCredentials(this.s3accessKey, this.s3secretKey);
    AmazonS3 s3 = new AmazonS3Client(myCredentials);
    S3Object object = s3.getObject(bucketName, contentFilename);

    FileCopyUtils.copy(object.getObjectContent(), outputStream);
}

From source file:com.shelfmap.simplequery.domain.impl.DefaultBlobReference.java

License:Apache License

@Override
public T getContent() throws BlobRestoreException {

    //TODO for avoiding a strange behavior of Amazon S3, I download all data from a bucket into a file and create a InputStream.
    //If I process something directly on the stream which have gotten by s3.getObject().getObjectContent(),
    //the remote socket of the s3 object suddenly be closed while the processing.
    //Same problems are foundable in google search, but no appropriate answer.
    File temp = null;/*from  ww  w .  j a  va 2s  .  co m*/
    InputStream resourceStream = null;
    try {
        String bucket = resourceInfo.getBucketName();
        String key = resourceInfo.getKey();
        String version = resourceInfo.getVersionId();
        AmazonS3 s3 = getContext().getS3();

        GetObjectRequest request = version.isEmpty() ? new GetObjectRequest(bucket, key)
                : new GetObjectRequest(bucket, key, version);
        temp = File.createTempFile("simplequery-", ".tmp");
        s3.getObject(request, temp);

        resourceStream = new FileInputStream(temp);
        T content = getContentConverter().restoreObject(getObjectMetadata(), resourceStream);
        return content;
    } catch (IOException ex) {
        throw new BlobRestoreException(ex);
    } finally {
        IO.close(resourceStream, this);
        if (temp != null)
            temp.delete();
    }
}

From source file:com.yahoo.athenz.zts.store.impl.S3ChangeLogStore.java

License:Apache License

SignedDomain getSignedDomain(AmazonS3 s3, String domainName) {

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("AWSS3ChangeLog: getting signed domain {}", domainName);
    }/*from  w w w.ja va2 s  . c  o m*/

    SignedDomain signedDomain = null;
    try {
        S3Object object = s3.getObject(s3BucketName, domainName);
        try (S3ObjectInputStream s3is = object.getObjectContent()) {
            byte[] data = ByteStreams.toByteArray(s3is);
            signedDomain = JSON.fromBytes(data, SignedDomain.class);
        }
    } catch (Exception ex) {
        LOGGER.error("AWSS3ChangeLog: getSignedDomain - unable to get domain {} error: {}", domainName,
                ex.getMessage());
    }
    return signedDomain;
}

From source file:net.solarnetwork.node.backup.s3.SdkS3Client.java

License:Open Source License

@Override
public S3Object getObject(String key) throws IOException {
    AmazonS3 client = getClient();
    try {//from  w ww . ja v a2 s .  c  o m
        return client.getObject(bucketName, key);
    } catch (AmazonServiceException e) {
        log.warn("AWS error: {}; HTTP code {}; AWS code {}; type {}; request ID {}", e.getMessage(),
                e.getStatusCode(), e.getErrorCode(), e.getErrorType(), e.getRequestId());
        throw new RemoteServiceException("Error getting S3 object at " + key, e);
    } catch (AmazonClientException e) {
        log.debug("Error communicating with AWS: {}", e.getMessage());
        throw new IOException("Error communicating with AWS", e);
    }
}

From source file:org.apache.nifi.minifi.c2.cache.s3.S3WritableConfiguration.java

License:Apache License

/**
 * Creates a new S3 writable configuration.
 * @param s3 An S3 {@link AmazonS3 client}.
 * @param s3ObjectSummary The S3 object {@link S3ObjectSummary summary}.
 * @param version The version of the configuration.
 *//*from www. jav a  2  s .c o m*/
public S3WritableConfiguration(AmazonS3 s3, S3ObjectSummary s3ObjectSummary, String version) {

    this.s3 = s3;
    this.s3Object = s3.getObject(s3ObjectSummary.getBucketName(), s3ObjectSummary.getKey());
    this.version = version;

}

From source file:org.deeplearning4j.aws.s3.reader.S3Downloader.java

License:Apache License

/**
 * Returns an input stream for the given bucket and key
 * @param bucket the bucket to retrieve from
 * @param key the key of the objec  t//from  w  w w  .ja va  2  s . c om
 * @return an input stream to the object
 */
public InputStream objectForKey(String bucket, String key) {
    AmazonS3 s3 = getClient();
    S3Object obj = s3.getObject(bucket, key);
    InputStream is = obj.getObjectContent();
    return is;
}

From source file:org.deeplearning4j.aws.s3.reader.S3Downloader.java

License:Apache License

public void download(String bucket, String key, File to) throws IOException {
    AmazonS3 s3 = getClient();
    S3Object obj = s3.getObject(bucket, key);
    InputStream is = obj.getObjectContent();
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(to));
    IOUtils.copy(is, bos);/*from  w w  w .  ja v  a2 s.c  o m*/
    bos.close();
    is.close();
    obj.close();
}