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

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

Introduction

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

Prototype

ListObjectsV2Request

Source Link

Usage

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

License:Apache License

/**
 * Provide a list of objects from a given bucket w/prefix (folder).
 *
 * @param bucketName S3 bucket name/*  w  ww.ja va  2 s .co m*/
 * @param prefix     S3 folder within the bucket
 * @return List of {@link S3ObjectSummary} sorted by date
 */
public List<S3ObjectSummary> getAllObjects(String bucketName, String prefix) {

    try {
        logger.debug("Listing S3 objects for s3://{}/{}", bucketName, prefix);
        final ListObjectsV2Request req = new ListObjectsV2Request().withBucketName(bucketName)
                .withPrefix(prefix);
        ListObjectsV2Result result;
        List<S3ObjectSummary> allObjects = new ArrayList<>();

        do {
            result = s3Client.listObjectsV2(req);

            allObjects.addAll(result.getObjectSummaries());

            req.setContinuationToken(result.getNextContinuationToken());
        } while (result.isTruncated());

        allObjects.sort(Comparator.comparing(S3ObjectSummary::getLastModified));

        return allObjects;
    } 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 null;
}

From source file:oulib.aws.s3.S3TiffProcessor.java

/**
* 
* @param bookInfo : contains the information of the source bucket name, target bucket name, and the name of the book
* @param context : lambda function runtime context
* @return ://from w  w w  .  j a va2 s . c  o  m
* 
*/
@Override
public String handleRequest(S3BookInfo bookInfo, Context context) {

    AmazonS3 s3client = new AmazonS3Client();
    Region usEast = Region.getRegion(Regions.US_EAST_1);
    s3client.setRegion(usEast);

    try {
        String sourceBucketName = bookInfo.getBucketSourceName();
        String targetBucketName = bookInfo.getBucketTargetName();
        String bookName = bookInfo.getBookName();

        // Every book has a folder in the target bucket:
        Map targetBucketKeyMap = S3Util.getBucketObjectKeyMap(targetBucketName, bookName, s3client);
        if (!S3Util.folderExitsts(bookName, targetBucketKeyMap)) {
            S3Util.createFolder(targetBucketName, bookName, s3client);
        }

        final ListObjectsV2Request req = new ListObjectsV2Request().withBucketName(sourceBucketName)
                .withPrefix(bookName + "/data/");
        ListObjectsV2Result result;

        do {
            result = s3client.listObjectsV2(req);

            for (S3ObjectSummary objectSummary : result.getObjectSummaries()) {
                String key = objectSummary.getKey();
                if (key.endsWith(".tif") && !targetBucketKeyMap.containsKey(key + ".tif")) {
                    S3Object object = s3client.getObject(new GetObjectRequest(sourceBucketName, key));
                    System.out.println("Start to generate smaller tif image for the object " + key);
                    S3Util.generateSmallTiffWithTargetSize(s3client, object, targetBucketName,
                            bookInfo.getCompressionSize());
                    //                       S3Util.copyS3ObjectTiffMetadata(s3client, object, s3client.getObject(new GetObjectRequest(targetBucketName, key)), targetBucketName, key+".tif");
                    System.out.println("Finished to generate smaller tif image for the object " + key + ".tif");
                    //                       break;
                }
            }
            System.out.println("Next Continuation Token : " + result.getNextContinuationToken());
            req.setContinuationToken(result.getNextContinuationToken());
        } while (result.isTruncated() == true);

    } catch (AmazonServiceException ase) {
        System.out.println(
                "Caught an AmazonServiceException, which means your request made it to Amazon S3, but was rejected with an error response for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        System.out.println(
                "Caught an AmazonClientException, which means the client encountered an internal error while trying to communicate with S3, \nsuch as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
    return null;
}

From source file:oulib.aws.s3.S3Util.java

/**
 * //from  w w  w .  j  a va  2  s . c om
 * @param bucketName : bucket name
 * @param folderName : a unique folder name or partial path in the bucket
 * @param client : s3 client
 * @return : a map of keys with keyset of object keys
 */
public static Map<String, String> getBucketObjectKeyMap(String bucketName, String folderName, AmazonS3 client) {
    final ListObjectsV2Request req = new ListObjectsV2Request().withBucketName(bucketName);
    ListObjectsV2Result result;
    Map<String, String> keyMap = new HashMap<>();

    do {
        result = client.listObjectsV2(req);

        for (S3ObjectSummary objectSummary : result.getObjectSummaries()) {
            String key = objectSummary.getKey();
            if (key.contains(folderName)) {
                keyMap.put(key, key);
            }
        }
        req.setContinuationToken(result.getNextContinuationToken());
    } while (result.isTruncated() == true);

    return keyMap;
}

From source file:oulib.aws.s3.S3Util.java

/**
 * /*from   ww  w .  ja  va2  s .  co  m*/
 * @param bucketName : bucket name
 * @param folderName : a unique folder name or partial path in the bucket
 * @param client : s3 client
 * @return : a list of keys
 */
public static List<String> getBucketObjectKeyList(String bucketName, String folderName, AmazonS3 client) {
    final ListObjectsV2Request req = new ListObjectsV2Request().withBucketName(bucketName);
    ListObjectsV2Result result;
    List<String> keyList = new ArrayList<>();

    do {
        result = client.listObjectsV2(req);

        for (S3ObjectSummary objectSummary : result.getObjectSummaries()) {
            String key = objectSummary.getKey();
            if (key.contains(folderName)) {
                keyList.add(key);
            }
        }
        req.setContinuationToken(result.getNextContinuationToken());
    } while (result.isTruncated() == true);

    return keyList;
}

From source file:oulib.aws.s3.S3Util.java

public static void generateTifDerivativesByS3Bucket(AmazonS3 s3client, S3BookInfo bookInfo) {

    String sourceBucketName = bookInfo.getBucketSourceName();
    String targetBucketName = bookInfo.getBucketTargetName();
    String bookName = bookInfo.getBookName();

    try {// w w w .  ja v a2 s.  c o  m

        // Every book has a folder in the target bucket:
        Map targetBucketKeyMap = S3Util.getBucketObjectKeyMap(targetBucketName, bookName, s3client);
        if (!S3Util.folderExitsts(bookName, targetBucketKeyMap)) {
            S3Util.createFolder(targetBucketName, bookName, s3client);
        }

        final ListObjectsV2Request req = new ListObjectsV2Request().withBucketName(sourceBucketName)
                .withPrefix(bookName + "/data/");
        ListObjectsV2Result result;

        do {
            result = s3client.listObjectsV2(req);

            for (S3ObjectSummary objectSummary : result.getObjectSummaries()) {
                String key = objectSummary.getKey();
                if (key.contains(".tif") && (key.contains("047") || key.contains("049") || key.contains("054"))
                        && !targetBucketKeyMap.containsKey(key + ".tif")) {
                    S3Object object = s3client.getObject(new GetObjectRequest(sourceBucketName, key));
                    System.out.println("Start to generate smaller tif image for the object " + key + "\n");
                    S3Util.generateSmallTiffWithTargetSize(s3client, object, targetBucketName,
                            bookInfo.getCompressionSize());
                    //                   S3Util.copyS3ObjectTiffMetadata(s3client, object, s3client.getObject(new GetObjectRequest(targetBucketName, key)), targetBucketName, key+".tif");
                    System.out.println("Finished to generate smaller tif image for the object " + key + "\n");
                    //                   break;
                }
            }
            System.out.println("Next Continuation Token : " + result.getNextContinuationToken() + "\n");
            req.setContinuationToken(result.getNextContinuationToken());
        } while (result.isTruncated() == true);

    } catch (AmazonServiceException ase) {
        System.out.println(
                "Caught an AmazonServiceException, which means your request made it to Amazon S3, but was rejected with an error response for some reason.\n");
        System.out.println("Error Message:    " + ase.getMessage() + "\n");
        System.out.println("HTTP Status Code: " + ase.getStatusCode() + "\n");
        System.out.println("AWS Error Code:   " + ase.getErrorCode() + "\n");
        System.out.println("Error Type:       " + ase.getErrorType() + "\n");
        System.out.println("Request ID:       " + ase.getRequestId() + "\n");
    } catch (AmazonClientException ace) {
        System.out.println(
                "Caught an AmazonClientException, which means the client encountered an internal error while trying to communicate with S3, \nsuch as not being able to access the network.\n");
        System.out.println("Error Message: " + ace.getMessage() + "\n");
    }
}