Search in sources :

Example 16 with DeleteObjectsRequest

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

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

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

Example 19 with DeleteObjectsRequest

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

the class OssDataSegmentKillerTest method test_killAll_noException_deletesAllSegments.

@Test
public void test_killAll_noException_deletesAllSegments() throws IOException {
    OSSObjectSummary objectSummary1 = OssTestUtils.newOSSObjectSummary(TEST_BUCKET, KEY_1, TIME_0);
    OSSObjectSummary objectSummary2 = OssTestUtils.newOSSObjectSummary(TEST_BUCKET, KEY_2, TIME_1);
    OssTestUtils.expectListObjects(client, PREFIX_URI, ImmutableList.of(objectSummary1, objectSummary2));
    DeleteObjectsRequest deleteRequest1 = new DeleteObjectsRequest(TEST_BUCKET);
    deleteRequest1.setKeys(Collections.singletonList(KEY_1));
    DeleteObjectsRequest deleteRequest2 = new DeleteObjectsRequest(TEST_BUCKET);
    deleteRequest2.setKeys(Collections.singletonList(KEY_2));
    OssTestUtils.mockClientDeleteObjects(client, ImmutableList.of(deleteRequest1, deleteRequest2), ImmutableMap.of());
    EasyMock.expect(segmentPusherConfig.getBucket()).andReturn(TEST_BUCKET);
    EasyMock.expectLastCall().anyTimes();
    EasyMock.expect(segmentPusherConfig.getPrefix()).andReturn(TEST_PREFIX);
    EasyMock.expectLastCall().anyTimes();
    EasyMock.expect(inputDataConfig.getMaxListingLength()).andReturn(MAX_KEYS);
    EasyMock.expectLastCall().anyTimes();
    EasyMock.replay(client, segmentPusherConfig, inputDataConfig);
    segmentKiller = new OssDataSegmentKiller(Suppliers.ofInstance(client), segmentPusherConfig, inputDataConfig);
    segmentKiller.killAll();
    EasyMock.verify(client, segmentPusherConfig, inputDataConfig);
}
Also used : OSSObjectSummary(com.aliyun.oss.model.OSSObjectSummary) DeleteObjectsRequest(com.aliyun.oss.model.DeleteObjectsRequest) Test(org.junit.Test)

Example 20 with DeleteObjectsRequest

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

the class OssDataSegmentKillerTest method test_killAll_nonrecoverableExceptionWhenListingObjects_deletesAllSegments.

@Test
public void test_killAll_nonrecoverableExceptionWhenListingObjects_deletesAllSegments() {
    boolean ioExceptionThrown = false;
    try {
        OSSObjectSummary objectSummary1 = OssTestUtils.newOSSObjectSummary(TEST_BUCKET, KEY_1, TIME_0);
        OssTestUtils.expectListObjects(client, PREFIX_URI, ImmutableList.of(objectSummary1));
        DeleteObjectsRequest deleteRequest1 = new DeleteObjectsRequest(TEST_BUCKET);
        deleteRequest1.withKeys(ImmutableList.of(KEY_1));
        OssTestUtils.mockClientDeleteObjects(client, ImmutableList.of(), ImmutableMap.of(deleteRequest1, NON_RECOVERABLE_EXCEPTION));
        EasyMock.expect(segmentPusherConfig.getBucket()).andReturn(TEST_BUCKET);
        EasyMock.expectLastCall().anyTimes();
        EasyMock.expect(segmentPusherConfig.getPrefix()).andReturn(TEST_PREFIX);
        EasyMock.expectLastCall().anyTimes();
        EasyMock.expect(inputDataConfig.getMaxListingLength()).andReturn(MAX_KEYS);
        EasyMock.expectLastCall().anyTimes();
        EasyMock.replay(client, segmentPusherConfig, inputDataConfig);
        segmentKiller = new OssDataSegmentKiller(Suppliers.ofInstance(client), segmentPusherConfig, inputDataConfig);
        segmentKiller.killAll();
    } catch (IOException e) {
        ioExceptionThrown = true;
    }
    Assert.assertTrue(ioExceptionThrown);
    EasyMock.verify(client, segmentPusherConfig, inputDataConfig);
}
Also used : OSSObjectSummary(com.aliyun.oss.model.OSSObjectSummary) IOException(java.io.IOException) 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