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

The following examples show how to use com.amazonaws.services.s3.AmazonS3#deleteObjects() . 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: pinpoint_list_endpoint_ids.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
private static void deleteS3Objects(String s3BucketName, List<String> keys) {

        AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient();

        String[] keysArray = keys.toArray(new String[keys.size()]);
        DeleteObjectsRequest request = new DeleteObjectsRequest(s3BucketName).withKeys(keysArray);

        System.out.println("Deleting the following Amazon S3 objects created by Amazon Pinpoint:");

        for (String key : keys) {
            System.out.println("\t- " + key);
        }

        try {
            s3Client.deleteObjects(request);
        } catch (AmazonServiceException e) {
            System.err.println(e.getErrorMessage());
            System.exit(1);
        }

        System.out.println("Finished deleting objects.");
    }
 
Example 2
Source File: PrimitiveS3OperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void deleteObjects(AmazonS3 service, ObjectListing listing) throws MultiObjectDeleteException, IOException {
	do {
		if (Thread.currentThread().isInterrupted()) {
			throw new IOException(FileOperationMessages.getString("IOperationHandler.interrupted")); //$NON-NLS-1$
		}
		List<S3ObjectSummary> objectSummaries = listing.getObjectSummaries();
		if (!objectSummaries.isEmpty()) {
			List<KeyVersion> keys = new ArrayList<KeyVersion>(objectSummaries.size());
			for (S3ObjectSummary object: objectSummaries) {
				keys.add(new KeyVersion(object.getKey()));
			}
			DeleteObjectsRequest request = new DeleteObjectsRequest(listing.getBucketName()).withKeys(keys).withQuiet(true);
			service.deleteObjects(request); // quiet
		}
		listing = service.listNextBatchOfObjects(listing);
	} while (listing.isTruncated());
}
 
Example 3
Source File: S3Uploader.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Delete files.
 *
 * @param s3client the s 3 client
 * @param s3Bucket the s 3 bucket
 * @param folder the folder
 */
private void deleteFiles(AmazonS3 s3client,String s3Bucket,String folder){
	
	String[] keys = listKeys(s3client,s3Bucket,folder);
	DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(s3Bucket).withKeys((keys));
	
	try{
		DeleteObjectsResult result = s3client.deleteObjects(multiObjectDeleteRequest);
		log.debug("Files Deleted " +result.getDeletedObjects().stream().map(obj->obj.getKey()).collect(Collectors.toList()));
	}catch(Exception e){
		log.error("Delete Failed",e);
		ErrorManageUtil.uploadError("all", "all", "all", e.getMessage());
	}
}
 
Example 4
Source File: S3Uploader.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Delete files.
 *
 * @param s3client the s 3 client
 * @param s3Bucket the s 3 bucket
 * @param folder the folder
 */
private void deleteFiles(AmazonS3 s3client,String s3Bucket,String folder){
	
	String[] keys = listKeys(s3client,s3Bucket,folder);
	DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(s3Bucket).withKeys((keys));
	
	try{
		DeleteObjectsResult result = s3client.deleteObjects(multiObjectDeleteRequest);
		log.debug("Files Deleted " +result.getDeletedObjects().stream().map(obj->obj.getKey()).collect(Collectors.toList()));
	}catch(Exception e){
		log.error("Delete Failed",e);
		ErrorManageUtil.uploadError("all", "all", "all", e.getMessage());
	}
}
 
Example 5
Source File: DeleteObjects.java    From aws-doc-sdk-examples with Apache License 2.0 5 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 at least\n" +
            "one object name (key) to delete.\n" +
            "\n" +
            "Ex: DeleteObjects <bucketname> <objectname1> [objectname2, ...]\n";

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

    String bucket_name = args[0];
    String[] object_keys = Arrays.copyOfRange(args, 1, args.length);

    System.out.println("Deleting objects from S3 bucket: " + bucket_name);
    for (String k : object_keys) {
        System.out.println(" * " + k);
    }

    final AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRegion(Regions.DEFAULT_REGION).build();
    try {
        DeleteObjectsRequest dor = new DeleteObjectsRequest(bucket_name)
                .withKeys(object_keys);
        s3.deleteObjects(dor);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}
 
Example 6
Source File: S3OperationsImpl.java    From herd with Apache License 2.0 4 votes vote down vote up
@Override
public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest, AmazonS3 s3Client)
{
    return s3Client.deleteObjects(deleteObjectsRequest);
}