Java Code Examples for com.amazonaws.services.s3.AmazonS3.deleteBucket()

The following are Jave code examples for showing how to use deleteBucket() of the com.amazonaws.services.s3.AmazonS3 class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: ats-framework   File: S3Operations.java   View Source Code Vote up 5 votes
/**
 * Delete the bucket specified in constructor
 *
 * @throws S3OperationException in case of a client or server error
 */
@PublicAtsApi
public void deleteBucket() throws S3OperationException {

    AmazonS3 s3Client = getClient();
    // TODO: check for direct method
    // remove all Objects before deleting the bucket
    for (S3ObjectInfo element : listBucket("" /* all */, ".*", true)) {
        deleteObject(element.getName());
    }

    s3Client.deleteBucket(bucketName);
    LOG.info("Deleted bucket '" + bucketName + "'");
}
 
Example 2
Project: alfresco-lambda-java-utils   File: S3Cleaner.java   View Source Code Vote up 5 votes
/**
 * Empties the contents of the given bucket and optionally deletes the bucket
 * 
 * @param s3 The S3 client object to use
 * @param bucketName The bucket to empty
 * @param deleteBucket Flag to determine whether to delete the bucket after emptying
 * @param context Lambda context object
 */
public static void emptyBucket(AmazonS3 s3, String bucketName, boolean deleteBucket, Context context)
{
    if (s3.doesBucketExistV2(bucketName))
    {
        logDebug("Emptying bucket '" + bucketName + "'...", context);
        
        VersionListing versionListing = s3.listVersions(new ListVersionsRequest().withBucketName(bucketName));
        while (true) 
        {
            for (S3VersionSummary vs : versionListing.getVersionSummaries()) 
            {
                s3.deleteVersion(bucketName, vs.getKey(), vs.getVersionId());
            }
            
            if (versionListing.isTruncated()) 
            {
                logDebug("Fetching next batch of versions for bucket '" + bucketName + "'", context);
                versionListing = s3.listNextBatchOfVersions(versionListing);
            } 
            else 
            {
                break;
            }
        }
        
        logInfo("Successfully emptied bucket '" + bucketName + "'", context);
        
        if (deleteBucket)
        {
            logDebug("Deleting bucket '" + bucketName + "'...", context);
            s3.deleteBucket(bucketName);
            logInfo("Successfully deleted bucket '" + bucketName + "'", context);
        }
    }
    else
    {
        logWarn("Bucket '" + bucketName + "' does not exist", context);
    }
}