com.amazonaws.services.s3.model.DeleteObjectsResult Java Examples

The following examples show how to use com.amazonaws.services.s3.model.DeleteObjectsResult. 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: AwsSdkTest.java    From s3proxy with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteMultipleObjects() throws Exception {
    String blobName = "foo";
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(BYTE_SOURCE.size());

    DeleteObjectsRequest request = new DeleteObjectsRequest(containerName)
            .withKeys(blobName);

    // without quiet
    client.putObject(containerName, blobName, BYTE_SOURCE.openStream(),
            metadata);

    DeleteObjectsResult result = client.deleteObjects(request);
    assertThat(result.getDeletedObjects()).hasSize(1);
    assertThat(result.getDeletedObjects().iterator().next().getKey())
            .isEqualTo(blobName);

    // with quiet
    client.putObject(containerName, blobName, BYTE_SOURCE.openStream(),
            metadata);

    result = client.deleteObjects(request.withQuiet(true));
    assertThat(result.getDeletedObjects()).isEmpty();
}
 
Example #2
Source File: DeleteMultipleObjectsVersionEnabledBucket.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
private static void uploadAndDeleteObjectsWithVersions() {
    System.out.println("Uploading and deleting objects with versions specified.");

    // Upload three sample objects.
    ArrayList<KeyVersion> keys = new ArrayList<KeyVersion>();
    for (int i = 0; i < 3; i++) {
        String keyName = "delete object without version ID example " + i;
        PutObjectResult putResult = S3_CLIENT.putObject(VERSIONED_BUCKET_NAME, keyName,
                "Object number " + i + " to be deleted.");
        // Gather the new object keys with version IDs.
        keys.add(new KeyVersion(keyName, putResult.getVersionId()));
    }

    // Delete the specified versions of the sample objects.
    DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(VERSIONED_BUCKET_NAME)
            .withKeys(keys)
            .withQuiet(false);

    // Verify that the object versions were successfully deleted.
    DeleteObjectsResult delObjRes = S3_CLIENT.deleteObjects(multiObjectDeleteRequest);
    int successfulDeletes = delObjRes.getDeletedObjects().size();
    System.out.println(successfulDeletes + " objects successfully deleted");
}
 
Example #3
Source File: DeleteMultipleObjectsVersionEnabledBucket.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
private static DeleteObjectsResult uploadAndDeleteObjectsWithoutVersions() {
    System.out.println("Uploading and deleting objects with no versions specified.");

    // Upload three sample objects.
    ArrayList<KeyVersion> keys = new ArrayList<KeyVersion>();
    for (int i = 0; i < 3; i++) {
        String keyName = "delete object with version ID example " + i;
        S3_CLIENT.putObject(VERSIONED_BUCKET_NAME, keyName, "Object number " + i + " to be deleted.");
        // Gather the new object keys without version IDs.
        keys.add(new KeyVersion(keyName));
    }

    // Delete the sample objects without specifying versions.
    DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(VERSIONED_BUCKET_NAME).withKeys(keys)
            .withQuiet(false);

    // Verify that delete markers were successfully added to the objects.
    DeleteObjectsResult delObjRes = S3_CLIENT.deleteObjects(multiObjectDeleteRequest);
    int successfulDeletes = delObjRes.getDeletedObjects().size();
    System.out.println(successfulDeletes + " objects successfully marked for deletion without versions.");
    return delObjRes;
}
 
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: MockAmazonS3.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public DeleteObjectsResult deleteObjects(DeleteObjectsRequest request) throws SdkClientException {
    assertThat(request.getBucketName(), equalTo(bucket));

    final List<DeleteObjectsResult.DeletedObject> deletions = new ArrayList<>();
    for (DeleteObjectsRequest.KeyVersion key : request.getKeys()) {
        if (blobs.remove(key.getKey()) != null) {
            DeleteObjectsResult.DeletedObject deletion = new DeleteObjectsResult.DeletedObject();
            deletion.setKey(key.getKey());
            deletions.add(deletion);
        }
    }
    return new DeleteObjectsResult(deletions);
}
 
Example #6
Source File: AWSS3ServiceIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test 
public void whenVerifyingDeleteObjects_thenCorrect() { 
    DeleteObjectsRequest request = mock(DeleteObjectsRequest.class);
    DeleteObjectsResult result = mock(DeleteObjectsResult.class);
    when(s3.deleteObjects((DeleteObjectsRequest)any())).thenReturn(result); 
    
    assertThat(service.deleteObjects(request)).isEqualTo(result);
    verify(s3).deleteObjects(request); 
}
 
Example #7
Source File: MockS3OperationsImpl.java    From herd with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #8
Source File: DeleteMultipleObjectsVersionEnabledBucket.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
private static void multiObjectVersionedDeleteRemoveDeleteMarkers(DeleteObjectsResult response) {
    List<KeyVersion> keyList = new ArrayList<KeyVersion>();
    for (DeletedObject deletedObject : response.getDeletedObjects()) {
        // Note that the specified version ID is the version ID for the delete marker.
        keyList.add(new KeyVersion(deletedObject.getKey(), deletedObject.getDeleteMarkerVersionId()));
    }
    // Create a request to delete the delete markers.
    DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest(VERSIONED_BUCKET_NAME).withKeys(keyList);

    // Delete the delete markers, leaving the objects intact in the bucket.
    DeleteObjectsResult delObjRes = S3_CLIENT.deleteObjects(deleteRequest);
    int successfulDeletes = delObjRes.getDeletedObjects().size();
    System.out.println(successfulDeletes + " delete markers successfully deleted");
}
 
Example #9
Source File: S3DataManipulator.java    From circus-train with Apache License 2.0 5 votes vote down vote up
@Override
public boolean delete(String path) {
  log.info("Deleting all data at location: {}", path);
  AmazonS3URI uri = toAmazonS3URI(URI.create(path));
  String bucket = uri.getBucket();

  List<KeyVersion> keysToDelete = getKeysToDelete(bucket, uri.getKey());
  log.debug("Deleting keys: {}", keysToDelete.stream().map(k -> k.getKey()).collect(Collectors.toList()));

  DeleteObjectsResult result = s3Client.deleteObjects(new DeleteObjectsRequest(bucket).withKeys(keysToDelete));
  return successfulDeletion(result, keysToDelete.size());
}
 
Example #10
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 #11
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);
}
 
Example #12
Source File: S3DataManipulator.java    From circus-train with Apache License 2.0 4 votes vote down vote up
private boolean successfulDeletion(DeleteObjectsResult result, int countToDelete) {
  int amountDeleted = result.getDeletedObjects().size();
  return amountDeleted == countToDelete;
}
 
Example #13
Source File: DummyS3Client.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** Unsupported Operation. */
@Override public DeleteObjectsResult deleteObjects(DeleteObjectsRequest delObjectsReq)
    throws SdkClientException {
    throw new UnsupportedOperationException("Operation not supported");
}
 
Example #14
Source File: AWSS3Service.java    From tutorials with MIT License 4 votes vote down vote up
public DeleteObjectsResult deleteObjects(DeleteObjectsRequest delObjReq) {
    return s3client.deleteObjects(delObjReq);
}
 
Example #15
Source File: S3Operations.java    From ats-framework with Apache License 2.0 4 votes vote down vote up
/**
 * Delete all objects matching given prefix. This method is preferred for efficient deletion of many files
 * 
 * @param folderPrefix empty path is expected for objects in the "root" of the bucket 
 * @param searchString what pattern to be matched. This pattern will be matched against "short file name", i.e. 
 *                     the object's ID after last path separator (&quot;/&quot;).<br />
 *                     If null it means all ( string &quot;.*&quot;). 
 * @param recursive if true searches recursively for matching in nested path levels (&quot;/&quot;)
 * 
 * @return list of deleted objects
 * @throws S3OperationException in case of an error from server
 */
@PublicAtsApi
public void deleteObjects( String folderPrefix, String searchString, boolean recursive ) {

    //Alternative but not documented in S3 API: getClient().listObjectsV2(bucket, "prefix")
    ListObjectsRequest request = new ListObjectsRequest(bucketName, folderPrefix, null, recursive
                                                                                                  ? null
                                                                                                  : "/",
                                                        null);
    int totallyDeleted = 0;
    try {
        ObjectListing objectListing = s3Client.listObjects(request);
        int i = 0;
        if (searchString == null) {
            searchString = ".*"; // any string
        }
        List<KeyVersion> keysForDelete = new ArrayList<KeyVersion>(100);
        Pattern searchStringPattern = Pattern.compile(searchString);
        while (true) {
            keysForDelete.clear();
            for (Iterator<?> iterator = objectListing.getObjectSummaries().iterator(); iterator.hasNext();) {
                S3ObjectSummary objectSummary = (S3ObjectSummary) iterator.next();
                if (LOG.isTraceEnabled()) {
                    LOG.trace("listObject[" + (++i) + "]: " + objectSummary.toString());
                }

                String[] fileTokens = objectSummary.getKey().split("/");
                String s3Object = fileTokens[fileTokens.length - 1];

                Matcher matcher = searchStringPattern.matcher(s3Object);
                if (matcher.find()) {
                    keysForDelete.add(new KeyVersion(objectSummary.getKey()));
                    //allListElements.add(new S3ObjectInfo(objectSummary));
                }
            }
            if (keysForDelete.size() > 0) {
                // delete current set / batch size
                DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(bucketName).withKeys(keysForDelete)
                                                                                                    .withQuiet(false);
                DeleteObjectsResult delObjRes = s3Client.deleteObjects(multiObjectDeleteRequest);
                int currentlyDeletedCount = delObjRes.getDeletedObjects().size();
                totallyDeleted = totallyDeleted + currentlyDeletedCount;

                // verify size of deleted objects
                if (keysForDelete.size() != currentlyDeletedCount) {
                    LOG.warn("The number of actually deleted objects " + currentlyDeletedCount +
                             " does not match the expected size of " + keysForDelete.size());
                } else {
                    LOG.debug("Number of deleted S3 objects in current batch is " + currentlyDeletedCount);
                }
            }

            // more objects to retrieve (1K batch size of objects)
            if (objectListing.isTruncated()) {
                objectListing = s3Client.listNextBatchOfObjects(objectListing);
            } else {
                break;
            }
        }
        LOG.info("Successfully deleted " + totallyDeleted + " objects");
    } catch (AmazonClientException e) {
        throw new S3OperationException("Error deleting multiple objects matching pattern " + searchString 
                                       + ". Number of deleted objects is " + totallyDeleted, e);
    } 
}
 
Example #16
Source File: AmazonS3Mock.java    From Scribengin with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest) throws AmazonClientException,
    AmazonServiceException {
  // TODO Auto-generated method stub
  return null;
}
 
Example #17
Source File: S3Operations.java    From herd with Apache License 2.0 2 votes vote down vote up
/**
 * Deletes the specified S3 objects in the specified S3 bucket.
 *
 * @param deleteObjectsRequest the request object containing all the options for deleting multiple objects in a specified bucket
 * @param s3Client the {@link AmazonS3} implementation to use
 *
 * @return the successful response to the delete object request
 */
public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest, AmazonS3 s3Client);