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

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

Introduction

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

Prototype

public VersionListing listVersions(ListVersionsRequest listVersionsRequest)
        throws SdkClientException, AmazonServiceException;

Source Link

Document

Returns a list of summary information about the versions in the specified bucket.

Usage

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 www.  j  a  va 2 s  .co  m*/
    }

    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:org.finra.herd.dao.impl.S3OperationsImpl.java

License:Apache License

@Override
public VersionListing listVersions(ListVersionsRequest listVersionsRequest, AmazonS3 s3Client) {
    return s3Client.listVersions(listVersionsRequest);
}

From source file:squash.deployment.lambdas.AngularjsAppCustomResourceLambda.java

License:Apache License

void deleteAngularjsApp(String websiteBucket, LambdaLogger logger) {
    logger.log("Removing AngularjsApp content from website versioned S3 bucket");

    // We need to delete every version of every key
    ListVersionsRequest listVersionsRequest = new ListVersionsRequest().withBucketName(websiteBucket);
    VersionListing versionListing;/*from   w ww .  j  a  v  a 2  s. c  om*/

    AmazonS3 client = TransferManagerBuilder.defaultTransferManager().getAmazonS3Client();
    do {
        versionListing = client.listVersions(listVersionsRequest);
        versionListing.getVersionSummaries().stream().filter(k -> (k.getKey().startsWith("app"))).forEach(k -> {
            logger.log("About to delete version: " + k.getVersionId() + " of AngularjsApp page: " + k.getKey());
            DeleteVersionRequest deleteVersionRequest = new DeleteVersionRequest(websiteBucket, k.getKey(),
                    k.getVersionId());
            client.deleteVersion(deleteVersionRequest);
            logger.log("Successfully deleted version: " + k.getVersionId() + " of AngularjsApp page: "
                    + k.getKey());
        });

        listVersionsRequest.setKeyMarker(versionListing.getNextKeyMarker());
    } while (versionListing.isTruncated());
    logger.log("Finished removing AngularjsApp content from website S3 bucket");
}

From source file:squash.deployment.lambdas.ApiGatewayCustomResourceLambda.java

License:Apache License

void removeSdkFromS3(LambdaLogger logger) {
    logger.log("About to remove apigateway sdk from website versioned S3 bucket");
    // We need to delete every version of every key
    ListVersionsRequest listVersionsRequest = new ListVersionsRequest().withBucketName(squashWebsiteBucket);
    VersionListing versionListing;/*  www .j a  v a  2  s. com*/
    IS3TransferManager transferManager = getS3TransferManager();
    AmazonS3 client = transferManager.getAmazonS3Client();
    do {
        versionListing = client.listVersions(listVersionsRequest);
        versionListing
                .getVersionSummaries().stream().filter(k -> !(k.getKey().startsWith("20")
                        || k.getKey().equals("today.html") || k.getKey().equals("bookings.html")))
                .forEach(k -> {
                    logger.log("About to delete version: " + k.getVersionId() + " of API SDK: " + k.getKey());
                    DeleteVersionRequest deleteVersionRequest = new DeleteVersionRequest(squashWebsiteBucket,
                            k.getKey(), k.getVersionId());
                    client.deleteVersion(deleteVersionRequest);
                    logger.log("Successfully deleted version: " + k.getVersionId() + " of API SDK key: "
                            + k.getKey());
                });

        listVersionsRequest.setKeyMarker(versionListing.getNextKeyMarker());
    } while (versionListing.isTruncated());

    logger.log("Finished remove apigateway sdk from website S3 bucket");
}

From source file:squash.deployment.lambdas.NoScriptAppCustomResourceLambda.java

License:Apache License

/**
 * Implementation for the AWS Lambda function backing the NoScriptApp custom resource.
 * /*  w  ww .  j av a2  s  .  com*/
 * <p>This lambda requires the following environment variables:
 * <ul>
 *    <li>SimpleDBDomainName - name of the simpleDB domain for bookings and booking rules.</li>
 *    <li>WebsiteBucket - name of S3 bucket serving the booking website.</li>
 *    <li>ApiGatewayBaseUrl - base Url of the ApiGateway Api.</li>
 *    <li>Region - the AWS region in which the Cloudformation stack is created.</li>
 *    <li>Revision - integer incremented to force stack updates to update this resource.</li>
 * </ul>
 * 
 * <p>On success, it returns the following output to Cloudformation:
 * <ul>
 *    <li>WebsiteURL - Url of the website's first booking page.</li>
 * </ul>
 *
 * @param request request parameters as provided by the CloudFormation service
 * @param context context as provided by the CloudFormation service
 */
@Override
public Object handleRequest(Map<String, Object> request, Context context) {

    LambdaLogger logger = context.getLogger();
    logger.log("Starting NoScriptApp custom resource handleRequest");

    // Handle standard request parameters
    Map<String, String> standardRequestParameters = LambdaInputLogger.logStandardRequestParameters(request,
            logger);
    String requestType = standardRequestParameters.get("RequestType");

    // Handle required environment variables
    logger.log("Logging environment variables required by custom resource request");

    String simpleDBDomainName = System.getenv("SimpleDBDomainName");
    String websiteBucket = System.getenv("WebsiteBucket");
    String apiGatewayBaseUrl = System.getenv("ApiGatewayBaseUrl");
    String region = System.getenv("AWS_REGION");
    String revision = System.getenv("Revision");

    // Log out our required environment variables
    logger.log("SimpleDBDomainName: " + simpleDBDomainName);
    logger.log("WebsiteBucket: " + websiteBucket);
    logger.log("ApiGatewayBaseUrl: " + apiGatewayBaseUrl);
    logger.log("Region: " + region);
    logger.log("Revision: " + revision);

    // API calls below can sometimes give access denied errors during stack
    // creation which I think is bc required new roles have not yet propagated
    // across AWS. We sleep here to allow time for this propagation.
    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        logger.log("Sleep to allow new roles to propagate has been interrupted.");
    }

    // Prepare our response to be sent in the finally block
    CloudFormationResponder cloudFormationResponder = new CloudFormationResponder(standardRequestParameters,
            "DummyPhysicalResourceId");
    // Initialise failure response, which will be changed on success
    String responseStatus = "FAILED";

    String websiteURL = null;
    try {
        cloudFormationResponder.initialise();

        if (requestType.equals("Create") || requestType.equals("Update")) {

            // Upload 21 initial bookings pages and index page to the S3 bucket
            UpdateBookingsLambdaRequest updateBookingsLambdaRequest = new UpdateBookingsLambdaRequest();
            UpdateBookingsLambda updateBookingsLambda = new UpdateBookingsLambda();
            UpdateBookingsLambdaResponse updateBookingsLambdaResponse = updateBookingsLambda
                    .updateBookings(updateBookingsLambdaRequest, context);
            String firstDate = updateBookingsLambdaResponse.getCurrentDate();

            websiteURL = "http://" + websiteBucket + ".s3-website-" + region + ".amazonaws.com?selectedDate="
                    + firstDate + ".html";

        } else if (requestType.equals("Delete")) {
            logger.log("Delete request - so removing bookings pages from website versioned S3 bucket");

            // We need to delete every version of every key before the bucket itself
            // can be deleted
            ListVersionsRequest listVersionsRequest = new ListVersionsRequest().withBucketName(websiteBucket);
            VersionListing versionListing;

            AmazonS3 client = TransferManagerBuilder.defaultTransferManager().getAmazonS3Client();
            do {
                versionListing = client.listVersions(listVersionsRequest);
                versionListing.getVersionSummaries().stream().filter(
                        // Maybe a bit slack, but '20' is to include e.g. 2015-10-04.html
                        k -> (k.getKey().startsWith("20") || k.getKey().equals("today.html"))).forEach(k -> {
                            logger.log("About to delete version: " + k.getVersionId() + " of booking page: "
                                    + k.getKey());
                            DeleteVersionRequest deleteVersionRequest = new DeleteVersionRequest(websiteBucket,
                                    k.getKey(), k.getVersionId());
                            client.deleteVersion(deleteVersionRequest);
                            logger.log("Successfully deleted version: " + k.getVersionId()
                                    + " of booking page: " + k.getKey());
                        });

                listVersionsRequest.setKeyMarker(versionListing.getNextKeyMarker());
            } while (versionListing.isTruncated());
            logger.log("Finished removing bookings pages from website S3 bucket");
        }

        responseStatus = "SUCCESS";
        return null;
    } catch (AmazonServiceException ase) {
        ExceptionUtils.logAmazonServiceException(ase, logger);
        return null;
    } catch (AmazonClientException ace) {
        ExceptionUtils.logAmazonClientException(ace, logger);
        return null;
    } catch (Exception e) {
        logger.log("Exception caught in NoScriptApp Lambda: " + e.getMessage());
        return null;
    } finally {
        // Send response to CloudFormation
        cloudFormationResponder.addKeyValueOutputsPair("WebsiteURL", websiteURL);
        cloudFormationResponder.sendResponse(responseStatus, logger);
    }
}