Search in sources :

Example 1 with DeletedObject

use of com.amazonaws.services.s3.model.DeleteObjectsResult.DeletedObject in project herd by FINRAOS.

the class MockS3OperationsImpl method deleteObjects.

@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);
}
Also used : KeyVersion(com.amazonaws.services.s3.model.DeleteObjectsRequest.KeyVersion) ArrayList(java.util.ArrayList) DeleteObjectsResult(com.amazonaws.services.s3.model.DeleteObjectsResult) DeletedObject(com.amazonaws.services.s3.model.DeleteObjectsResult.DeletedObject)

Example 2 with DeletedObject

use of com.amazonaws.services.s3.model.DeleteObjectsResult.DeletedObject in project aws-doc-sdk-examples by awsdocs.

the class DeleteMultipleObjectsVersionEnabledBucket method multiObjectVersionedDeleteRemoveDeleteMarkers.

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");
}
Also used : KeyVersion(com.amazonaws.services.s3.model.DeleteObjectsRequest.KeyVersion) ArrayList(java.util.ArrayList) DeleteObjectsResult(com.amazonaws.services.s3.model.DeleteObjectsResult) DeletedObject(com.amazonaws.services.s3.model.DeleteObjectsResult.DeletedObject) DeleteObjectsRequest(com.amazonaws.services.s3.model.DeleteObjectsRequest)

Example 3 with DeletedObject

use of com.amazonaws.services.s3.model.DeleteObjectsResult.DeletedObject in project alluxio by Alluxio.

the class S3AUnderFileSystem method deleteObjects.

@Override
protected List<String> deleteObjects(List<String> keys) throws IOException {
    if (!mUfsConf.getBoolean(PropertyKey.UNDERFS_S3_BULK_DELETE_ENABLED)) {
        return super.deleteObjects(keys);
    }
    Preconditions.checkArgument(keys != null && keys.size() <= getListingChunkLengthMax());
    try {
        List<DeleteObjectsRequest.KeyVersion> keysToDelete = new ArrayList<>();
        for (String key : keys) {
            keysToDelete.add(new DeleteObjectsRequest.KeyVersion(key));
        }
        DeleteObjectsResult deletedObjectsResult = mClient.deleteObjects(new DeleteObjectsRequest(mBucketName).withKeys(keysToDelete));
        List<String> deletedObjects = new ArrayList<>();
        for (DeleteObjectsResult.DeletedObject deletedObject : deletedObjectsResult.getDeletedObjects()) {
            deletedObjects.add(deletedObject.getKey());
        }
        return deletedObjects;
    } catch (AmazonClientException e) {
        throw new IOException(e);
    }
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) DeleteObjectsResult(com.amazonaws.services.s3.model.DeleteObjectsResult) IOException(java.io.IOException) DeleteObjectsRequest(com.amazonaws.services.s3.model.DeleteObjectsRequest)

Example 4 with DeletedObject

use of com.amazonaws.services.s3.model.DeleteObjectsResult.DeletedObject in project herd by FINRAOS.

the class S3DaoTest method testDeleteDirectoryAssertMultiObjectDeleteExceptionLogged.

/**
 * Asserts that when delete directory is called but S3 throws MultiObjectDeleteException, the exception is logged properly.
 */
@Test
// TODO: Log4J2 - This test works within an IDE, but not from Maven. We need to figure out why.
@Ignore
public void testDeleteDirectoryAssertMultiObjectDeleteExceptionLogged() throws Exception {
    // Inject mock
    S3Operations mockS3Operations = mock(S3Operations.class);
    S3Operations originalS3Operations = (S3Operations) ReflectionTestUtils.getField(s3Dao, "s3Operations");
    ReflectionTestUtils.setField(s3Dao, "s3Operations", mockS3Operations);
    // Override logger with my own appender to inspect the output
    String loggerName = S3DaoImpl.class.getName();
    LogLevel originalLoggerLevel = getLogLevel(loggerName);
    setLogLevel(loggerName, LogLevel.ERROR);
    String appenderName = "TestWriterAppender";
    StringWriter stringWriter = addLoggingWriterAppender(appenderName);
    try {
        // Set up mocked behavior
        // Return a list of mock version listing
        VersionListing versionListing = new VersionListing();
        S3VersionSummary s3VersionSummary = new S3VersionSummary();
        s3VersionSummary.setKey("s3VersionSummaryKey");
        s3VersionSummary.setVersionId("s3VersionSummaryVersionId");
        versionListing.setVersionSummaries(Arrays.asList(s3VersionSummary));
        when(mockS3Operations.listVersions(any(), any())).thenReturn(versionListing);
        // Have mock implementation throw exception
        List<DeleteError> errors = new ArrayList<>();
        {
            DeleteError deleteError = new DeleteError();
            deleteError.setCode("deleteError1Code");
            deleteError.setKey("deleteError1Key");
            deleteError.setMessage("deleteError1Message");
            deleteError.setVersionId("deleteError1VersionId");
            errors.add(deleteError);
        }
        {
            DeleteError deleteError = new DeleteError();
            deleteError.setCode("deleteError2Code");
            deleteError.setKey("deleteError2Key");
            deleteError.setMessage("deleteError2Message");
            deleteError.setVersionId("deleteError2VersionId");
            errors.add(deleteError);
        }
        List<DeletedObject> deletedObjects = new ArrayList<>();
        MultiObjectDeleteException multiObjectDeleteException = new MultiObjectDeleteException(errors, deletedObjects);
        when(mockS3Operations.deleteObjects(any(), any())).thenThrow(multiObjectDeleteException);
        // try the operation and catch exception
        try {
            S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto();
            s3FileTransferRequestParamsDto.setS3KeyPrefix("/test/prefix");
            s3Dao.deleteDirectory(s3FileTransferRequestParamsDto);
            fail();
        } catch (Exception e) {
            // Inspect and assert exception
            assertEquals(IllegalStateException.class, e.getClass());
            assertEquals(multiObjectDeleteException, e.getCause());
        }
        assertEquals(String.format("Error deleting multiple objects. Below are the list of objects which failed to delete.%n" + "s3Key=\"deleteError1Key\" s3VersionId=\"deleteError1VersionId\" " + "s3DeleteErrorCode=\"deleteError1Code\" s3DeleteErrorMessage=\"deleteError1Message\"%n" + "s3Key=\"deleteError2Key\" s3VersionId=\"deleteError2VersionId\" " + "s3DeleteErrorCode=\"deleteError2Code\" s3DeleteErrorMessage=\"deleteError2Message\"%n%n"), stringWriter.toString());
    } finally {
        // Restore original resources
        ReflectionTestUtils.setField(s3Dao, "s3Operations", originalS3Operations);
        setLogLevel(loggerName, originalLoggerLevel);
        removeLoggingAppender(appenderName);
    }
}
Also used : DeleteError(com.amazonaws.services.s3.model.MultiObjectDeleteException.DeleteError) S3FileTransferRequestParamsDto(org.finra.herd.model.dto.S3FileTransferRequestParamsDto) VersionListing(com.amazonaws.services.s3.model.VersionListing) ArrayList(java.util.ArrayList) LogLevel(org.finra.herd.core.helper.LogLevel) MultiObjectDeleteException(com.amazonaws.services.s3.model.MultiObjectDeleteException) ObjectNotFoundException(org.finra.herd.model.ObjectNotFoundException) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonClientException(com.amazonaws.AmazonClientException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) IOException(java.io.IOException) StringWriter(java.io.StringWriter) S3VersionSummary(com.amazonaws.services.s3.model.S3VersionSummary) MultiObjectDeleteException(com.amazonaws.services.s3.model.MultiObjectDeleteException) DeletedObject(com.amazonaws.services.s3.model.DeleteObjectsResult.DeletedObject) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)4 DeleteObjectsResult (com.amazonaws.services.s3.model.DeleteObjectsResult)3 DeletedObject (com.amazonaws.services.s3.model.DeleteObjectsResult.DeletedObject)3 AmazonClientException (com.amazonaws.AmazonClientException)2 DeleteObjectsRequest (com.amazonaws.services.s3.model.DeleteObjectsRequest)2 KeyVersion (com.amazonaws.services.s3.model.DeleteObjectsRequest.KeyVersion)2 IOException (java.io.IOException)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)1 MultiObjectDeleteException (com.amazonaws.services.s3.model.MultiObjectDeleteException)1 DeleteError (com.amazonaws.services.s3.model.MultiObjectDeleteException.DeleteError)1 S3VersionSummary (com.amazonaws.services.s3.model.S3VersionSummary)1 VersionListing (com.amazonaws.services.s3.model.VersionListing)1 StringWriter (java.io.StringWriter)1 LogLevel (org.finra.herd.core.helper.LogLevel)1 ObjectNotFoundException (org.finra.herd.model.ObjectNotFoundException)1 S3FileTransferRequestParamsDto (org.finra.herd.model.dto.S3FileTransferRequestParamsDto)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1