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

The following examples show how to use com.amazonaws.services.s3.AmazonS3#deleteObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AmazonS3FileSystemTestHelper.java    From iaf with Apache License 2.0 6 votes vote down vote up
public void cleanUpBucketAndShutDown(AmazonS3 s3Client) {
	if(s3Client.doesBucketExistV2(bucketName)) {
		 ObjectListing objectListing = s3Client.listObjects(bucketName);
            while (true) {
                Iterator<S3ObjectSummary> objIter = objectListing.getObjectSummaries().iterator();
                while (objIter.hasNext()) {
                    s3Client.deleteObject(bucketName, objIter.next().getKey());
                }
    
                // If the bucket contains many objects, the listObjects() call
                // might not return all of the objects in the first listing. Check to
                // see whether the listing was truncated. If so, retrieve the next page of objects 
                // and delete them.
                if (objectListing.isTruncated()) {
                    objectListing = s3Client.listNextBatchOfObjects(objectListing);
                } else {
                    break;
                }
            }
		s3Client.deleteBucket(bucketName);
	}
	if(s3Client != null) {
		s3Client.shutdown();
	}
}
 
Example 2
Source File: S3CheckpointSpiSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception If error.
 */
@Override protected void afterSpiStopped() throws Exception {
    AWSCredentials cred = new BasicAWSCredentials(IgniteS3TestSuite.getAccessKey(),
        IgniteS3TestSuite.getSecretKey());

    AmazonS3 s3 = new AmazonS3Client(cred);

    String bucketName = S3CheckpointSpi.BUCKET_NAME_PREFIX + "unit-test-bucket";

    try {
        ObjectListing list = s3.listObjects(bucketName);

        while (true) {
            for (S3ObjectSummary sum : list.getObjectSummaries())
                s3.deleteObject(bucketName, sum.getKey());

            if (list.isTruncated())
                list = s3.listNextBatchOfObjects(list);
            else
                break;
        }
    }
    catch (AmazonClientException e) {
        throw new IgniteSpiException("Failed to read checkpoint bucket: " + bucketName, e);
    }
}
 
Example 3
Source File: DeleteObject.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    final String USAGE = "\n" +
            "To run this example, supply the name of an S3 bucket and object\n" +
            "name (key) to delete.\n" +
            "\n" +
            "Ex: DeleteObject <bucketname> <objectname>\n";

    if (args.length < 2) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String bucket_name = args[0];
    String object_key = args[1];

    System.out.format("Deleting object %s from S3 bucket: %s\n", object_key,
            bucket_name);
    final AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRegion(Regions.DEFAULT_REGION).build();
    try {
        s3.deleteObject(bucket_name, object_key);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}
 
Example 4
Source File: AmazonS3Storage.java    From thunderbit with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public F.Promise<Void> delete(String key, String name) {
    Promise<Void> promise = Futures.promise();

    AmazonS3 amazonS3 = new AmazonS3Client(credentials);
    DeleteObjectRequest request = new DeleteObjectRequest(bucketName, key);
    request.withGeneralProgressListener(progressEvent -> {
        if (progressEvent.getEventType().isTransferEvent()) {
            if (progressEvent.getEventType().equals(ProgressEventType.TRANSFER_COMPLETED_EVENT)) {
                promise.success(null);
            } else if (progressEvent.getEventType().equals(ProgressEventType.TRANSFER_FAILED_EVENT)) {
                logger.error(progressEvent.toString());
                promise.failure(new Exception(progressEvent.toString()));
            }
        }
    });

    try {
        amazonS3.deleteObject(request);
    } catch (AmazonServiceException ase) {
        logAmazonServiceException (ase);
    } catch (AmazonClientException ace) {
        logAmazonClientException(ace);
    }

    return F.Promise.wrap(promise.future());
}
 
Example 5
Source File: TestUtil.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public static void createBucket(AmazonS3 s3client, String bucketName) {
  if(s3client.doesBucketExist(bucketName)) {
    for(S3ObjectSummary s : S3Objects.inBucket(s3client, bucketName)) {
      s3client.deleteObject(bucketName, s.getKey());
    }
    s3client.deleteBucket(bucketName);
  }
  Assert.assertFalse(s3client.doesBucketExist(bucketName));
  // Note that CreateBucketRequest does not specify region. So bucket is
  // bucketName
  s3client.createBucket(new CreateBucketRequest(bucketName));
}
 
Example 6
Source File: AmazonS3Util.java    From datacollector with Apache License 2.0 5 votes vote down vote up
static void copy(
    AmazonS3 s3Client,
    String srcBucket,
    String sourceKey,
    String destBucket,
    String destKey,
    boolean isMove
) {
  CopyObjectRequest cp = new CopyObjectRequest(srcBucket, sourceKey, destBucket, destKey);
  s3Client.copyObject(cp);
  if (isMove) {
    s3Client.deleteObject(new DeleteObjectRequest(srcBucket, sourceKey));
  }
}
 
Example 7
Source File: AmazonS3InstallationService.java    From java-slack-sdk with MIT License 5 votes vote down vote up
@Override
public void deleteBot(Bot bot) throws Exception {
    AmazonS3 s3 = this.createS3Client();
    String key = getBotKey(bot.getEnterpriseId(), bot.getTeamId());
    if (isHistoricalDataEnabled()) {
        key = key + "-latest";
    }
    s3.deleteObject(bucketName, key);
}
 
Example 8
Source File: SpecifyServerSideEncryption.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
private static void changeSSEEncryptionStatusByCopying(AmazonS3 s3Client,
                                                       String bucketName,
                                                       String sourceKey,
                                                       String destKey) {
    // Upload a new, unencrypted object.
    PutObjectResult putResult = s3Client.putObject(bucketName, sourceKey, "Object example to encrypt by copying");
    System.out.println("Unencrypted object \"" + sourceKey + "\" uploaded.");
    printEncryptionStatus(putResult);

    // Make a copy of the object and use server-side encryption when storing the copy.
    CopyObjectRequest request = new CopyObjectRequest(bucketName,
            sourceKey,
            bucketName,
            destKey);
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
    request.setNewObjectMetadata(objectMetadata);

    // Perform the copy operation and display the copy's encryption status.
    CopyObjectResult response = s3Client.copyObject(request);
    System.out.println("Object \"" + destKey + "\" uploaded with SSE.");
    printEncryptionStatus(response);

    // Delete the original, unencrypted object, leaving only the encrypted copy in Amazon S3.
    s3Client.deleteObject(bucketName, sourceKey);
    System.out.println("Unencrypted object \"" + sourceKey + "\" deleted.");
}
 
Example 9
Source File: Rename.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
public cfData execute( cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException{
AmazonKey amazonKey	= getAmazonKey(_session, argStruct);
AmazonS3 s3Client		= getAmazonS3(amazonKey);

	String bucket			= getNamedStringParam(argStruct, "bucket", null );
	String srckey			= getNamedStringParam(argStruct, "srckey", null );
	String deskey			= getNamedStringParam(argStruct, "destkey", null );
	String aes256key	= getNamedStringParam(argStruct, "aes256key", null );

if ( srckey != null && srckey.charAt( 0 ) == '/' )
	srckey	= srckey.substring(1);

if ( deskey != null && deskey.charAt( 0 ) == '/' )
	deskey	= deskey.substring(1);

	
	CopyObjectRequest cor = new CopyObjectRequest(bucket, srckey, bucket, deskey);
	
	if ( aes256key != null && !aes256key.isEmpty() ){
		cor.setSourceSSECustomerKey( new SSECustomerKey(aes256key) );
		cor.setDestinationSSECustomerKey( new SSECustomerKey(aes256key) );
	}
	

try {
	s3Client.copyObject(cor);
	s3Client.deleteObject(new DeleteObjectRequest(bucket, srckey));
	return cfBooleanData.TRUE;
} catch (Exception e) {
	throwException(_session, "AmazonS3: " + e.getMessage() );
	return cfBooleanData.FALSE;
}
}
 
Example 10
Source File: SpringCloudStreamAwsApplicationsITCase.java    From spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@Test
public void testS3Source() throws IOException, InterruptedException {
	String bucket = "TestBucket";
	String key = "foo";
	String content = "Spring Cloud Stream AWS S3 Source test";

	this.applicationContext = SpringApplication.run(S3SourceBootConfiguration.class,
			"--s3.remoteDir=" + bucket, "--file.consumer.mode=lines", "--file.consumer.with-markers=false");

	ResourceIdResolver resourceIdResolver = this.applicationContext.getBean(ResourceIdResolver.class);

	AmazonS3 amazonS3 = this.applicationContext.getBean(AmazonS3.class);
	ObjectMetadata objectMetadata = new ObjectMetadata();
	objectMetadata.setContentLength(content.length());
	String bucketName = resourceIdResolver.resolveToPhysicalResourceId(bucket);
	amazonS3.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes("UTF-8")), objectMetadata);

	try {
		Source source = this.applicationContext.getBean(Source.class);
		MessageCollector messageCollector = this.applicationContext.getBean(MessageCollector.class);
		Message<?> received = messageCollector.forChannel(source.output()).poll(10, TimeUnit.SECONDS);
		assertNotNull(received);
		assertThat(received, hasHeader(FileHeaders.FILENAME, key));
		assertThat(received, hasPayload("Spring Cloud Stream AWS S3 Source test"));
	}
	finally {
		amazonS3.deleteObject(bucketName, key);
	}
}
 
Example 11
Source File: S3EventProcessorUnzip.java    From aws-lambda-unzip with Apache License 2.0 4 votes vote down vote up
@Override
public String handleRequest(S3Event s3Event, Context context) {
    byte[] buffer = new byte[1024];
    try {
        for (S3EventNotificationRecord record: s3Event.getRecords()) {
            String srcBucket = record.getS3().getBucket().getName();

            // Object key may have spaces or unicode non-ASCII characters.
            String srcKey = record.getS3().getObject().getKey()
                    .replace('+', ' ');
            srcKey = URLDecoder.decode(srcKey, "UTF-8");

            // Detect file type
            Matcher matcher = Pattern.compile(".*\\.([^\\.]*)").matcher(srcKey);
            if (!matcher.matches()) {
                System.out.println("Unable to detect file type for key " + srcKey);
                return "";
            }
            String extension = matcher.group(1).toLowerCase();
            if (!"zip".equals(extension)) {
                System.out.println("Skipping non-zip file " + srcKey + " with extension " + extension);
                return "";
            }
            System.out.println("Extracting zip file " + srcBucket + "/" + srcKey);
            
            // Download the zip from S3 into a stream
            AmazonS3 s3Client = new AmazonS3Client();
            S3Object s3Object = s3Client.getObject(new GetObjectRequest(srcBucket, srcKey));
            ZipInputStream zis = new ZipInputStream(s3Object.getObjectContent());
            ZipEntry entry = zis.getNextEntry();

            while(entry != null) {
                String fileName = entry.getName();
                String mimeType = FileMimeType.fromExtension(FilenameUtils.getExtension(fileName)).mimeType();
                System.out.println("Extracting " + fileName + ", compressed: " + entry.getCompressedSize() + " bytes, extracted: " + entry.getSize() + " bytes, mimetype: " + mimeType);
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    outputStream.write(buffer, 0, len);
                }
                InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
                ObjectMetadata meta = new ObjectMetadata();
                meta.setContentLength(outputStream.size());
                meta.setContentType(mimeType);
                s3Client.putObject(srcBucket, FilenameUtils.getFullPath(srcKey) + fileName, is, meta);
                is.close();
                outputStream.close();
                entry = zis.getNextEntry();
            }
            zis.closeEntry();
            zis.close();
            
            //delete zip file when done
            System.out.println("Deleting zip file " + srcBucket + "/" + srcKey + "...");
            s3Client.deleteObject(new DeleteObjectRequest(srcBucket, srcKey));
            System.out.println("Done deleting");
        }
        return "Ok";
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 12
Source File: AWSCommon.java    From camel-kafka-connector with Apache License 2.0 4 votes vote down vote up
/**
 * Delete an S3 bucket using the provided client. Coming from AWS documentation:
 * https://docs.aws.amazon.com/AmazonS3/latest/dev/delete-or-empty-bucket.html#delete-bucket-sdk-java
 * @param s3Client the AmazonS3 client instance used to delete the bucket
 * @param bucketName a String containing the bucket name
 */
public static void deleteBucket(AmazonS3 s3Client, String bucketName) {
    // Delete all objects from the bucket. This is sufficient
    // for non versioned buckets. For versioned buckets, when you attempt to delete objects, Amazon S3 inserts
    // delete markers for all objects, but doesn't delete the object versions.
    // To delete objects from versioned buckets, delete all of the object versions before deleting
    // the bucket (see below for an example).
    ObjectListing objectListing = s3Client.listObjects(bucketName);
    while (true) {
        Iterator<S3ObjectSummary> objIter = objectListing.getObjectSummaries().iterator();
        while (objIter.hasNext()) {
            s3Client.deleteObject(bucketName, objIter.next().getKey());
        }

        // If the bucket contains many objects, the listObjects() call
        // might not return all of the objects in the first listing. Check to
        // see whether the listing was truncated. If so, retrieve the next page of objects
        // and delete them.
        if (objectListing.isTruncated()) {
            objectListing = s3Client.listNextBatchOfObjects(objectListing);
        } else {
            break;
        }
    }

    // Delete all object versions (required for versioned buckets).
    VersionListing versionList = s3Client.listVersions(new ListVersionsRequest().withBucketName(bucketName));
    while (true) {
        Iterator<S3VersionSummary> versionIter = versionList.getVersionSummaries().iterator();
        while (versionIter.hasNext()) {
            S3VersionSummary vs = versionIter.next();
            s3Client.deleteVersion(bucketName, vs.getKey(), vs.getVersionId());
        }

        if (versionList.isTruncated()) {
            versionList = s3Client.listNextBatchOfVersions(versionList);
        } else {
            break;
        }
    }

    // After all objects and object versions are deleted, delete the bucket.
    s3Client.deleteBucket(bucketName);
}
 
Example 13
Source File: S3SinkTestIT.java    From components with Apache License 2.0 4 votes vote down vote up
@After
public void after() {
    AmazonS3 s3_client = S3Connection.createClient(PropertiesPreparer.createS3OtuputProperties());
    s3_client.deleteObject(PropertiesPreparer.bucket, PropertiesPreparer.objectkey);
}
 
Example 14
Source File: DeleteBucket.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
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);
    }

    String bucket_name = args[0];

    System.out.println("Deleting S3 bucket: " + bucket_name);
    final AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRegion(Regions.DEFAULT_REGION).build();
    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!");
}
 
Example 15
Source File: S3Samples.java    From aws-sdk-java-samples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws IOException {
    //BEGIN_SAMPLE:AmazonS3.CreateClient
    //TITLE:Create an S3 client
    //DESCRIPTION:Create your credentials file at ~/.aws/credentials (C:\Users\USER_NAME\.aws\credentials for Windows users)
    AmazonS3 s3 = AmazonS3ClientBuilder.standard().build();
    Region usWest2 = com.amazonaws.regions.Region.getRegion(Regions.US_WEST_2);
    s3.setRegion(usWest2);
    //END_SAMPLE

    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 {
        System.out.println("Creating bucket " + bucketName + "\n");

        //BEGIN_SAMPLE:AmazonS3.CreateBucket
        //TITLE:Create an S3 bucket
        //DESCRIPTION: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.
        s3.createBucket(bucketName);
        //END_SAMPLE


        System.out.println("Listing buckets");
        //BEGIN_SAMPLE:AmazonS3.ListBuckets
        //TITLE:List buckets
        //DESCRIPTION:List the buckets in your account
        for (Bucket bucket : s3.listBuckets()) {
            System.out.println(" - " + bucket.getName());
        }
        System.out.println();
        //END_SAMPLE


        System.out.println("Uploading a new object to S3 from a file\n");
        //BEGIN_SAMPLE:AmazonS3.PutObject
        //TITLE:Upload an object to a bucket
        //DESCRIPTION:You can easily upload a file to S3, or upload directly an InputStream if you know the length of the data in the stream.
        s3.putObject(new PutObjectRequest(bucketName, key, createSampleFile()));
        //END_SAMPLE

        System.out.println("Downloading an object");
        //BEGIN_SAMPLE:AmazonS3.GetObject
        //TITLE:Download an S3 object.
        //DESCRIPTION:When you download an object, you get all of the object's metadata and a stream from which to read the contents.
        S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
        //END_SAMPLE
        System.out.println("Content-Type: "  + object.getObjectMetadata().getContentType());
        displayTextInputStream(object.getObjectContent());


        System.out.println("Listing objects");

        //BEGIN_SAMPLE:AmazonS3.ListObjects
        //TITLE:List S3 objects in bucket
        //DESCRIPTION:List objects in your bucket by prefix.  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.
        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();
        //END_SAMPLE


        System.out.println("Deleting an object\n");

        //BEGIN_SAMPLE:AmazonS3.DeleteObject
        //TITLE:Delete an S3 object
        //DESCRIPTION:Unless versioning has been turned on for your bucket, there is no way to undelete an object, so use caution when deleting objects.
        s3.deleteObject(bucketName, key);
        //END_SAMPLE


        System.out.println("Deleting bucket " + bucketName + "\n");

        //BEGIN_SAMPLE:AmazonS3.DeleteBucket
        //TITLE:Delete an S3 bucket
        //DESCRIPTION: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.
        s3.deleteBucket(bucketName);
        //END_SAMPLE
    } 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());
    }
}
 
Example 16
Source File: AmazonS3OAuthStateService.java    From java-slack-sdk with MIT License 4 votes vote down vote up
@Override
public void deleteStateFromDatastore(String state) throws Exception {
    AmazonS3 s3 = this.createS3Client();
    s3.deleteObject(bucketName, getKey(state));
}
 
Example 17
Source File: S3Util.java    From s3committer with Apache License 2.0 4 votes vote down vote up
public static void revertCommit(AmazonS3 client,
                                PendingUpload commit) {
  client.deleteObject(commit.newDeleteRequest());
}
 
Example 18
Source File: Delete.java    From cloudExplorer with GNU General Public License v3.0 4 votes vote down vote up
public void run() {
    AWSCredentials credentials = new BasicAWSCredentials(access_key, secret_key);
    AmazonS3 s3Client = new AmazonS3Client(credentials,
            new ClientConfiguration());
    if (endpoint.contains("amazonaws.com")) {
        String aws_endpoint = s3Client.getBucketLocation(new GetBucketLocationRequest(bucket));
        if (aws_endpoint.contains("US")) {
            s3Client.setEndpoint("https://s3.amazonaws.com");
        } else if (aws_endpoint.contains("us-west")) {
            s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
        } else if (aws_endpoint.contains("eu-west")) {
            s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
        } else if (aws_endpoint.contains("ap-")) {
            s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
        } else if (aws_endpoint.contains("sa-east-1")) {
            s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
        } else {
            s3Client.setEndpoint("https://s3." + aws_endpoint + ".amazonaws.com");
        }
    } else {
        s3Client.setS3ClientOptions(S3ClientOptions.builder().setPathStyleAccess(true).build());
        s3Client.setEndpoint(endpoint);
    }

    try {

        if (version != null) {
            s3Client.deleteVersion(new DeleteVersionRequest(bucket, what, version));
        } else {
            s3Client.deleteObject(new DeleteObjectRequest(bucket, what));
        }
        if (!debug) {
            NewJFrame.jTextArea1.append("\nDeleted file: " + what);
        }
        calibrate();
    } catch (AmazonServiceException ase) {
        if (NewJFrame.gui) {
            mainFrame.jTextArea1.append("\n\nError Message:    " + ase.getMessage());
            mainFrame.jTextArea1.append("\nHTTP Status Code: " + ase.getStatusCode());
            mainFrame.jTextArea1.append("\nAWS Error Code:   " + ase.getErrorCode());
            mainFrame.jTextArea1.append("\nError Type:       " + ase.getErrorType());
            mainFrame.jTextArea1.append("\nRequest ID:       " + ase.getRequestId());
            calibrate();
        } else {
            System.out.print("\n\nError Message:    " + ase.getMessage());
            System.out.print("\nHTTP Status Code: " + ase.getStatusCode());
            System.out.print("\nAWS Error Code:   " + ase.getErrorCode());
            System.out.print("\nError Type:       " + ase.getErrorType());
            System.out.print("\nRequest ID:       " + ase.getRequestId());
        }
    } catch (Exception delete) {
    }
}
 
Example 19
Source File: CloudFormationClient.java    From herd-mdl with Apache License 2.0 4 votes vote down vote up
/**
 * Delete the stack {@link #stackName}
 */
public void deleteStack() throws Exception {

    CFTStackInfo cftStackInfo = getStackInfo();
    String rootStackId = cftStackInfo.stackId(); // Use the stack id to track the delete operation
    LOGGER.info("rootStackId   =   " + rootStackId);

    // Go through the stack and pick up resources that we want
    // to finalize before deleting the stack.
    List<String> s3BucketIds = new ArrayList<>();

    DescribeStacksResult describeStacksResult = amazonCloudFormation.describeStacks();
    for (Stack currentStack : describeStacksResult.getStacks()) {
        if (rootStackId.equals(currentStack.getRootId()) || rootStackId
                .equals(currentStack.getStackId())) {
            LOGGER.info("stackId   =   " + currentStack.getStackId());
            DescribeStackResourcesRequest describeStackResourcesRequest = new DescribeStackResourcesRequest();
            describeStackResourcesRequest.setStackName(currentStack.getStackName());
            List<StackResource> stackResources = amazonCloudFormation
                    .describeStackResources(describeStackResourcesRequest).getStackResources();
            for (StackResource stackResource : stackResources) {
                if (!stackResource.getResourceStatus()
                        .equals(ResourceStatus.DELETE_COMPLETE.toString())) {
                    if (stackResource.getResourceType().equals("AWS::S3::Bucket")) {
                        s3BucketIds.add(stackResource.getPhysicalResourceId());
                    }
                }
            }
        }
    }

    // Now empty S3 buckets, clean up will be done when the stack is deleted
    AmazonS3 amazonS3 = AmazonS3ClientBuilder.standard().withRegion(Regions.getCurrentRegion().getName())
            .withCredentials(new InstanceProfileCredentialsProvider(true)).build();
    for (String s3BucketPhysicalId : s3BucketIds) {
        String s3BucketName = s3BucketPhysicalId;
        if(!amazonS3.doesBucketExistV2(s3BucketName)){
            break;
        }
        LOGGER.info("Empyting S3 bucket, " + s3BucketName);
        ObjectListing objectListing = amazonS3.listObjects(s3BucketName);
        while (true) {
            for (Iterator<?> iterator = objectListing.getObjectSummaries().iterator(); iterator
                    .hasNext(); ) {
                S3ObjectSummary summary = (S3ObjectSummary) iterator.next();
                amazonS3.deleteObject(s3BucketName, summary.getKey());
            }
            if (objectListing.isTruncated()) {
                objectListing = amazonS3.listNextBatchOfObjects(objectListing);
            }
            else {
                break;
            }
        }
    }

    //Proceed with the regular stack deletion operation
    DeleteStackRequest deleteRequest = new DeleteStackRequest();
    deleteRequest.setStackName(stackName);
    amazonCloudFormation.deleteStack(deleteRequest);
    LOGGER.info("Stack deletion initiated");

    CFTStackStatus cftStackStatus = waitForCompletionAndGetStackStatus(amazonCloudFormation,
            rootStackId);
    LOGGER.info(
            "Stack deletion completed, the stack " + stackName + " completed with " + cftStackStatus);

    // Throw exception if failed
    if (!cftStackStatus.getStackStatus().equals(StackStatus.DELETE_COMPLETE.toString())) {
        throw new Exception(
                "deleteStack operation failed for stack " + stackName + " - " + cftStackStatus);
    }
}
 
Example 20
Source File: S3BucketService.java    From cassandra-backup with Apache License 2.0 4 votes vote down vote up
private void delete(final AmazonS3 s3Client, final ObjectListing objectListing, final String bucketName) {
    for (final S3ObjectSummary summary : objectListing.getObjectSummaries()) {
        s3Client.deleteObject(bucketName, summary.getKey());
    }
}