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:com.netflix.spinnaker.clouddriver.artifacts.s3.S3ArtifactCredentials.java

License:Apache License

@Override
public InputStream download(Artifact artifact) throws IllegalArgumentException {
    String reference = artifact.getReference();
    if (reference.startsWith("s3://")) {
        reference = reference.substring("s3://".length());
    }//from   w w w  . j ava2 s  .com

    int slash = reference.indexOf("/");
    if (slash <= 0) {
        throw new IllegalArgumentException(
                "S3 references must be of the format s3://<bucket>/<file-path>, got: " + artifact);
    }
    String bucketName = reference.substring(0, slash);
    String path = reference.substring(slash + 1);
    S3Object s3obj = getS3Client().getObject(bucketName, path);
    return s3obj.getObjectContent();
}

From source file:com.netflix.spinnaker.clouddriver.aws.provider.view.AmazonS3DataProvider.java

License:Apache License

@Override
public void getAdhocData(String groupId, String bucketId, String objectId, OutputStream outputStream) {
    String[] bucketCoordinates = bucketId.split(":");
    if (bucketCoordinates.length != 3) {
        throw new IllegalArgumentException("'bucketId' must be of the form {account}:{region}:{name}");
    }/*from  www  .  jav  a 2  s.  c  o m*/

    String bucketAccount = getAccountName(bucketCoordinates[0]);
    String bucketRegion = bucketCoordinates[1];
    String bucketName = bucketCoordinates[2];

    AdhocRecord record = configuration.getAdhocRecord(groupId);
    Matcher bucketNameMatcher = record.getBucketNamePattern().matcher(bucketName);
    Matcher objectKeyMatcher = record.getObjectKeyPattern().matcher(objectId);

    if (!bucketNameMatcher.matches() || !objectKeyMatcher.matches()) {
        throw new AccessDeniedException("Access denied (bucket: " + bucketName + ", object: " + objectId + ")");
    }

    try {
        S3Object s3Object = fetchObject(bucketAccount, bucketRegion, bucketName, objectId);
        IOUtils.copy(s3Object.getObjectContent(), outputStream);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}

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 {//from w w w.j ava  2 s . co m
        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.front50.model.S3StorageService.java

License:Apache License

private <T extends Timestamped> T deserialize(S3Object s3Object, Class<T> clazz) throws IOException {
    return objectMapper.readValue(s3Object.getObjectContent(), clazz);
}

From source file:com.netflix.spinnaker.front50.model.S3Support.java

License:Apache License

private T deserialize(S3Object s3Object) throws IOException {
    return objectMapper.readValue(s3Object.getObjectContent(), getSerializedClass());
}

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 ww. j a va 2 s .  c  o  m
        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.nextdoor.bender.config.BenderConfig.java

License:Apache License

public static BenderConfig load(AmazonS3ClientFactory s3ClientFactory, AmazonS3URI s3Uri) {
    AmazonS3Client s3 = s3ClientFactory.newInstance();
    S3Object s3object = s3.getObject(s3Uri.getBucket(), s3Uri.getKey());

    StringWriter writer = new StringWriter();

    try {/*  w ww .j av  a 2  s  .  c o  m*/
        IOUtils.copy(s3object.getObjectContent(), writer, "UTF-8");
    } catch (IOException e) {
        throw new ConfigurationException("Unable to read file from s3", e);
    }
    BenderConfig config = load(s3Uri.getKey().toString(), writer.toString());
    config.setConfigFile(s3Uri.getURI().toString());

    return config;
}

From source file:com.nextdoor.bender.handler.s3.S3EventIterator.java

License:Apache License

private void updateCursor() {
    if (this.currentIndex == 0 || (this.currentIndex < this.records.size() && !this.lineIterator.hasNext())) {
        /*// ww w  .  ja  v a 2  s  . c om
         * The previous reader must be closed in order to prevent S3 connection leaking
         */
        closeCurrentReader();

        /*
         * Use the S3 trigger event time for arrival time of records in file. This is less precise but
         * avoids making a call to the S3 api to find file creation time. Note that if the
         * deserializer creates a {@link com.nextdoor.bender.deserializer.DeserializedTimeSeriesEvent}
         * then this arrival time is not used.
         */
        S3EventNotificationRecord event = this.records.get(currentIndex);
        this.arrivalTime = event.getEventTime().toDate().getTime();
        this.currentS3Entity = event.getS3();

        /*
         * The S3 Object key is URL encoded and must be decoded before it can be used by the
         * AmazonS3Client
         */
        String key;
        try {
            key = URLDecoder.decode(this.currentS3Entity.getObject().getKey(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }

        /*
         * Stream object back from S3 into a reader
         */
        String bucketName = this.currentS3Entity.getBucket().getName();
        logger.debug("opening s3://" + bucketName + "/" + key);
        GetObjectRequest req = new GetObjectRequest(bucketName, key);
        S3Object obj = client.getObject(req);
        logger.trace("s3 get request id: " + client.getCachedResponseMetadata(req).getRequestId() + " host: "
                + client.getCachedResponseMetadata(req).getHostId() + " cloudfrontid: "
                + client.getCachedResponseMetadata(req).getCloudFrontId());

        /*
         * If the file is compressed run it through the GZIP decompressor
         */
        // TODO: support different types of compressions
        if (key.endsWith(".gz")) {
            GZIPInputStream gzip;
            try {
                gzip = new GZIPInputStream(obj.getObjectContent());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            reader = new BufferedReader(new InputStreamReader(gzip));
        } else {
            reader = new BufferedReader(new InputStreamReader(obj.getObjectContent()));
        }

        /*
         * Note the BufferedReader is lazy and so is the iterator. The object is directly streamed
         * from S3, fed into an input stream and consumed line by line by the iterator.
         */
        this.lineIterator = reader.lines().iterator();

        currentIndex++;
    }
}

From source file:com.nextdoor.bender.operations.geo.GeoIpOperationFactory.java

License:Apache License

@Override
public void setConf(AbstractConfig config) {
    this.config = (GeoIpOperationConfig) config;
    AmazonS3Client client = this.s3Factory.newInstance();

    AmazonS3URI uri = new AmazonS3URI(this.config.getGeoLiteDb());
    GetObjectRequest req = new GetObjectRequest(uri.getBucket(), uri.getKey());
    S3Object obj = client.getObject(req);

    try {/*w  w w  .ja v a2 s  .co  m*/
        this.databaseReader = new DatabaseReader.Builder(obj.getObjectContent()).withCache(new CHMCache())
                .build();
    } catch (IOException e) {
        throw new ConfigurationException("Unable to read " + this.config.getGeoLiteDb(), e);
    }
}

From source file:com.nike.cerberus.config.CmsEnvPropertiesLoader.java

License:Apache License

private String getObject(String path) {
    final GetObjectRequest request = new GetObjectRequest(bucketName, path);

    try {/*from ww  w . j  a  v a 2s  . c  om*/
        S3Object s3Object = s3Client.getObject(request);
        InputStream object = s3Object.getObjectContent();
        return IOUtils.toString(object, Charset.defaultCharset());
    } catch (AmazonServiceException ase) {
        if (StringUtils.equalsIgnoreCase(ase.getErrorCode(), "NoSuchKey")) {
            final String errorMessage = String.format("The S3 object doesn't exist. Bucket: %s, Key: %s",
                    bucketName, request.getKey());
            logger.debug(errorMessage);
            throw new IllegalStateException(errorMessage);
        } else {
            logger.error("Unexpected error communicating with AWS.", ase);
            throw ase;
        }
    } catch (IOException e) {
        String errorMessage = String.format(
                "Unable to read contents of S3 object. Bucket: %s, Key: %s, Expected Encoding: %s", bucketName,
                request.getKey(), Charset.defaultCharset());
        logger.error(errorMessage);
        throw new IllegalStateException(errorMessage, e);
    }
}