List of usage examples for com.amazonaws.services.s3 AmazonS3 deleteObjects
public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest) throws SdkClientException, AmazonServiceException;
From source file:aws.example.s3.DeleteObjects.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 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);/*from w w w . j a v a2s. co m*/ } 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 = new AmazonS3Client(); 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!"); }
From source file:awslabs.lab21.SolutionCode.java
License:Open Source License
@Override public void deleteBucket(AmazonS3 s3Client, String bucketName) { // First, try to delete the bucket. DeleteBucketRequest deleteBucketRequest = new DeleteBucketRequest(bucketName); try {//from w w w . j a v a 2s . c om s3Client.deleteBucket(deleteBucketRequest); // If we got here, no error was generated so we'll assume the bucket was deleted and return. return; } catch (AmazonS3Exception ex) { if (!ex.getErrorCode().equals("BucketNotEmpty")) { // The only other exception we're going to handle is BucketNotEmpty, so rethrow anything else. throw ex; } } // If we got here, the bucket isn't empty, so delete the contents and try again. List<KeyVersion> keys = new ArrayList<KeyVersion>(); for (S3ObjectSummary obj : s3Client.listObjects(bucketName).getObjectSummaries()) { // Add the keys to our list of object. keys.add(new KeyVersion(obj.getKey())); } // Create the request to delete the objects. DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName); deleteObjectsRequest.withKeys(keys); // Submit the delete objects request. s3Client.deleteObjects(deleteObjectsRequest); // The bucket is empty now, so attempt the delete again. s3Client.deleteBucket(deleteBucketRequest); }
From source file:com.easarrive.aws.plugins.common.service.impl.S3Service.java
License:Open Source License
/** * {@inheritDoc}// w w w . j a v a 2 s. co m */ @Override public DeleteObjectsResult deleteObjects(AmazonS3 client, String bucketName, List<String> keyList) { if (client == null) { return null; } else if (StringUtil.isEmpty(bucketName)) { return null; } else if (keyList == null || keyList.size() < 1) { return null; } DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName); List<KeyVersion> keys = new ArrayList<KeyVersion>(); for (String key : keyList) { KeyVersion keyVersion = new KeyVersion(key); keys.add(keyVersion); } deleteObjectsRequest.setKeys(keys); DeleteObjectsResult result = client.deleteObjects(deleteObjectsRequest); return result; }
From source file:com.easarrive.aws.plugins.common.service.impl.S3Service.java
License:Open Source License
/** * {@inheritDoc}//from w w w . j a v a2s. c o m */ public DeleteObjectsResult deleteObjects2(AmazonS3 client, String bucketName, List<KeyVersion> keyList) { if (client == null) { return null; } else if (StringUtil.isEmpty(bucketName)) { return null; } else if (keyList == null || keyList.size() < 1) { return null; } DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName); deleteObjectsRequest.setKeys(keyList); DeleteObjectsResult result = client.deleteObjects(deleteObjectsRequest); return result; }
From source file:com.images3.data.impl.ImageContentAccessImplS3.java
License:Apache License
private void deleteAllImageContent(AmazonS3 client, AmazonS3Bucket bucket, ObjectListing objList) { boolean isFinished = false; while (!isFinished) { DeleteObjectsRequest request = new DeleteObjectsRequest(bucket.getName()); List<KeyVersion> keys = new ArrayList<KeyVersion>(objList.getMaxKeys()); for (S3ObjectSummary sum : objList.getObjectSummaries()) { keys.add(new KeyVersion(sum.getKey())); }//from w ww . j a v a 2s . c om request.setKeys(keys); client.deleteObjects(request); if (objList.isTruncated()) { objList = client.listNextBatchOfObjects(objList); } else { isFinished = true; } } }
From source file:ecplugins.s3.S3Util.java
License:Apache License
/** * This procedure deletes the bucket along with its contents * @param bucketName/* w ww .j a v a2 s . c om*/ * @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:mail.server.storage.AWSStorageDelete.java
License:GNU General Public License
protected void deleteBucketContents(AmazonS3 s3, String bucketName) throws Exception { while (true) { List<String> keys = new ArrayList<String>(); log.debug("creating batch delete"); ObjectListing listing = s3.listObjects(bucketName); for (S3ObjectSummary i : listing.getObjectSummaries()) { log.debug("key", i.getKey()); keys.add(i.getKey());//ww w .j a va 2s . com } if (keys.isEmpty()) break; DeleteObjectsRequest req = new DeleteObjectsRequest(bucketName).withKeys(keys.toArray(new String[0])); log.debug("deleting"); s3.deleteObjects(req); } }
From source file:net.solarnetwork.node.backup.s3.SdkS3Client.java
License:Open Source License
@Override public void deleteObjects(Set<String> keys) throws IOException { AmazonS3 client = getClient(); try {//from w w w . j a v a 2s . c o m DeleteObjectsRequest req = new DeleteObjectsRequest(bucketName) .withKeys(keys.stream().map(k -> new KeyVersion(k)).collect(Collectors.toList())); client.deleteObjects(req); } catch (AmazonServiceException e) { log.warn("AWS error: {}; HTTP code {}; AWS code {}; type {}; request ID {}", e.getMessage(), e.getStatusCode(), e.getErrorCode(), e.getErrorType(), e.getRequestId()); throw new RemoteServiceException("Error deleting S3 objects " + keys, e); } catch (AmazonClientException e) { log.debug("Error communicating with AWS: {}", e.getMessage()); throw new IOException("Error communicating with AWS", e); } }
From source file:org.alanwilliamson.amazon.s3.Delete.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); if (bucket == null) throwException(_session, "Please specify a bucket"); cfData key = getNamedParam(argStruct, "key"); try {/*from w ww . j av a2 s .co m*/ if (key.getDataType() == cfData.CFARRAYDATA) { DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(bucket); List keysT = new ArrayList(); cfArrayData arrData = (cfArrayData) key; for (int x = 0; x < arrData.size(); x++) { String k = arrData.getData(x + 1).toString(); if (k.charAt(0) == '/') k = k.substring(1); keysT.add(new KeyVersion(k)); } multiObjectDeleteRequest.setKeys(keysT); DeleteObjectsResult delObjRes = s3Client.deleteObjects(multiObjectDeleteRequest); return new cfNumberData(delObjRes.getDeletedObjects().size()); } else { String k = key.toString(); if (k.charAt(0) == '/') k = k.substring(1); s3Client.deleteObject(new DeleteObjectRequest(bucket, k)); return new cfNumberData(1); } } catch (Exception e) { throwException(_session, "AmazonS3: " + e.getMessage()); return new cfNumberData(0); } }
From source file:org.finra.herd.dao.impl.S3OperationsImpl.java
License:Apache License
@Override public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest, AmazonS3 s3Client) { return s3Client.deleteObjects(deleteObjectsRequest); }