Example usage for com.amazonaws.services.s3.model ObjectListing isTruncated

List of usage examples for com.amazonaws.services.s3.model ObjectListing isTruncated

Introduction

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

Prototype

boolean isTruncated

To view the source code for com.amazonaws.services.s3.model ObjectListing isTruncated.

Click Source Link

Document

Indicates if this is a complete listing, or if the caller needs to make additional requests to Amazon S3 to see the full object listing for an S3 bucket

Usage

From source file:S3ClientSideEncryptionWithSymmetricMasterKey.java

License:Apache License

private static void deleteBucketAndAllContents(AmazonS3 client) {
    System.out.println("Deleting S3 bucket: " + bucketName);
    ObjectListing objectListing = client.listObjects(bucketName);

    while (true) {
        for (Iterator<?> iterator = objectListing.getObjectSummaries().iterator(); iterator.hasNext();) {
            S3ObjectSummary objectSummary = (S3ObjectSummary) iterator.next();
            client.deleteObject(bucketName, objectSummary.getKey());
        }/* w  ww . j av a  2  s .c  o m*/

        if (objectListing.isTruncated()) {
            objectListing = client.listNextBatchOfObjects(objectListing);
        } else {
            break;
        }
    }
    ;
    client.deleteBucket(bucketName);
}

From source file:AwsConsoleApp.java

License:Open Source License

public static void main(String[] args) throws Exception {

    System.out.println("===========================================");
    System.out.println("Welcome to the AWS Java SDK!");
    System.out.println("===========================================");

    init();//w  w  w. j  a  va 2 s .  c o  m

    /*
     * Amazon EC2
     *
     * The AWS EC2 client allows you to create, delete, and administer
     * instances programmatically.
     *
     * In this sample, we use an EC2 client to get a list of all the
     * availability zones, and all instances sorted by reservation id.
     */
    try {
        DescribeAvailabilityZonesResult availabilityZonesResult = ec2.describeAvailabilityZones();
        System.out.println("You have access to " + availabilityZonesResult.getAvailabilityZones().size()
                + " Availability Zones.");

        DescribeInstancesResult describeInstancesRequest = ec2.describeInstances();
        List<Reservation> reservations = describeInstancesRequest.getReservations();
        Set<Instance> instances = new HashSet<Instance>();

        for (Reservation reservation : reservations) {
            instances.addAll(reservation.getInstances());
        }

        System.out.println("You have " + instances.size() + " Amazon EC2 instance(s) running.");
    } catch (AmazonServiceException ase) {
        System.out.println("Caught Exception: " + ase.getMessage());
        System.out.println("Reponse Status Code: " + ase.getStatusCode());
        System.out.println("Error Code: " + ase.getErrorCode());
        System.out.println("Request ID: " + ase.getRequestId());
    }

    /*
     * Amazon SimpleDB
     *
     * The AWS SimpleDB client allows you to query and manage your data
     * stored in SimpleDB domains (similar to tables in a relational DB).
     *
     * In this sample, we use a SimpleDB client to iterate over all the
     * domains owned by the current user, and add up the number of items
     * (similar to rows of data in a relational DB) in each domain.
     */
    try {
        ListDomainsRequest sdbRequest = new ListDomainsRequest().withMaxNumberOfDomains(100);
        ListDomainsResult sdbResult = sdb.listDomains(sdbRequest);

        int totalItems = 0;
        for (String domainName : sdbResult.getDomainNames()) {
            DomainMetadataRequest metadataRequest = new DomainMetadataRequest().withDomainName(domainName);
            DomainMetadataResult domainMetadata = sdb.domainMetadata(metadataRequest);
            totalItems += domainMetadata.getItemCount();
        }

        System.out.println("You have " + sdbResult.getDomainNames().size() + " Amazon SimpleDB domain(s)"
                + "containing a total of " + totalItems + " items.");
    } catch (AmazonServiceException ase) {
        System.out.println("Caught Exception: " + ase.getMessage());
        System.out.println("Reponse Status Code: " + ase.getStatusCode());
        System.out.println("Error Code: " + ase.getErrorCode());
        System.out.println("Request ID: " + ase.getRequestId());
    }

    /*
     * Amazon S3
     *
     * The AWS S3 client allows you to manage buckets and programmatically
     * put and get objects to those buckets.
     *
     * In this sample, we use an S3 client to iterate over all the buckets
     * owned by the current user, and all the object metadata in each
     * bucket, to obtain a total object and space usage count. This is done
     * without ever actually downloading a single object -- the requests
     * work with object metadata only.
     */
    try {
        List<Bucket> buckets = s3.listBuckets();

        long totalSize = 0;
        int totalItems = 0;
        for (Bucket bucket : buckets) {
            /*
             * In order to save bandwidth, an S3 object listing does not
             * contain every object in the bucket; after a certain point the
             * S3ObjectListing is truncated, and further pages must be
             * obtained with the AmazonS3Client.listNextBatchOfObjects()
             * method.
             */
            ObjectListing objects = s3.listObjects(bucket.getName());
            do {
                for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
                    totalSize += objectSummary.getSize();
                    totalItems++;
                }
                objects = s3.listNextBatchOfObjects(objects);
            } while (objects.isTruncated());
        }

        System.out.println("You have " + buckets.size() + " Amazon S3 bucket(s), " + "containing " + totalItems
                + " objects with a total size of " + totalSize + " bytes.");
    } catch (AmazonServiceException ase) {
        /*
         * AmazonServiceExceptions represent an error response from an AWS
         * services, i.e. your request made it to AWS, but the AWS service
         * either found it invalid or encountered an error trying to execute
         * it.
         */
        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) {
        /*
         * AmazonClientExceptions represent an error that occurred inside
         * the client on the local host, either while trying to send the
         * request to AWS or interpret the response. For example, if no
         * network connection is available, the client won't be able to
         * connect to AWS to execute a request and will throw an
         * AmazonClientException.
         */
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:S3ClientSideEncryptionAsymmetricMasterKey.java

License:Apache License

private static void deleteBucketAndAllContents(AmazonS3 client) {
    System.out.println("Deleting S3 bucket: " + bucketName);
    ObjectListing objectListing = client.listObjects(bucketName);

    while (true) {
        for (Iterator<?> iterator = objectListing.getObjectSummaries().iterator(); iterator.hasNext();) {
            S3ObjectSummary objectSummary = (S3ObjectSummary) iterator.next();
            client.deleteObject(bucketName, objectSummary.getKey());
        }/*from   w ww . j av  a 2s . c o m*/

        if (objectListing.isTruncated()) {
            objectListing = client.listNextBatchOfObjects(objectListing);
        } else {
            break;
        }
    }
    client.deleteBucket(bucketName);
}

From source file:aws.example.s3.DeleteBucket.java

License:Open Source License

public static void main(String[] args) {
    final String USAGE = "\n" + "To run this example, supply the name of an S3 bucket\n" + "\n"
            + "Ex: DeleteBucket <bucketname>\n";

    if (args.length < 1) {
        System.out.println(USAGE);
        System.exit(1);//from   w w w.  j  ava2  s. c om
    }

    String bucket_name = args[0];

    System.out.println("Deleting S3 bucket: " + bucket_name);
    final AmazonS3 s3 = new AmazonS3Client();

    try {
        System.out.println(" - removing objects from bucket");
        ObjectListing object_listing = s3.listObjects(bucket_name);
        while (true) {
            for (Iterator<?> iterator = object_listing.getObjectSummaries().iterator(); iterator.hasNext();) {
                S3ObjectSummary summary = (S3ObjectSummary) iterator.next();
                s3.deleteObject(bucket_name, summary.getKey());
            }

            // more object_listing to retrieve?
            if (object_listing.isTruncated()) {
                object_listing = s3.listNextBatchOfObjects(object_listing);
            } else {
                break;
            }
        }
        ;

        System.out.println(" - removing versions from bucket");
        VersionListing version_listing = s3.listVersions(new ListVersionsRequest().withBucketName(bucket_name));
        while (true) {
            for (Iterator<?> iterator = version_listing.getVersionSummaries().iterator(); iterator.hasNext();) {
                S3VersionSummary vs = (S3VersionSummary) iterator.next();
                s3.deleteVersion(bucket_name, vs.getKey(), vs.getVersionId());
            }

            if (version_listing.isTruncated()) {
                version_listing = s3.listNextBatchOfVersions(version_listing);
            } else {
                break;
            }
        }

        System.out.println(" OK, bucket ready to delete!");
        s3.deleteBucket(bucket_name);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}

From source file:aws.sample.AwsConsoleApp.java

License:Open Source License

public static void main(String[] args) throws Exception {

    System.out.println("===========================================");
    System.out.println("Welcome to the AWS Java SDK!");
    System.out.println("===========================================");

    init();/*from   ww w  .j  a v  a 2 s  . co  m*/

    /*
     * Amazon EC2
     * 
     * The AWS EC2 client allows you to create, delete, and administer instances programmatically.
     * 
     * In this sample, we use an EC2 client to get a list of all the availability zones, and all instances sorted by reservation id.
     */
    try {
        DescribeAvailabilityZonesResult availabilityZonesResult = ec2.describeAvailabilityZones();
        System.out.println("You have access to " + availabilityZonesResult.getAvailabilityZones().size()
                + " Availability Zones.");

        DescribeInstancesResult describeInstancesRequest = ec2.describeInstances();
        List<Reservation> reservations = describeInstancesRequest.getReservations();
        Set<Instance> instances = new HashSet<Instance>();

        for (Reservation reservation : reservations) {
            instances.addAll(reservation.getInstances());
        }

        System.out.println("You have " + instances.size() + " Amazon EC2 instance(s) running.");
    } catch (AmazonServiceException ase) {
        System.out.println("Caught Exception: " + ase.getMessage());
        System.out.println("Reponse Status Code: " + ase.getStatusCode());
        System.out.println("Error Code: " + ase.getErrorCode());
        System.out.println("Request ID: " + ase.getRequestId());
    }

    /*
     * Amazon SimpleDB
     * 
     * The AWS SimpleDB client allows you to query and manage your data stored in SimpleDB domains (similar to tables in a relational DB).
     * 
     * In this sample, we use a SimpleDB client to iterate over all the domains owned by the current user, and add up the number of items (similar to rows of data in a relational DB) in each domain.
     */
    try {
        ListDomainsRequest sdbRequest = new ListDomainsRequest().withMaxNumberOfDomains(100);
        ListDomainsResult sdbResult = sdb.listDomains(sdbRequest);

        int totalItems = 0;
        for (String domainName : sdbResult.getDomainNames()) {
            DomainMetadataRequest metadataRequest = new DomainMetadataRequest().withDomainName(domainName);
            DomainMetadataResult domainMetadata = sdb.domainMetadata(metadataRequest);
            totalItems += domainMetadata.getItemCount();
        }

        System.out.println("You have " + sdbResult.getDomainNames().size() + " Amazon SimpleDB domain(s)"
                + "containing a total of " + totalItems + " items.");
    } catch (AmazonServiceException ase) {
        System.out.println("Caught Exception: " + ase.getMessage());
        System.out.println("Reponse Status Code: " + ase.getStatusCode());
        System.out.println("Error Code: " + ase.getErrorCode());
        System.out.println("Request ID: " + ase.getRequestId());
    }

    /*
     * Amazon S3
     * 
     * The AWS S3 client allows you to manage buckets and programmatically put and get objects to those buckets.
     * 
     * In this sample, we use an S3 client to iterate over all the buckets owned by the current user, and all the object metadata in each bucket, to obtain a total object and space usage count. This is done without ever actually downloading a single object -- the requests work with object metadata only.
     */
    try {
        List<Bucket> buckets = s3.listBuckets();

        long totalSize = 0;
        int totalItems = 0;
        for (Bucket bucket : buckets) {
            /*
             * In order to save bandwidth, an S3 object listing does not contain every object in the bucket; after a certain point the S3ObjectListing is truncated, and further pages must be obtained with the AmazonS3Client.listNextBatchOfObjects() method.
             */
            ObjectListing objects = s3.listObjects(bucket.getName());
            do {
                for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
                    totalSize += objectSummary.getSize();
                    totalItems++;
                }
                objects = s3.listNextBatchOfObjects(objects);
            } while (objects.isTruncated());
        }

        System.out.println("You have " + buckets.size() + " Amazon S3 bucket(s), " + "containing " + totalItems
                + " objects with a total size of " + totalSize + " bytes.");
    } catch (AmazonServiceException ase) {
        /*
         * AmazonServiceExceptions represent an error response from an AWS services, i.e. your request made it to AWS, but the AWS service either found it invalid or encountered an error trying to execute it.
         */
        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) {
        /*
         * AmazonClientExceptions represent an error that occurred inside the client on the local host, either while trying to send the request to AWS or interpret the response. For example, if no network connection is available, the client won't be able to connect to AWS to execute a request and will throw an AmazonClientException.
         */
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:ca.pgon.amazons3masscontenttype.App.java

License:Apache License

public static void main(String[] args) {

    if (args.length != 4) {
        System.out.println("Usage: AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY BUCKET_NAME CONTENT_TYPE");
        return;/*from  w  w  w.j  a va  2s. c  o  m*/
    }

    // Get the parameters
    int i = 0;
    awsAccessKeyId = args[i++];
    awsSecretAccessKey = args[i++];
    bucketName = args[i++];
    contentType = args[i++];

    // Prepare service
    AWSCredentials awsCredentials = new BasicAWSCredentials(awsAccessKeyId, awsSecretAccessKey);
    amazonS3Client = new AmazonS3Client(awsCredentials);

    // Go through the first page
    ObjectListing objectListing = amazonS3Client.listObjects(bucketName);
    process(objectListing);
    // Go through the other pages
    while (objectListing.isTruncated()) {
        amazonS3Client.listNextBatchOfObjects(objectListing);
        process(objectListing);
    }

    System.out.println();
    System.out.println("Processed " + count + " files");
}

From source file:cloudExplorer.BucketClass.java

License:Open Source License

String listBucketContents(String access_key, String secret_key, String bucket, String endpoint) {
    objectlist = null;//from   ww w.  j a va  2s. com

    AWSCredentials credentials = new BasicAWSCredentials(access_key, secret_key);
    AmazonS3 s3Client = new AmazonS3Client(credentials,
            new ClientConfiguration().withSignerOverride("S3SignerType"));
    s3Client.setEndpoint(endpoint);

    try {
        ObjectListing current = s3Client.listObjects((bucket));

        ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucket);
        ObjectListing objectListing;
        do {
            objectListing = s3Client.listObjects(listObjectsRequest);
            for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                objectlist = objectlist + "@@" + objectSummary.getKey();
            }
            listObjectsRequest.setMarker(objectListing.getNextMarker());
        } while (objectListing.isTruncated());

    } catch (Exception listBucket) {
        mainFrame.jTextArea1.append("\n" + listBucket.getMessage());
    }

    String parse = null;
    if (objectlist != null) {
        parse = objectlist;
    } else {
        parse = "No objects_found.";
    }
    return parse;
}

From source file:cloudExplorer.BucketClass.java

License:Open Source License

String getObjectInfo(String key, String access_key, String secret_key, String bucket, String endpoint,
        String process) {/*  w w  w.jav  a  2 s. c om*/
    AWSCredentials credentials = new BasicAWSCredentials(access_key, secret_key);
    AmazonS3 s3Client = new AmazonS3Client(credentials,
            new ClientConfiguration().withSignerOverride("S3SignerType"));
    s3Client.setEndpoint(endpoint);
    objectlist = null;

    try {
        ObjectListing current = s3Client.listObjects((bucket));

        ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucket);
        ObjectListing objectListing;
        do {
            objectListing = s3Client.listObjects(listObjectsRequest);

            for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {

                if (process.contains("objectsize")) {
                    if (objectSummary.getKey().contains(key)) {
                        objectlist = String.valueOf(objectSummary.getSize());
                        break;
                    }
                }

                if (process.contains("objectdate")) {
                    if (objectSummary.getKey().contains(key)) {
                        objectlist = String.valueOf(objectSummary.getLastModified());
                        break;
                    }

                }
            }
            listObjectsRequest.setMarker(objectListing.getNextMarker());
        } while (objectListing.isTruncated());

    } catch (Exception listBucket) {
        mainFrame.jTextArea1.append("\n" + listBucket.getMessage());
    }

    return objectlist;
}

From source file:com.adobe.acs.commons.mcp.impl.processes.asset.S3AssetIngestor.java

License:Apache License

private void createFolders(ActionManager manager, ObjectListing listing) {
    listing.getObjectSummaries().stream().filter(sum -> !sum.getKey().equals(s3BasePath))
            .map(S3HierarchicalElement::new).filter(S3HierarchicalElement::isFolder)
            .filter(this::canImportFolder).forEach(el -> {
                manager.deferredWithResolver(Actions.retry(retries, retryPause, rr -> {
                    manager.setCurrentItem(el.getItemName());
                    createFolderNode(el, rr);
                }));/*from w ww  .  jav  a  2 s  .c o  m*/
            });
    if (listing.isTruncated()) {
        createFolders(manager, s3Client.listNextBatchOfObjects(listing));
    }
}

From source file:com.adobe.acs.commons.mcp.impl.processes.asset.S3AssetIngestor.java

License:Apache License

private void importAssets(ActionManager manager, ObjectListing listing) {
    listing.getObjectSummaries().stream().map(S3HierarchicalElement::new).filter(S3HierarchicalElement::isFile)
            .filter(this::canImportContainingFolder).map(S3HierarchicalElement::getSource).forEach(ss -> {
                try {
                    if (canImportFile(ss)) {
                        manager.deferredWithResolver(
                                Actions.retry(retries, retryPause, importAsset(ss, manager)));
                    } else {
                        incrementCount(skippedFiles, 1);
                        trackDetailedActivity(ss.getName(), "Skip", "Skipping file", 0L);
                    }/*ww w .jav  a2s . c om*/
                } catch (IOException ex) {
                    Failure failure = new Failure();
                    failure.setException(ex);
                    failure.setNodePath(ss.getElement().getNodePath(preserveFileName));
                    manager.getFailureList().add(failure);
                } finally {
                    try {
                        ss.close();
                    } catch (IOException ex) {
                        Failure failure = new Failure();
                        failure.setException(ex);
                        failure.setNodePath(ss.getElement().getNodePath(preserveFileName));
                        manager.getFailureList().add(failure);
                    }
                }
            });
    if (listing.isTruncated()) {
        importAssets(manager, s3Client.listNextBatchOfObjects(listing));
    }
}