Search in sources :

Example 1 with DeleteObjectsResponse

use of com.qcloud.cos.internal.DeleteObjectsResponse in project cos-java-sdk-v5 by tencentyun.

the class COSClient method deleteObjects.

@Override
public DeleteObjectsResult deleteObjects(DeleteObjectsRequest deleteObjectsRequest) throws MultiObjectDeleteException, CosClientException, CosServiceException {
    rejectNull(deleteObjectsRequest, "The DeleteObjectsRequest parameter must be specified when deleting objects");
    rejectNull(clientConfig.getRegion(), "region is null, region in clientConfig must be specified when deleting objects");
    CosHttpRequest<DeleteObjectsRequest> request = createRequest(deleteObjectsRequest.getBucketName(), null, deleteObjectsRequest, HttpMethodName.POST);
    request.addParameter("delete", null);
    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 {
        byte[] md5 = Md5Utils.computeMD5Hash(content);
        String md5Base64 = BinaryUtils.toBase64(md5);
        request.addHeader("Content-MD5", md5Base64);
    } catch (Exception e) {
        throw new CosClientException("Couldn't compute md5 sum", e);
    }
    @SuppressWarnings("unchecked") ResponseHeaderHandlerChain<DeleteObjectsResponse> responseHandler = new ResponseHeaderHandlerChain<DeleteObjectsResponse>(new Unmarshallers.DeleteObjectsResultUnmarshaller());
    DeleteObjectsResponse response = invoke(request, responseHandler);
    /*
         * If the result was only partially successful, throw an exception
         */
    if (!response.getErrors().isEmpty()) {
        Map<String, String> headers = responseHandler.getResponseHeaders();
        MultiObjectDeleteException ex = new MultiObjectDeleteException(response.getErrors(), response.getDeletedObjects());
        ex.setStatusCode(200);
        ex.setRequestId(headers.get(Headers.REQUEST_ID));
        throw ex;
    }
    DeleteObjectsResult result = new DeleteObjectsResult(response.getDeletedObjects());
    return result;
}
Also used : DeleteObjectsResponse(com.qcloud.cos.internal.DeleteObjectsResponse) Unmarshallers(com.qcloud.cos.internal.Unmarshallers) CosClientException(com.qcloud.cos.exception.CosClientException) DecoderException(org.apache.commons.codec.DecoderException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MultiObjectDeleteException(com.qcloud.cos.exception.MultiObjectDeleteException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CosServiceException(com.qcloud.cos.exception.CosServiceException) CosClientException(com.qcloud.cos.exception.CosClientException) MalformedURLException(java.net.MalformedURLException) MultiObjectDeleteXmlFactory(com.qcloud.cos.internal.MultiObjectDeleteXmlFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) MultiObjectDeleteException(com.qcloud.cos.exception.MultiObjectDeleteException) ResponseHeaderHandlerChain(com.qcloud.cos.internal.ResponseHeaderHandlerChain)

Aggregations

CosClientException (com.qcloud.cos.exception.CosClientException)1 CosServiceException (com.qcloud.cos.exception.CosServiceException)1 MultiObjectDeleteException (com.qcloud.cos.exception.MultiObjectDeleteException)1 DeleteObjectsResponse (com.qcloud.cos.internal.DeleteObjectsResponse)1 MultiObjectDeleteXmlFactory (com.qcloud.cos.internal.MultiObjectDeleteXmlFactory)1 ResponseHeaderHandlerChain (com.qcloud.cos.internal.ResponseHeaderHandlerChain)1 Unmarshallers (com.qcloud.cos.internal.Unmarshallers)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 DecoderException (org.apache.commons.codec.DecoderException)1