List of usage examples for com.amazonaws.services.s3 AmazonS3 deleteBucket
public void deleteBucket(String bucketName) throws SdkClientException, AmazonServiceException;
Deletes the specified bucket.
From source file:ecplugins.s3.S3Util.java
License:Apache License
/** * This procedure deletes the bucket along with its contents * @param bucketName//from w ww . j av a 2 s. c o m * @return * @throws Exception */ public static boolean DeleteBucket(String bucketName) throws Exception { Properties props = TestUtils.getProperties(); BasicAWSCredentials credentials = new BasicAWSCredentials(props.getProperty(StringConstants.ACCESS_ID), props.getProperty(StringConstants.SECRET_ACCESS_ID)); // Create TransferManager TransferManager tx = new TransferManager(credentials); // Get S3 Client AmazonS3 s3 = tx.getAmazonS3Client(); if (s3.doesBucketExist(bucketName)) { // Multi-object delete by specifying only keys (no version ID). DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(bucketName).withQuiet(false); //get keys List<String> keys = new ArrayList<String>(); ObjectListing objectListing = s3.listObjects(new ListObjectsRequest().withBucketName(bucketName)); for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { keys.add(objectSummary.getKey()); } // Create request that include only object key names. List<DeleteObjectsRequest.KeyVersion> justKeys = new ArrayList<DeleteObjectsRequest.KeyVersion>(); for (String key : keys) { justKeys.add(new DeleteObjectsRequest.KeyVersion(key)); } if (justKeys.size() == 0) { return false; } multiObjectDeleteRequest.setKeys(justKeys); // Execute DeleteObjects - Amazon S3 add delete marker for each object // deletion. The objects no disappear from your bucket (verify). DeleteObjectsResult delObjRes = null; delObjRes = s3.deleteObjects(multiObjectDeleteRequest); s3.deleteBucket(bucketName); return true; } else { System.out.println("Error: Bucket with name " + bucketName + " does not exists."); return false; } }
From source file:exemplos.S3Sample.java
License:Open Source License
public static void main(String[] args) throws IOException { /*/* w w w . ja va 2s .com*/ * This credentials provider implementation loads your AWS credentials * from a properties file at the root of your classpath. * * Important: Be sure to fill in your AWS access credentials in the * AwsCredentials.properties file before you try to run this * sample. * http://aws.amazon.com/security-credentials */ AmazonS3 s3 = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider()); Region usWest2 = Region.getRegion(Regions.US_WEST_2); s3.setRegion(usWest2); String bucketName = "my-first-s3-bucket-" + UUID.randomUUID(); String key = "MyObjectKey"; System.out.println("==========================================="); System.out.println("Getting Started with Amazon S3"); System.out.println("===========================================\n"); try { /* * Create a new S3 bucket - Amazon S3 bucket names are globally unique, * so once a bucket name has been taken by any user, you can't create * another bucket with that same name. * * You can optionally specify a location for your bucket if you want to * keep your data closer to your applications or users. */ System.out.println("Creating bucket " + bucketName + "\n"); s3.createBucket(bucketName); /* * List the buckets in your account */ System.out.println("Listing buckets"); for (Bucket bucket : s3.listBuckets()) { System.out.println(" - " + bucket.getName()); } System.out.println(); /* * Upload an object to your bucket - You can easily upload a file to * S3, or upload directly an InputStream if you know the length of * the data in the stream. You can also specify your own metadata * when uploading to S3, which allows you set a variety of options * like content-type and content-encoding, plus additional metadata * specific to your applications. */ System.out.println("Uploading a new object to S3 from a file\n"); s3.putObject(new PutObjectRequest(bucketName, key, createSampleFile())); /* * Download an object - When you download an object, you get all of * the object's metadata and a stream from which to read the contents. * It's important to read the contents of the stream as quickly as * possibly since the data is streamed directly from Amazon S3 and your * network connection will remain open until you read all the data or * close the input stream. * * GetObjectRequest also supports several other options, including * conditional downloading of objects based on modification times, * ETags, and selectively downloading a range of an object. */ System.out.println("Downloading an object"); S3Object object = s3.getObject(new GetObjectRequest(bucketName, key)); System.out.println("Content-Type: " + object.getObjectMetadata().getContentType()); displayTextInputStream(object.getObjectContent()); /* * List objects in your bucket by prefix - There are many options for * listing the objects in your bucket. Keep in mind that buckets with * many objects might truncate their results when listing their objects, * so be sure to check if the returned object listing is truncated, and * use the AmazonS3.listNextBatchOfObjects(...) operation to retrieve * additional results. */ System.out.println("Listing objects"); ObjectListing objectListing = s3 .listObjects(new ListObjectsRequest().withBucketName(bucketName).withPrefix("My")); for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { System.out.println( " - " + objectSummary.getKey() + " " + "(size = " + objectSummary.getSize() + ")"); } System.out.println(); /* * Delete an object - Unless versioning has been turned on for your bucket, * there is no way to undelete an object, so use caution when deleting objects. */ System.out.println("Deleting an object\n"); s3.deleteObject(bucketName, key); /* * Delete a bucket - A bucket must be completely empty before it can be * deleted, so remember to delete any objects from your buckets before * you try to delete them. */ System.out.println("Deleting bucket " + bucketName + "\n"); s3.deleteBucket(bucketName); } 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 " + "a serious internal problem while trying to communicate with S3, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:ics.uci.edu.amazons3.S3Sample.java
License:Open Source License
public static void main(String[] args) throws IOException { /*/*from w ww . ja v a 2s. co m*/ * This credentials provider implementation loads your AWS credentials * from a properties file at the root of your classpath. * * Important: Be sure to fill in your AWS access credentials in the * AwsCredentials.properties file before you try to run this * sample. * http://aws.amazon.com/security-credentials */ final AmazonS3 s3 = new AmazonS3Client( new BasicAWSCredentials("AKIAJTW5BOY6EXOGV2YQ", "PDcnFYIf9Hdo9GsKTEjLXretZ3yEg4mRCDQKjxu6")); String bucketName = "my-first-s3-bucket-" + UUID.randomUUID(); String key = "MyObjectKey"; System.out.println("==========================================="); System.out.println("Getting Started with Amazon S3"); System.out.println("===========================================\n"); try { /* * Create a new S3 bucket - Amazon S3 bucket names are globally unique, * so once a bucket name has been taken by any user, you can't create * another bucket with that same name. * * You can optionally specify a location for your bucket if you want to * keep your data closer to your applications or users. */ System.out.println("Creating bucket " + bucketName + "\n"); s3.createBucket(bucketName); /* * List the buckets in your account */ System.out.println("Listing buckets"); for (Bucket bucket : s3.listBuckets()) { System.out.println(" - " + bucket.getName()); } System.out.println(); /* * Upload an object to your bucket - You can easily upload a file to * S3, or upload directly an InputStream if you know the length of * the data in the stream. You can also specify your own metadata * when uploading to S3, which allows you set a variety of options * like content-type and content-encoding, plus additional metadata * specific to your applications. */ System.out.println("Uploading a new object to S3 from a file\n"); s3.putObject(new PutObjectRequest(bucketName, key, createSampleFile())); /* * Download an object - When you download an object, you get all of * the object's metadata and a stream from which to read the contents. * It's important to read the contents of the stream as quickly as * possibly since the data is streamed directly from Amazon S3 and your * network connection will remain open until you read all the data or * close the input stream. * * GetObjectRequest also supports several other options, including * conditional downloading of objects based on modification times, * ETags, and selectively downloading a range of an object. */ System.out.println("Downloading an object"); S3Object object = s3.getObject(new GetObjectRequest(bucketName, key)); System.out.println("Content-Type: " + object.getObjectMetadata().getContentType()); displayTextInputStream(object.getObjectContent()); /* * List objects in your bucket by prefix - There are many options for * listing the objects in your bucket. Keep in mind that buckets with * many objects might truncate their results when listing their objects, * so be sure to check if the returned object listing is truncated, and * use the AmazonS3.listNextBatchOfObjects(...) operation to retrieve * additional results. */ System.out.println("Listing objects"); ObjectListing objectListing = s3 .listObjects(new ListObjectsRequest().withBucketName(bucketName).withPrefix("My")); for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { System.out.println( " - " + objectSummary.getKey() + " " + "(size = " + objectSummary.getSize() + ")"); } System.out.println(); /* * Delete an object - Unless versioning has been turned on for your bucket, * there is no way to undelete an object, so use caution when deleting objects. */ System.out.println("Deleting an object\n"); s3.deleteObject(bucketName, key); /* * Delete a bucket - A bucket must be completely empty before it can be * deleted, so remember to delete any objects from your buckets before * you try to delete them. */ System.out.println("Deleting bucket " + bucketName + "\n"); s3.deleteBucket(bucketName); } 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 " + "a serious internal problem while trying to communicate with S3, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:io.konig.camel.aws.s3.DeleteObjectProducer.java
License:Apache License
private void deleteBucket(AmazonS3 s3Client, Exchange exchange) { String bucketName;//from w w w. j a va2 s . c om bucketName = exchange.getIn().getHeader(S3Constants.BUCKET_NAME, String.class); if (ObjectHelper.isEmpty(bucketName)) { bucketName = getConfiguration().getBucketName(); } DeleteBucketRequest deleteBucketRequest = new DeleteBucketRequest(bucketName); s3Client.deleteBucket(deleteBucketRequest); }
From source file:io.konig.maven.DeleteAwsS3BucketAction.java
License:Apache License
public AwsDeployment from(String path) throws Exception { String cfTemplatePresent = System.getProperty("cfTemplatePresent"); if (cfTemplatePresent == null || cfTemplatePresent.equals("N")) { try {/*from w ww . j av a2 s .c om*/ File file = deployment.file(path); ObjectMapper mapper = new ObjectMapper(); S3Bucket bucket = mapper.readValue(file, S3Bucket.class); deployment.verifyAWSCredentials(); Regions regions = Regions.fromName(bucket.getRegion()); AmazonS3 s3client = AmazonS3ClientBuilder.standard().withCredentials(deployment.getCredential()) .withRegion(regions).build(); String envtName = ""; if (System.getProperty("environmentName") != null) { envtName = System.getProperty("environmentName"); } String bucketName = StringUtils.replaceOnce(bucket.getBucketName(), "${environmentName}", envtName); s3client.deleteBucket(bucketName); boolean status = s3client.doesBucketExistV2(bucketName); if (!status) deployment.setResponse("AWS S3 Bucket is deleted ::" + bucketName); } catch (Exception e) { throw e; } } else { deployment.setResponse("S3 Bucket will be deleted through cloud formation stack"); } return deployment; }
From source file:jp.classmethod.aws.gradle.s3.DeleteBucketTask.java
License:Apache License
@TaskAction public void deleteBucket() { // to enable conventionMappings feature String bucketName = getBucketName(); boolean ifExists = isIfExists(); if (bucketName == null) { throw new GradleException("bucketName is not specified"); }/*from w ww .ja v a 2 s. c o m*/ AmazonS3PluginExtension ext = getProject().getExtensions().getByType(AmazonS3PluginExtension.class); AmazonS3 s3 = ext.getClient(); if (ifExists == false || exists(s3)) { if (deleteObjects) { getLogger().info("Delete all S3 objects in bucket [{}]", bucketName); ObjectListing objectListing = s3.listObjects(bucketName); while (objectListing.getObjectSummaries().isEmpty() == false) { objectListing.getObjectSummaries().forEach(summary -> { getLogger().info(" => delete s3://{}/{}", bucketName, summary.getKey()); s3.deleteObject(bucketName, summary.getKey()); }); objectListing = s3.listNextBatchOfObjects(objectListing); } } s3.deleteBucket(bucketName); getLogger().info("S3 bucket {} is deleted", bucketName); } else { getLogger().debug("S3 bucket {} does not exist", bucketName); } }
From source file:mail.server.storage.AWSStorageDelete.java
License:GNU General Public License
public void delete(String bucketName, String awsAccessKeyId, String awsSecretKey) throws Exception { log.debug("will delete", bucketName); SimpleAWSCredentials credentials = new SimpleAWSCredentials(awsAccessKeyId, awsSecretKey); AmazonS3 s3 = new AmazonS3Client(credentials); AmazonIdentityManagement im = new AmazonIdentityManagementClient(credentials); log.debug("deriving names"); deriveNames(bucketName);//from w w w. j a v a 2 s . com deleteUser(im, groupName, readWriteIdentity, policyReadWriteName, false); deleteUser(im, groupName, writeIdentity, policyWriteName, false); log.debug("deleting group", groupName); im.deleteGroup(new DeleteGroupRequest().withGroupName(groupName)); deleteBucketContents(s3, bucketName); log.debug("deleting bucket"); s3.deleteBucket(new DeleteBucketRequest(bucketName)); }
From source file:org.alanwilliamson.amazon.s3.DeleteBucket.java
License:Open Source License
public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException { AmazonKey amazonKey = getAmazonKey(_session, argStruct); AmazonS3 s3Client = getAmazonS3(amazonKey); String bucket = getNamedStringParam(argStruct, "bucket", null); try {/*from www .ja v a2s . c om*/ s3Client.deleteBucket(bucket.toLowerCase()); } catch (Exception e) { throwException(_session, "AmazonS3: " + e.getMessage()); } return cfBooleanData.TRUE; }
From source file:org.apache.jackrabbit.aws.ext.Utils.java
License:Apache License
/** * Delete S3 bucket. This method first deletes all objects from bucket and * then delete empty bucket.//from w ww. ja v a2 s . c om * * @param bucketName the bucket name. */ public static void deleteBucket(final String bucketName) throws IOException { Properties prop = readConfig(DEFAULT_CONFIG_FILE); AmazonS3 s3service = openService(prop); ObjectListing prevObjectListing = s3service.listObjects(bucketName); while (true) { for (S3ObjectSummary s3ObjSumm : prevObjectListing.getObjectSummaries()) { s3service.deleteObject(bucketName, s3ObjSumm.getKey()); } if (!prevObjectListing.isTruncated()) { break; } prevObjectListing = s3service.listNextBatchOfObjects(prevObjectListing); } s3service.deleteBucket(bucketName); }
From source file:org.boriken.s3fileuploader.S3SampleRefactored.java
License:Open Source License
public static void deleteBucket(AmazonS3 s3, String bucketName) { /*// w w w . j a v a 2s. co m * Delete a bucket - A bucket must be completely empty before it can be * deleted, so remember to delete any objects from your buckets before * you try to delete them. */ System.out.println("Deleting bucket " + bucketName + "\n"); s3.deleteBucket(bucketName); }