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