Search in sources :

Example 1 with DeleteObjectsResponse

use of com.amazonaws.services.s3.internal.DeleteObjectsResponse in project aws-sdk-android by aws-amplify.

the class AmazonS3Client method deleteObjects.

/*
     * (non-Javadoc)
     * @see
     * com.amazonaws.services.s3.AmazonS3#deleteObjects(com.amazonaws.services
     * .s3.model.DeleteObjectsRequest)
     */
@Override
public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest) {
    final Request<DeleteObjectsRequest> request = createRequest(deleteObjectsRequest.getBucketName(), null, deleteObjectsRequest, HttpMethodName.POST);
    request.addParameter("delete", null);
    if (deleteObjectsRequest.getMfa() != null) {
        populateRequestWithMfaDetails(request, deleteObjectsRequest.getMfa());
    }
    populateRequesterPaysHeader(request, deleteObjectsRequest.isRequesterPays());
    final byte[] content = new MultiObjectDeleteXmlFactory().convertToXmlByteArray(deleteObjectsRequest);
    request.addHeader("Content-Length", String.valueOf(content.length));
    request.addHeader("Content-Type", "application/xml");
    request.setContent(new ByteArrayInputStream(content));
    try {
        final byte[] md5 = Md5Utils.computeMD5Hash(content);
        final String md5Base64 = BinaryUtils.toBase64(md5);
        request.addHeader("Content-MD5", md5Base64);
    } catch (final Exception e) {
        throw new AmazonClientException("Couldn't compute md5 sum", e);
    }
    @SuppressWarnings("unchecked") final ResponseHeaderHandlerChain<DeleteObjectsResponse> responseHandler = new ResponseHeaderHandlerChain<DeleteObjectsResponse>(new Unmarshallers.DeleteObjectsResultUnmarshaller(), new S3RequesterChargedHeaderHandler<DeleteObjectsResponse>());
    final DeleteObjectsResponse response = invoke(request, responseHandler, deleteObjectsRequest.getBucketName(), null);
    /*
         * If the result was only partially successful, throw an exception
         */
    if (!response.getErrors().isEmpty()) {
        final Map<String, String> headers = responseHandler.getResponseHeaders();
        final MultiObjectDeleteException ex = new MultiObjectDeleteException(response.getErrors(), response.getDeletedObjects());
        ex.setStatusCode(200);
        ex.setRequestId(headers.get(Headers.REQUEST_ID));
        ex.setExtendedRequestId(headers.get(Headers.EXTENDED_REQUEST_ID));
        ex.setCloudFrontId(headers.get(Headers.CLOUD_FRONT_ID));
        throw ex;
    }
    final DeleteObjectsResult result = new DeleteObjectsResult(response.getDeletedObjects(), response.isRequesterCharged());
    return result;
}
Also used : DeleteObjectsResponse(com.amazonaws.services.s3.internal.DeleteObjectsResponse) Unmarshallers(com.amazonaws.services.s3.model.transform.Unmarshallers) AmazonClientException(com.amazonaws.AmazonClientException) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonClientException(com.amazonaws.AmazonClientException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) FileNotFoundException(java.io.FileNotFoundException) AbortedException(com.amazonaws.AbortedException) MultiObjectDeleteXmlFactory(com.amazonaws.services.s3.model.transform.MultiObjectDeleteXmlFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) ResponseHeaderHandlerChain(com.amazonaws.services.s3.internal.ResponseHeaderHandlerChain)

Example 2 with DeleteObjectsResponse

use of com.amazonaws.services.s3.internal.DeleteObjectsResponse in project aws-sdk-android by aws-amplify.

the class MultiObjectDeleteIntegrationTest method testErrorHandling.

/**
 * It's difficult to trigger an actual error, so we just test the error
 * parsing.
 */
@Test
public void testErrorHandling() throws Exception {
    DeleteObjectsResultUnmarshaller unmarshaller = new Unmarshallers.DeleteObjectsResultUnmarshaller();
    DeleteObjectsResponse response = unmarshaller.unmarshall(ApplicationProvider.getApplicationContext().getResources().getAssets().open("errorResponse.xml"));
    assertEquals(1, response.getDeletedObjects().size());
    assertEquals(1, response.getErrors().size());
    assertEquals("object1", response.getDeletedObjects().get(0).getKey());
    assertEquals("123", response.getDeletedObjects().get(0).getVersionId());
    assertEquals("object2", response.getErrors().get(0).getKey());
    assertEquals("AccessDenied", response.getErrors().get(0).getCode());
    assertEquals("Access Denied", response.getErrors().get(0).getMessage());
}
Also used : DeleteObjectsResponse(com.amazonaws.services.s3.internal.DeleteObjectsResponse) DeleteObjectsResultUnmarshaller(com.amazonaws.services.s3.model.transform.Unmarshallers.DeleteObjectsResultUnmarshaller) Test(org.junit.Test)

Example 3 with DeleteObjectsResponse

use of com.amazonaws.services.s3.internal.DeleteObjectsResponse in project aws-sdk-android by aws-amplify.

the class MultiObjectDeleteIntegrationTest method testFullServiceResponse.

/**
 * Rather than recreating the conditions where a fully fleshed out response
 * comes back from the service, just make sure we can parse such a response.
 */
@Test
public void testFullServiceResponse() throws Exception {
    DeleteObjectsResultUnmarshaller unmarshaller = new Unmarshallers.DeleteObjectsResultUnmarshaller();
    DeleteObjectsResponse response = unmarshaller.unmarshall(ApplicationProvider.getApplicationContext().getResources().getAssets().open("fullResponse.xml"));
    assertEquals(2, response.getDeletedObjects().size());
    assertEquals(1, response.getErrors().size());
    boolean seen1 = false;
    boolean seen3 = false;
    for (DeletedObject obj : response.getDeletedObjects()) {
        if (obj.getKey().equals("key1")) {
            seen1 = true;
            assertEquals("Version1", obj.getVersionId());
            assertNull(obj.getDeleteMarkerVersionId());
            assertFalse(obj.isDeleteMarker());
        } else if (obj.getKey().equals("key3")) {
            seen3 = true;
            assertNull(obj.getVersionId());
            assertEquals("Version3", obj.getDeleteMarkerVersionId());
            assertTrue(obj.isDeleteMarker());
        } else {
            fail("Unexpected object found: " + obj.getKey());
        }
    }
    assertTrue("Didn't find key1", seen1);
    assertTrue("Didn't find key3", seen3);
    assertEquals("key2", response.getErrors().get(0).getKey());
    assertEquals("Version2", response.getErrors().get(0).getVersionId());
    assertEquals("Code", response.getErrors().get(0).getCode());
    assertEquals("Message", response.getErrors().get(0).getMessage());
}
Also used : DeleteObjectsResponse(com.amazonaws.services.s3.internal.DeleteObjectsResponse) DeleteObjectsResultUnmarshaller(com.amazonaws.services.s3.model.transform.Unmarshallers.DeleteObjectsResultUnmarshaller) DeletedObject(com.amazonaws.services.s3.model.DeleteObjectsResult.DeletedObject) Test(org.junit.Test)

Aggregations

DeleteObjectsResponse (com.amazonaws.services.s3.internal.DeleteObjectsResponse)3 DeleteObjectsResultUnmarshaller (com.amazonaws.services.s3.model.transform.Unmarshallers.DeleteObjectsResultUnmarshaller)2 Test (org.junit.Test)2 AbortedException (com.amazonaws.AbortedException)1 AmazonClientException (com.amazonaws.AmazonClientException)1 AmazonServiceException (com.amazonaws.AmazonServiceException)1 ResponseHeaderHandlerChain (com.amazonaws.services.s3.internal.ResponseHeaderHandlerChain)1 DeletedObject (com.amazonaws.services.s3.model.DeleteObjectsResult.DeletedObject)1 MultiObjectDeleteXmlFactory (com.amazonaws.services.s3.model.transform.MultiObjectDeleteXmlFactory)1 Unmarshallers (com.amazonaws.services.s3.model.transform.Unmarshallers)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1