/** * Converts the specified {@link DeleteObjectsRequest} object to an XML fragment that * can be sent to Amazon S3. * * @param rq * The {@link DeleteObjectsRequest} */ public byte[] convertToXmlByteArray(DeleteObjectsRequest rq) throws SdkClientException { XmlWriter xml = new XmlWriter(); xml.start("Delete"); if ( rq.getQuiet() ) { xml.start("Quiet").value("true").end(); } for (KeyVersion keyVersion : rq.getKeys()) { writeKeyVersion(xml, keyVersion); } xml.end(); return xml.getBytes(); }
@Override public void remove(String accessKey) throws Exception { LogUtils.debug(LOG_TAG, "Deleting file with access key: " + accessKey); AmazonS3 s3Client = new AmazonS3Client(new DefaultAWSCredentialsProviderChain()); DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(bucketName); List<KeyVersion> keys = new ArrayList<KeyVersion>(); keys.add(new KeyVersion(accessKey)); keys.add(new KeyVersion(accessKey + "_key")); multiObjectDeleteRequest.setKeys(keys); s3Client.deleteObjects(multiObjectDeleteRequest); LogUtils.debug(LOG_TAG, "Deleted file with access key: " + accessKey); }
public static void main(String[] args) throws IOException, InterruptedException { System.out.println("WARNING: This will indiscriminately delete every object in a given bucket. This operation cannot be undone."); final String bucketName = prompt("Bucket"); if (!"yes".equals(prompt("Are you sure you want to delete all objects in bucket " + bucketName + "? (type 'yes' to continue)"))) { System.out.println("Aborting..."); return; } final TruncateBucket truncater = new TruncateBucket(bucketName); truncater.truncate(); ObjectListing results; do { results = s3.listObjects(bucketName); final List<KeyVersion> keys = results.getObjectSummaries().stream() .map((k) -> new KeyVersion(k.getKey())) .collect(toList()); final DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest(bucketName).withKeys(keys); s3.deleteObjects(deleteRequest); } while (results.isTruncated()); }
private void writeKeyVersion(XmlWriter xml, KeyVersion keyVersion) { xml.start("Object"); xml.start("Key").value(keyVersion.getKey()).end(); if (keyVersion.getVersion() != null) { xml.start("VersionId").value(keyVersion.getVersion()).end(); } xml.end(); }
@Override public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest, AmazonS3 s3Client) { LOGGER.debug("deleteObjects(): deleteObjectRequest.getBucketName() = " + deleteObjectsRequest.getBucketName() + ", deleteObjectRequest.getKeys() = " + deleteObjectsRequest.getKeys()); List<DeletedObject> deletedObjects = new ArrayList<>(); MockS3Bucket mockS3Bucket = mockS3Buckets.get(deleteObjectsRequest.getBucketName()); for (KeyVersion keyVersion : deleteObjectsRequest.getKeys()) { String s3ObjectKey = keyVersion.getKey(); String s3ObjectVersion = keyVersion.getVersion(); String s3ObjectKeyVersion = s3ObjectKey + (s3ObjectVersion != null ? s3ObjectVersion : ""); mockS3Bucket.getObjects().remove(s3ObjectKey); if (mockS3Bucket.getVersions().remove(s3ObjectKeyVersion) != null) { DeletedObject deletedObject = new DeletedObject(); deletedObject.setKey(s3ObjectKey); deletedObject.setVersionId(s3ObjectVersion); deletedObjects.add(deletedObject); } } return new DeleteObjectsResult(deletedObjects); }
/** * Method delete all <i>objects</i> from <i>bucketName</i> in groups by 1000 * elements * * @param bucketName * @param objects */ public void delete(String bucketName, List<KeyVersion> objects) { ReactiveSeq.fromList(objects) .grouped(1000) .forEach(l -> { DeleteObjectsRequest req = new DeleteObjectsRequest( bucketName); req.setKeys(l.toList()); client.deleteObjects(req); }); }
@Test public void deleteObjects() { AmazonS3Client client = mock(AmazonS3Client.class); S3Utils utils = new S3Utils( client, null, null, false, null); List<KeyVersion> keys = new ArrayList<>(); for (int i = 0; i < 2000; i++) { keys.add(new KeyVersion( "")); } utils.delete("", keys); verify(client, times(2)).deleteObjects(any()); }
@Override public DeleteObjectsResult deleteObjects(final String bucketName, final List<KeyVersion> keys) throws AmazonClientException, AmazonServiceException { LOGGER.info("deleteObjects invoked, bucketName: {}, keys: {}", bucketName, keys); final DeleteObjectsRequest deleteObjReq = new DeleteObjectsRequest(bucketName); deleteObjReq.setKeys(keys); return s3client.deleteObjects(deleteObjReq); }
/** * Test method for {@link com.github.abhinavmishra14.aws.s3.service.AwsS3IamService#deleteObjects(java.lang.String,java.util.List)}. * * @throws Exception the exception */ @Test public void testDeleteObjects() throws Exception { List<KeyVersion> keys = new ArrayList<KeyVersion>(); KeyVersion key1= new KeyVersion(AWSUtilConstants.SAMPLE_FILE_NAME); KeyVersion key2= new KeyVersion("TestPutObject2.txt"); keys.add(key1); keys.add(key2); awsS3IamService.createBucket(AWS_S3_BUCKET);//create bucket for test DeleteObjectsResult delResp = awsS3IamService.deleteObjects(AWS_S3_BUCKET,keys); assertNotNull(delResp); }
/** * {@inheritDoc} * * The stub does not currently support versions so this stub only supports full deletes of the objects. Passing in a * list of KeyVersion specs to be deleted will completely delete those objects from the stubs. * * @see com.amazonaws.services.s3.AmazonS3#deleteObjects(com.amazonaws.services.s3.model.DeleteObjectsRequest) */ @Override public DeleteObjectsResult deleteObjects(final DeleteObjectsRequest deleteObjectsRequest) { final List<DeleteObjectsResult.DeletedObject> deletedObjects = new ArrayList<DeletedObject>(); final BucketInfo bucket = getBucketInfo(deleteObjectsRequest.getBucketName()); for (final KeyVersion key : deleteObjectsRequest.getKeys()) { final boolean found = bucket.deleteObject(key.getKey()); if (!deleteObjectsRequest.getQuiet() && found) { final DeleteObjectsResult.DeletedObject result = new DeleteObjectsResult.DeletedObject(); result.setKey(key.getKey()); deletedObjects.add(result); } } return new DeleteObjectsResult(deletedObjects); }
@Override public boolean deleteEntities(String bucketName) { LOG.info("Deletes multiple objects in a bucket " + bucketName + " from Amazon S3"); List<S3ObjectSummary> s3ObjectSummaries = findEntityByBucket(bucketName); if (s3ObjectSummaries == null || s3ObjectSummaries.isEmpty()) { return false; } // Provide a list of object keys and versions. List<KeyVersion> keyVersions = new ArrayList<KeyVersion>(); for (S3ObjectSummary s3ObjectSummary : s3ObjectSummaries) { keyVersions.add( new KeyVersion(s3ObjectSummary.getKey())); } try { DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName) .withKeys(keyVersions); DeleteObjectsResult deleteObjectsResult = amazonS3Client.deleteObjects(deleteObjectsRequest); if (deleteObjectsResult != null) { LOG.info("Successfully deleted all the " + deleteObjectsResult.getDeletedObjects().size() + " items.\n"); return true; } } catch (AmazonServiceException ase) { LOG.warn(ase.getMessage(), ase); } catch (AmazonClientException ace) { LOG.warn(ace.getMessage(), ace); } return false; }
/** * Delete objects. * * @param bucketName the bucket name * @param keys the keys * @return the delete objects result * @throws AmazonClientException the amazon client exception * @throws AmazonServiceException the amazon service exception */ DeleteObjectsResult deleteObjects(final String bucketName, final List<KeyVersion> keys) throws AmazonClientException, AmazonServiceException;