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);
}
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());
}
}
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.");
}
}
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());
}
}
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);
}
}
Aggregations