Search in sources :

Example 6 with DeleteObjectsResult

use of com.aliyun.oss.model.DeleteObjectsResult in project hadoop by apache.

the class AliyunOSSFileSystemStore method deleteObjects.

/**
   * Delete a list of keys, and update write operation statistics.
   *
   * @param keysToDelete collection of keys to delete.
   * @throws IOException if failed to delete objects.
   */
public void deleteObjects(List<String> keysToDelete) throws IOException {
    if (CollectionUtils.isEmpty(keysToDelete)) {
        LOG.warn("Keys to delete is empty.");
        return;
    }
    int retry = 10;
    int tries = 0;
    List<String> deleteFailed = keysToDelete;
    while (CollectionUtils.isNotEmpty(deleteFailed)) {
        DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest(bucketName);
        deleteRequest.setKeys(deleteFailed);
        // There are two modes to do batch delete:
        // 1. detail mode: DeleteObjectsResult.getDeletedObjects returns objects
        // which were deleted successfully.
        // 2. simple mode: DeleteObjectsResult.getDeletedObjects returns objects
        // which were deleted unsuccessfully.
        // Here, we choose the simple mode to do batch delete.
        deleteRequest.setQuiet(true);
        DeleteObjectsResult result = ossClient.deleteObjects(deleteRequest);
        deleteFailed = result.getDeletedObjects();
        tries++;
        if (tries == retry) {
            break;
        }
    }
    if (tries == retry && CollectionUtils.isNotEmpty(deleteFailed)) {
        // Aliyun OSS service problems.
        throw new IOException("Failed to delete Aliyun OSS objects for " + tries + " times.");
    }
}
Also used : DeleteObjectsResult(com.aliyun.oss.model.DeleteObjectsResult) IOException(java.io.IOException) DeleteObjectsRequest(com.aliyun.oss.model.DeleteObjectsRequest)

Example 7 with DeleteObjectsResult

use of com.aliyun.oss.model.DeleteObjectsResult in project aliyun-oss-java-sdk by aliyun.

the class DeleteObjectsTest method testDeleteObjectsWithEncodingType.

@Ignore
public void testDeleteObjectsWithEncodingType() {
    final String objectPrefix = "object-with-special-characters-";
    try {
        // Add several objects with special characters into bucket
        List<String> existingKeys = new ArrayList<String>();
        existingKeys.add(objectPrefix + "\001\007");
        existingKeys.add(objectPrefix + "\002\007");
        if (!batchPutObject(ossClient, bucketName, existingKeys)) {
            Assert.fail("batch put object failed");
        }
        // List objects under nonempty bucket
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName);
        listObjectsRequest.setEncodingType(DEFAULT_ENCODING_TYPE);
        ObjectListing objectListing = ossClient.listObjects(listObjectsRequest);
        List<String> returnedKeys = new ArrayList<String>();
        for (OSSObjectSummary s : objectListing.getObjectSummaries()) {
            String decodedKey = URLDecoder.decode(s.getKey(), "UTF-8");
            returnedKeys.add(decodedKey);
            Assert.assertTrue(existingKeys.contains(decodedKey));
        }
        Assert.assertEquals(existingKeys.size(), objectListing.getObjectSummaries().size());
        // Delete multiple objects
        DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName);
        deleteObjectsRequest.setEncodingType(DEFAULT_ENCODING_TYPE);
        deleteObjectsRequest.setKeys(returnedKeys);
        deleteObjectsRequest.setQuiet(false);
        DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(deleteObjectsRequest);
        Assert.assertEquals(DEFAULT_ENCODING_TYPE, deleteObjectsResult.getEncodingType());
        Assert.assertEquals(existingKeys.size(), deleteObjectsResult.getDeletedObjects().size());
        for (String o : deleteObjectsResult.getDeletedObjects()) {
            String decodedKey = URLDecoder.decode(o, "UTF-8");
            Assert.assertTrue(existingKeys.contains(decodedKey));
        }
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : ListObjectsRequest(com.aliyun.oss.model.ListObjectsRequest) OSSObjectSummary(com.aliyun.oss.model.OSSObjectSummary) ArrayList(java.util.ArrayList) ObjectListing(com.aliyun.oss.model.ObjectListing) DeleteObjectsResult(com.aliyun.oss.model.DeleteObjectsResult) DeleteObjectsRequest(com.aliyun.oss.model.DeleteObjectsRequest) Ignore(org.junit.Ignore)

Example 8 with DeleteObjectsResult

use of com.aliyun.oss.model.DeleteObjectsResult in project aliyun-oss-java-sdk by aliyun.

the class DeleteObjectsTest method testDeleleNonexistentObjects.

@Test
public void testDeleleNonexistentObjects() {
    List<String> nonexistentKeys = new ArrayList<String>();
    final int keyCount = 100;
    final String keyPrefix = "delete-nonexistent-objects";
    for (int i = 0; i < keyCount; i++) {
        nonexistentKeys.add(keyPrefix + i);
    }
    DeleteObjectsRequest request = new DeleteObjectsRequest(bucketName);
    request.setKeys(nonexistentKeys);
    try {
        DeleteObjectsResult result = ossClient.deleteObjects(request);
        List<String> deletedObjects = result.getDeletedObjects();
        Assert.assertEquals(keyCount, deletedObjects.size());
        Assert.assertEquals(result.getRequestId().length(), REQUEST_ID_LEN);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : ArrayList(java.util.ArrayList) DeleteObjectsResult(com.aliyun.oss.model.DeleteObjectsResult) DeleteObjectsRequest(com.aliyun.oss.model.DeleteObjectsRequest) Test(org.junit.Test)

Example 9 with DeleteObjectsResult

use of com.aliyun.oss.model.DeleteObjectsResult in project aliyun-oss-java-sdk by aliyun.

the class DeleteObjectsTest method testDeleleExistingObjects.

@Test
public void testDeleleExistingObjects() {
    List<String> existingKeys = new ArrayList<String>();
    final int keyCount = 100;
    final String keyPrefix = "delete-existing-objects";
    for (int i = 0; i < keyCount; i++) {
        existingKeys.add(keyPrefix + i);
    }
    if (!batchPutObject(ossClient, bucketName, existingKeys)) {
        Assert.fail("batch put object failed");
    }
    DeleteObjectsRequest request = new DeleteObjectsRequest(bucketName);
    request.setKeys(existingKeys);
    try {
        DeleteObjectsResult result = ossClient.deleteObjects(request);
        List<String> deletedObjects = result.getDeletedObjects();
        Assert.assertEquals(keyCount, deletedObjects.size());
        Assert.assertEquals(result.getRequestId().length(), REQUEST_ID_LEN);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : ArrayList(java.util.ArrayList) DeleteObjectsResult(com.aliyun.oss.model.DeleteObjectsResult) DeleteObjectsRequest(com.aliyun.oss.model.DeleteObjectsRequest) Test(org.junit.Test)

Example 10 with DeleteObjectsResult

use of com.aliyun.oss.model.DeleteObjectsResult in project aliyun-oss-java-sdk by aliyun.

the class DeleteObjectsSample method main.

public static void main(String[] args) throws IOException {
    /*
         * Constructs a client instance with your account for accessing OSS
         */
    OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    try {
        /*
             * Batch put objects into the bucket
             */
        final String content = "Thank you for using Aliyun Object Storage Service";
        final String keyPrefix = "MyObjectKey";
        List<String> keys = new ArrayList<String>();
        for (int i = 0; i < 100; i++) {
            String key = keyPrefix + i;
            InputStream instream = new ByteArrayInputStream(content.getBytes());
            client.putObject(bucketName, key, instream);
            System.out.println("Succeed to put object " + key);
            keys.add(key);
        }
        System.out.println();
        /*
             * Delete all objects uploaded recently under the bucket
             */
        System.out.println("\nDeleting all objects:");
        DeleteObjectsResult deleteObjectsResult = client.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(keys));
        List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
        for (String object : deletedObjects) {
            System.out.println("\t" + object);
        }
        System.out.println();
    } catch (OSSException oe) {
        System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason.");
        System.out.println("Error Message: " + oe.getErrorCode());
        System.out.println("Error Code:       " + oe.getErrorCode());
        System.out.println("Request ID:      " + oe.getRequestId());
        System.out.println("Host ID:           " + oe.getHostId());
    } catch (ClientException ce) {
        System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network.");
        System.out.println("Error Message: " + ce.getMessage());
    } finally {
        /*
             * Do not forget to shut down the client finally to release all allocated resources.
             */
        client.shutdown();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) OSSException(com.aliyun.oss.OSSException) DeleteObjectsResult(com.aliyun.oss.model.DeleteObjectsResult) ClientException(com.aliyun.oss.ClientException) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) DeleteObjectsRequest(com.aliyun.oss.model.DeleteObjectsRequest)

Aggregations

DeleteObjectsResult (com.aliyun.oss.model.DeleteObjectsResult)10 DeleteObjectsRequest (com.aliyun.oss.model.DeleteObjectsRequest)9 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)4 ClientException (com.aliyun.oss.ClientException)2 OSS (com.aliyun.oss.OSS)2 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)2 OSSException (com.aliyun.oss.OSSException)2 ListObjectsRequest (com.aliyun.oss.model.ListObjectsRequest)2 OSSObjectSummary (com.aliyun.oss.model.OSSObjectSummary)2 ObjectListing (com.aliyun.oss.model.ObjectListing)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 ResponseParseException (com.aliyun.oss.common.parser.ResponseParseException)1 AbortMultipartUploadRequest (com.aliyun.oss.model.AbortMultipartUploadRequest)1 AppendObjectRequest (com.aliyun.oss.model.AppendObjectRequest)1 AppendObjectResult (com.aliyun.oss.model.AppendObjectResult)1 CompleteMultipartUploadRequest (com.aliyun.oss.model.CompleteMultipartUploadRequest)1 CompleteMultipartUploadResult (com.aliyun.oss.model.CompleteMultipartUploadResult)1 CopyObjectResult (com.aliyun.oss.model.CopyObjectResult)1