Search in sources :

Example 11 with DeleteObjectsRequest

use of com.aliyun.oss.model.DeleteObjectsRequest in project druid by druid-io.

the class OssTaskLogsTest method test_killOlderThan_noException_deletesOnlyTaskLogsOlderThan.

@Test
public void test_killOlderThan_noException_deletesOnlyTaskLogsOlderThan() throws IOException {
    OSSObjectSummary objectSummary1 = OssTestUtils.newOSSObjectSummary(TEST_BUCKET, KEY_1, TIME_0);
    OSSObjectSummary objectSummary2 = OssTestUtils.newOSSObjectSummary(TEST_BUCKET, KEY_2, TIME_FUTURE);
    OssTestUtils.expectListObjects(ossClient, PREFIX_URI, ImmutableList.of(objectSummary1, objectSummary2));
    DeleteObjectsRequest deleteRequest1 = new DeleteObjectsRequest(TEST_BUCKET);
    deleteRequest1.setKeys(Collections.singletonList(KEY_1));
    OssTestUtils.mockClientDeleteObjects(ossClient, ImmutableList.of(deleteRequest1), ImmutableMap.of());
    EasyMock.replay(ossClient, timeSupplier);
    OssTaskLogsConfig config = new OssTaskLogsConfig();
    config.setBucket(TEST_BUCKET);
    config.setPrefix(TEST_PREFIX);
    OssInputDataConfig inputDataConfig = new OssInputDataConfig();
    inputDataConfig.setMaxListingLength(MAX_KEYS);
    OssTaskLogs taskLogs = new OssTaskLogs(ossClient, config, inputDataConfig, timeSupplier);
    taskLogs.killOlderThan(TIME_NOW);
    EasyMock.verify(ossClient, timeSupplier);
}
Also used : OSSObjectSummary(com.aliyun.oss.model.OSSObjectSummary) DeleteObjectsRequest(com.aliyun.oss.model.DeleteObjectsRequest) Test(org.junit.Test)

Example 12 with DeleteObjectsRequest

use of com.aliyun.oss.model.DeleteObjectsRequest in project druid by druid-io.

the class OssTestUtils method mockClientDeleteObjects.

public static void mockClientDeleteObjects(OSS client, List<DeleteObjectsRequest> deleteRequestsExpected, Map<DeleteObjectsRequest, Exception> requestToException) {
    Map<DeleteObjectsRequest, IExpectationSetters<DeleteObjectsResult>> requestToResultExpectationSetter = new HashMap<>();
    for (Map.Entry<DeleteObjectsRequest, Exception> requestsAndErrors : requestToException.entrySet()) {
        DeleteObjectsRequest request = requestsAndErrors.getKey();
        Exception exception = requestsAndErrors.getValue();
        IExpectationSetters<DeleteObjectsResult> resultExpectationSetter = requestToResultExpectationSetter.get(request);
        if (resultExpectationSetter == null) {
            client.deleteObjects(OssTestUtils.deleteObjectsRequestArgumentMatcher(request));
            resultExpectationSetter = EasyMock.<DeleteObjectsResult>expectLastCall().andThrow(exception);
            requestToResultExpectationSetter.put(request, resultExpectationSetter);
        } else {
            resultExpectationSetter.andThrow(exception);
        }
    }
    for (DeleteObjectsRequest request : deleteRequestsExpected) {
        IExpectationSetters<DeleteObjectsResult> resultExpectationSetter = requestToResultExpectationSetter.get(request);
        if (resultExpectationSetter == null) {
            client.deleteObjects(OssTestUtils.deleteObjectsRequestArgumentMatcher(request));
            resultExpectationSetter = EasyMock.expectLastCall();
            requestToResultExpectationSetter.put(request, resultExpectationSetter);
        }
        resultExpectationSetter.andReturn(new DeleteObjectsResult());
    }
}
Also used : IExpectationSetters(org.easymock.IExpectationSetters) HashMap(java.util.HashMap) DeleteObjectsResult(com.aliyun.oss.model.DeleteObjectsResult) HashMap(java.util.HashMap) Map(java.util.Map) DeleteObjectsRequest(com.aliyun.oss.model.DeleteObjectsRequest)

Example 13 with DeleteObjectsRequest

use of com.aliyun.oss.model.DeleteObjectsRequest 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 14 with DeleteObjectsRequest

use of com.aliyun.oss.model.DeleteObjectsRequest 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 15 with DeleteObjectsRequest

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

the class DeleteObjectsTest method testDeleleNullOrEmptyObjects.

@Test
public void testDeleleNullOrEmptyObjects() {
    List<String> emptyKeys = new ArrayList<String>();
    DeleteObjectsRequest request = new DeleteObjectsRequest(bucketName);
    try {
        request.setKeys(emptyKeys);
        ossClient.deleteObjects(request);
        Assert.fail("Delete objects should not be successfully");
    } catch (Exception e) {
        Assert.assertTrue(e instanceof IllegalArgumentException);
    }
    List<String> withNullKeys = new ArrayList<String>();
    withNullKeys.add("dummykey");
    withNullKeys.add(null);
    try {
        request.setKeys(withNullKeys);
        ossClient.deleteObjects(request);
        Assert.fail("Delete objects should not be successfully");
    } catch (Exception e) {
        Assert.assertTrue(e instanceof IllegalArgumentException);
    }
}
Also used : ArrayList(java.util.ArrayList) DeleteObjectsRequest(com.aliyun.oss.model.DeleteObjectsRequest) Test(org.junit.Test)

Aggregations

DeleteObjectsRequest (com.aliyun.oss.model.DeleteObjectsRequest)23 Test (org.junit.Test)15 OSSObjectSummary (com.aliyun.oss.model.OSSObjectSummary)12 ArrayList (java.util.ArrayList)10 DeleteObjectsResult (com.aliyun.oss.model.DeleteObjectsResult)9 IOException (java.io.IOException)4 ListObjectsRequest (com.aliyun.oss.model.ListObjectsRequest)3 ObjectListing (com.aliyun.oss.model.ObjectListing)3 ClientException (com.aliyun.oss.ClientException)2 OSS (com.aliyun.oss.OSS)2 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)2 OSSException (com.aliyun.oss.OSSException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 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