Example usage for com.amazonaws.services.s3.model GetObjectRequest GetObjectRequest

List of usage examples for com.amazonaws.services.s3.model GetObjectRequest GetObjectRequest

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.model GetObjectRequest GetObjectRequest.

Prototype

public GetObjectRequest(String bucketName, String key) 

Source Link

Document

Constructs a new GetObjectRequest with all the required parameters.

Usage

From source file:org.rdswitchboard.utils.s3.find.App.java

License:Open Source License

public static void main(String[] args) {
    try {//w  w  w .  j a  v  a  2 s  .c om
        if (args.length != 2)
            throw new IllegalArgumentException("Bucket name and search string can not be empty");

        String buckey = args[0];
        String search = args[1];
        String prefix = null;
        int pos = buckey.indexOf('/');
        if (pos > 0) {
            prefix = buckey.substring(pos + 1);
            buckey = buckey.substring(0, pos);
        }

        AmazonS3 s3client = new AmazonS3Client(new InstanceProfileCredentialsProvider());

        //         AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());        

        ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(buckey);

        if (!StringUtils.isNullOrEmpty(prefix))
            listObjectsRequest.setPrefix(prefix);

        ObjectListing objectListing;

        do {
            objectListing = s3client.listObjects(listObjectsRequest);
            for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                String key = objectSummary.getKey();
                System.out.println(" - " + key);

                S3Object object = s3client.getObject(new GetObjectRequest(buckey, key));
                String str = IOUtils.toString(object.getObjectContent());
                if (str.contains(search)) {
                    System.out.println("Found!");

                    FileUtils.writeStringToFile(new File("s3/" + key), str);
                }
            }
            listObjectsRequest.setMarker(objectListing.getNextMarker());
        } while (objectListing.isTruncated());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.researchgraph.crossref.CrossRef.java

private String getCahcedFile(String file) throws IOException {
    if (null != cache) {
        File f = new File(cache, file);
        if (f.exists() && !f.isDirectory()) {
            return FileUtils.readFileToString(f);
        }/* w w w .java 2  s . co  m*/

    } else if (null != s3Client) {
        S3Object o = s3Client.getObject(new GetObjectRequest(s3Bucket, getS3Key(file)));
        if (null != o) {
            try (InputStream is = o.getObjectContent()) {
                return IOUtils.toString(is);
            }
        }
    }

    return null;
}

From source file:org.serginho.awss3conn.Connection.java

public DefaultStreamedContent getFile(String key) {
    return new DefaultStreamedContent(
            getS3Client().getObject(new GetObjectRequest(this.amazonBucket, this.key)).getObjectContent(),
            URLConnection.guessContentTypeFromName(fileName(key)), fileName(key));
}

From source file:org.shareok.data.commons.recipes.S3BookRecipeFileGenerator.java

/**
 * Read out the manifest file information and convert it into a map with keys of bookname and values of hash codes
 * /*from   w  w w.  j a v a2  s  .c  o m*/
 * @param bookName
 * @return 
 */
public Map<String, String> getManifest(String bookName) {

    Map<String, String> manifest = new HashMap<>();
    S3ObjectInputStream in = null;
    BufferedReader reader = null;
    String line;

    try {
        in = s3client.getObject(new GetObjectRequest(sourceBucket, bookName + "/manifest-md5.txt"))
                .getObjectContent();
        reader = new BufferedReader(new InputStreamReader(in));
        while ((line = reader.readLine()) != null) {
            if (null != line) {
                String[] manifestInfo = line.split(" ");
                int length = manifestInfo.length;
                if (length < 2) {
                    continue;
                }
                String hash = manifestInfo[0];
                String filePath = manifestInfo[length - 1];
                if (!filePath.endsWith(".tif")) {
                    continue;
                }
                //                    String fileInfo[] = filePath.split("/");
                //                    String fileName = fileInfo[fileInfo.length-1];
                manifest.put(bookName + "/" + filePath, hash);
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (null != in) {
            try {
                in.close();
            } catch (IOException ex) {
                Logger.getLogger(S3BookRecipeFileGenerator.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (null != reader) {
            try {
                reader.close();
            } catch (IOException ex) {
                Logger.getLogger(S3BookRecipeFileGenerator.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    return manifest;
}

From source file:org.sifarish.util.Utility.java

License:Apache License

public static void setConfiguration(Configuration conf) throws Exception {
    String confFilePath = conf.get("conf.path");
    if (null != confFilePath) {
        InputStream is = null;/*from  ww w  .j  a  v  a 2 s.c o  m*/
        if (confFilePath.startsWith(S3_PREFIX)) {
            Matcher matcher = s3pattern.matcher(confFilePath);
            matcher.matches();
            String bucket = matcher.group(1);
            String key = matcher.group(2);
            S3Object object = s3.getObject(new GetObjectRequest(bucket, key));
            is = object.getObjectContent();
        } else {
            is = new FileInputStream(confFilePath);
        }
        Properties configProps = new Properties();
        configProps.load(is);

        for (Object key : configProps.keySet()) {
            String keySt = key.toString();
            conf.set(keySt, configProps.getProperty(keySt));
        }
    }
}

From source file:org.springframework.cloud.aws.core.io.s3.SimpleStorageResource.java

License:Apache License

@Override
public InputStream getInputStream() throws IOException {
    GetObjectRequest getObjectRequest = new GetObjectRequest(this.bucketName, this.objectName);
    if (this.versionId != null) {
        getObjectRequest.setVersionId(this.versionId);
    }/*from  ww  w  .  j  av  a  2  s  .com*/
    return this.amazonS3.getObject(getObjectRequest).getObjectContent();
}

From source file:org.springframework.integration.aws.outbound.S3MessageHandler.java

License:Apache License

private Transfer download(Message<?> requestMessage) {
    Object payload = requestMessage.getPayload();
    Assert.state(payload instanceof File, "For the 'DOWNLOAD' operation the 'payload' must be of "
            + "'java.io.File' type, but gotten: [" + payload.getClass() + ']');

    File targetFile = (File) payload;

    String bucket = obtainBucket(requestMessage);

    String key = null;/* ww w.  ja  va  2s . c o  m*/
    if (this.keyExpression != null) {
        key = this.keyExpression.getValue(this.evaluationContext, requestMessage, String.class);
    } else {
        key = targetFile.getName();
    }

    Assert.state(key != null,
            "The 'keyExpression' must not be null for non-File payloads and can't evaluate to null. "
                    + "Root object is: " + requestMessage);

    if (targetFile.isDirectory()) {
        return this.transferManager.downloadDirectory(bucket, key, targetFile);
    } else {
        if (this.s3ProgressListener != null) {
            return this.transferManager.download(new GetObjectRequest(bucket, key), targetFile,
                    this.s3ProgressListener);
        } else {
            return this.transferManager.download(bucket, key, targetFile);
        }
    }
}

From source file:org.springframework.integration.aws.s3.core.AmazonS3OperationsImpl.java

License:Apache License

public AmazonS3Object getObject(String bucketName, String folder, String objectName) {
    if (logger.isDebugEnabled())
        logger.debug("Getting from bucket " + bucketName + ", from folder " + folder + " the  object name "
                + objectName);//from w w w.  j a  v  a  2  s.  co m
    GetObjectRequest request = new GetObjectRequest(bucketName, objectName);
    S3Object s3Object = client.getObject(request);
    AmazonS3Object object = new AmazonS3Object(s3Object.getObjectMetadata().getUserMetadata(),
            s3Object.getObjectMetadata().getRawMetadata(), s3Object.getObjectContent(), null);
    return object;
}

From source file:org.springframework.integration.aws.s3.core.DefaultAmazonS3Operations.java

License:Apache License

/**
 * Gets the object from the given bucket with the given key using the AWS SDK implementation
 *
 * @param bucketName// w  ww .ja v  a  2  s. c  om
 * @param key
 * @return The Amazon S3 Object representing the Object in S3, may be null.
 */
@Override
protected AmazonS3Object doGetObject(String bucketName, String key) {
    GetObjectRequest request = new GetObjectRequest(bucketName, key);
    S3Object s3Object;
    try {
        s3Object = client.getObject(request);
    } catch (AmazonS3Exception e) {
        if ("NoSuchKey".equals(e.getErrorCode())) {
            //If the key is not found, return null rather than throwing the exception
            return null;
        } else {
            //throw the exception to caller in all other cases
            throw e;
        }
    }
    return new AmazonS3Object(s3Object.getObjectMetadata().getUserMetadata(),
            s3Object.getObjectMetadata().getRawMetadata(), s3Object.getObjectContent(), null);
}

From source file:org.symphonyoss.vb.util.AwsS3Client.java

License:Apache License

public InputStream getObject(S3ObjectSummary objectSummary) {

    S3Object object = null;//from   w  ww .  j  av a 2  s  .c o  m

    try {
        logger.info("Retrieving object inputstream for s3://{}/{}", objectSummary.getBucketName(),
                objectSummary.getKey());
        object = s3Client
                .getObject(new GetObjectRequest(objectSummary.getBucketName(), objectSummary.getKey()));

    } catch (AmazonServiceException ase) {
        logger.error("Caught an AmazonServiceException, " + "which means your request made it "
                + "to Amazon S3, but was rejected with an error response " + "for some reason.");
        logger.error("Error Message:    " + ase.getMessage());
        logger.error("HTTP Status Code: " + ase.getStatusCode());
        logger.error("AWS Error Code:   " + ase.getErrorCode());
        logger.error("Error Type:       " + ase.getErrorType());
        logger.error("Request ID:       " + ase.getRequestId());

    } catch (AmazonClientException ace) {
        logger.error("Caught an AmazonClientException, " + "which means the client encountered "
                + "an internal error while trying to communicate" + " with S3, "
                + "such as not being able to access the network.");
        logger.error("Error Message: " + ace.getMessage());
    }
    return object == null ? null : object.getObjectContent();
}