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

The following examples show how to use com.amazonaws.services.s3.model.MultiObjectDeleteException. 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: S3DaoImpl.java    From herd with Apache License 2.0 6 votes vote down vote up
/**
 * Logs the given MultiObjectDeleteException.
 *
 * @param multiObjectDeleteException The exception to log
 */
private void logMultiObjectDeleteException(MultiObjectDeleteException multiObjectDeleteException)
{
    // Create and initialize a string buffer. The initialization is required here in order to avoid an InsufficientStringBufferDeclaration PMD violation.
    StringBuilder builder = new StringBuilder(128);
    builder.append(String.format("Error deleting multiple objects. Below are the list of objects which failed to delete.%n"));
    List<DeleteError> deleteErrors = multiObjectDeleteException.getErrors();
    for (DeleteError deleteError : deleteErrors)
    {
        String key = deleteError.getKey();
        String versionId = deleteError.getVersionId();
        String code = deleteError.getCode();
        String message = deleteError.getMessage();
        builder
            .append(String.format("s3Key=\"%s\" s3VersionId=\"%s\" s3DeleteErrorCode=\"%s\" s3DeleteErrorMessage=\"%s\"%n", key, versionId, code, message));
    }
    LOGGER.error(builder.toString());
}
 
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: S3Operations.java    From ats-framework with Apache License 2.0 5 votes vote down vote up
private String handleMultiDeleteExceptionDetails( MultiObjectDeleteException de ) {

        StringBuilder sb = new StringBuilder();
        sb.append("Error deleting multiple objects. Details:\n");
        sb.append("Details: " + de.getErrorMessage() + ")\n");
        sb.append("Successfully deleted objects: " + Arrays.toString(de.getDeletedObjects().toArray()) + "\n");
        // TODO: DeleteError does not overwrite toString(). Optionally get messages only 
        sb.append("Deletion errors: " + de.getErrors().size());
        throw new S3OperationException(sb.toString(), de);
    }
 
Example #4
Source File: S3DaoImpl.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes a list of keys/key versions from the specified S3 bucket.
 *
 * @param s3Client the S3 client
 * @param s3BucketName the S3 bucket name
 * @param keyVersions the list of S3 keys/key versions
 */
private void deleteKeyVersions(AmazonS3Client s3Client, String s3BucketName, List<DeleteObjectsRequest.KeyVersion> keyVersions)
{
    // Create a request to delete multiple objects in the specified bucket.
    DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(s3BucketName);

    // The Multi-Object Delete request can contain a list of up to 1000 keys.
    for (int i = 0; i < keyVersions.size() / MAX_KEYS_PER_DELETE_REQUEST + 1; i++)
    {
        List<DeleteObjectsRequest.KeyVersion> keysSubList =
            keyVersions.subList(i * MAX_KEYS_PER_DELETE_REQUEST, Math.min(keyVersions.size(), (i + 1) * MAX_KEYS_PER_DELETE_REQUEST));
        multiObjectDeleteRequest.setKeys(keysSubList);
        try
        {
            s3Operations.deleteObjects(multiObjectDeleteRequest, s3Client);
        }
        catch (MultiObjectDeleteException multiObjectDeleteException)
        {
            logMultiObjectDeleteException(multiObjectDeleteException);
            throw multiObjectDeleteException;
        }

        LOGGER.info("Successfully requested the deletion of the listed below keys/key versions from the S3 bucket. s3KeyCount={} s3BucketName=\"{}\"",
            keysSubList.size(), s3BucketName);

        for (DeleteObjectsRequest.KeyVersion keyVersion : keysSubList)
        {
            LOGGER.info("s3Key=\"{}\" s3VersionId=\"{}\"", keyVersion.getKey(), keyVersion.getVersion());
        }
    }
}
 
Example #5
Source File: S3DaoImplTest.java    From herd with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeleteDirectoryMultiObjectDeleteException()
{
    // Create an S3 file transfer request parameters DTO to access S3 objects.
    S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto();
    s3FileTransferRequestParamsDto.setS3BucketName(S3_BUCKET_NAME);
    s3FileTransferRequestParamsDto.setS3KeyPrefix(S3_KEY_PREFIX);

    // Create a retry policy.
    RetryPolicy retryPolicy =
        new RetryPolicy(PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION, PredefinedRetryPolicies.DEFAULT_BACKOFF_STRATEGY, INTEGER_VALUE, true);

    // Create an S3 version summary.
    S3VersionSummary s3VersionSummary = new S3VersionSummary();
    s3VersionSummary.setKey(S3_KEY);
    s3VersionSummary.setVersionId(S3_VERSION_ID);

    // Create a version listing.
    VersionListing versionListing = new VersionListing();
    versionListing.setVersionSummaries(Collections.singletonList(s3VersionSummary));

    // Create a delete error.
    MultiObjectDeleteException.DeleteError deleteError = new MultiObjectDeleteException.DeleteError();
    deleteError.setKey(S3_KEY);
    deleteError.setVersionId(S3_VERSION_ID);
    deleteError.setCode(ERROR_CODE);
    deleteError.setMessage(ERROR_MESSAGE);

    // Create a multi object delete exception.
    MultiObjectDeleteException multiObjectDeleteException = new MultiObjectDeleteException(Collections.singletonList(deleteError), new ArrayList<>());

    // Mock the external calls.
    when(retryPolicyFactory.getRetryPolicy()).thenReturn(retryPolicy);
    when(s3Operations.listVersions(any(ListVersionsRequest.class), any(AmazonS3Client.class))).thenReturn(versionListing);
    when(s3Operations.deleteObjects(any(DeleteObjectsRequest.class), any(AmazonS3Client.class))).thenThrow(multiObjectDeleteException);

    // Try to call the method under test.
    try
    {
        s3DaoImpl.deleteDirectory(s3FileTransferRequestParamsDto);
    }
    catch (IllegalStateException e)
    {
        assertEquals(String.format(
            "Failed to delete keys/key versions with prefix \"%s\" from bucket \"%s\". Reason: One or more objects could not be deleted " +
                "(Service: null; Status Code: 0; Error Code: null; Request ID: null; S3 Extended Request ID: null)", S3_KEY_PREFIX, S3_BUCKET_NAME),
            e.getMessage());
    }

    // Verify the external calls.
    verify(retryPolicyFactory, times(2)).getRetryPolicy();
    verify(s3Operations).listVersions(any(ListVersionsRequest.class), any(AmazonS3Client.class));
    verify(s3Operations).deleteObjects(any(DeleteObjectsRequest.class), any(AmazonS3Client.class));
    verifyNoMoreInteractionsHelper();
}