Search in sources :

Example 1 with DeleteObjectsResult

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

the class ResponseParsers method parseDeleteObjectsResult.

/**
 * Unmarshall delete objects response body to corresponding result.
 */
@SuppressWarnings("unchecked")
public static DeleteObjectsResult parseDeleteObjectsResult(InputStream responseBody) throws ResponseParseException {
    try {
        Element root = getXmlRootElement(responseBody);
        DeleteObjectsResult deleteObjectsResult = new DeleteObjectsResult();
        if (root.getChild("EncodingType") != null) {
            String encodingType = root.getChildText("EncodingType");
            deleteObjectsResult.setEncodingType(isNullOrEmpty(encodingType) ? null : encodingType);
        }
        List<String> deletedObjects = new ArrayList<String>();
        List<Element> deletedElements = root.getChildren("Deleted");
        for (Element elem : deletedElements) {
            deletedObjects.add(elem.getChildText("Key"));
        }
        deleteObjectsResult.setDeletedObjects(deletedObjects);
        return deleteObjectsResult;
    } catch (JDOMParseException e) {
        throw new ResponseParseException(e.getPartialDocument() + ": " + e.getMessage(), e);
    } catch (Exception e) {
        throw new ResponseParseException(e.getMessage(), e);
    }
}
Also used : JDOMParseException(org.jdom.input.JDOMParseException) Element(org.jdom.Element) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException) ArrayList(java.util.ArrayList) DeleteObjectsResult(com.aliyun.oss.model.DeleteObjectsResult) ParseException(java.text.ParseException) JDOMParseException(org.jdom.input.JDOMParseException) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException)

Example 2 with DeleteObjectsResult

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

the class ListObjectsSample method main.

public static void main(String[] args) throws IOException {
    OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    try {
        final String content = "Hello OSS";
        final String keyPrefix = "MyObjectKey";
        if (!client.doesBucketExist(bucketName)) {
            client.createBucket(bucketName);
        }
        // Prepare the environment---inserting 100 test files.
        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);
            keys.add(key);
        }
        System.out.println("Put " + keys.size() + " objects completed.");
        ObjectListing objectListing = null;
        String nextMarker = null;
        final int maxKeys = 30;
        List<OSSObjectSummary> sums = null;
        // use default parameter to list the files. By default up to 100 entries could be listed.
        System.out.println("Default paramters:");
        objectListing = client.listObjects(bucketName);
        sums = objectListing.getObjectSummaries();
        for (OSSObjectSummary s : sums) {
            System.out.println("\t" + s.getKey());
        }
        // Sets the max keys with 200 (the max value could be 1000).
        System.out.println("With max keys:");
        objectListing = client.listObjects(new ListObjectsRequest(bucketName).withMaxKeys(200));
        sums = objectListing.getObjectSummaries();
        for (OSSObjectSummary s : sums) {
            System.out.println("\t" + s.getKey());
        }
        // Gets the object with specified prefix. By default it returns up to 100 entries.
        System.out.println("With prefix:");
        objectListing = client.listObjects(new ListObjectsRequest(bucketName).withPrefix(keyPrefix));
        sums = objectListing.getObjectSummaries();
        for (OSSObjectSummary s : sums) {
            System.out.println("\t" + s.getKey());
        }
        // Gets the object with specified marker. By default it returns up to 100 entries.
        System.out.println("With marker: ");
        objectListing = client.listObjects(new ListObjectsRequest(bucketName).withMarker(keyPrefix + "11"));
        sums = objectListing.getObjectSummaries();
        for (OSSObjectSummary s : sums) {
            System.out.println("\t" + s.getKey());
        }
        // Gets all object by paging. Each page will have up to 100 entries.
        System.out.println("List all objects:");
        nextMarker = null;
        do {
            objectListing = client.listObjects(new ListObjectsRequest(bucketName).withMarker(nextMarker).withMaxKeys(maxKeys));
            sums = objectListing.getObjectSummaries();
            for (OSSObjectSummary s : sums) {
                System.out.println("\t" + s.getKey());
            }
            nextMarker = objectListing.getNextMarker();
        } while (objectListing.isTruncated());
        // Gets all object with specified prefix by paging. Each page will have up to 100 entries.
        System.out.println("List all objects after marker:");
        nextMarker = keyPrefix + "11";
        do {
            objectListing = client.listObjects(new ListObjectsRequest(bucketName).withMarker(nextMarker).withMaxKeys(maxKeys));
            sums = objectListing.getObjectSummaries();
            for (OSSObjectSummary s : sums) {
                System.out.println("\t" + s.getKey());
            }
            nextMarker = objectListing.getNextMarker();
        } while (objectListing.isTruncated());
        // Gets all object with specified marker by paging. Each page will have up to 100 entries.
        System.out.println("List all objects with prefix:");
        nextMarker = null;
        do {
            objectListing = client.listObjects(new ListObjectsRequest(bucketName).withPrefix(keyPrefix + "1").withMarker(nextMarker).withMaxKeys(maxKeys));
            sums = objectListing.getObjectSummaries();
            for (OSSObjectSummary s : sums) {
                System.out.println("\t" + s.getKey());
            }
            nextMarker = objectListing.getNextMarker();
        } while (objectListing.isTruncated());
        // Clean up the environment----deleting the test files.
        System.out.println("Deleting 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);
        }
    } 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) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ObjectListing(com.aliyun.oss.model.ObjectListing) OSSException(com.aliyun.oss.OSSException) DeleteObjectsResult(com.aliyun.oss.model.DeleteObjectsResult) OSS(com.aliyun.oss.OSS) DeleteObjectsRequest(com.aliyun.oss.model.DeleteObjectsRequest) OSSObjectSummary(com.aliyun.oss.model.OSSObjectSummary) ListObjectsRequest(com.aliyun.oss.model.ListObjectsRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) ClientException(com.aliyun.oss.ClientException) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder)

Example 3 with DeleteObjectsResult

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

the class DeleteObjectsTest method testDeleleObjectsQuietly.

@Test
public void testDeleleObjectsQuietly() {
    List<String> existingKeys = new ArrayList<String>();
    final int keyCount = 100;
    final String keyPrefix = "delete-objects-quietly";
    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.setQuiet(true);
    request.setKeys(existingKeys);
    try {
        DeleteObjectsResult result = ossClient.deleteObjects(request);
        List<String> deletedObjects = result.getDeletedObjects();
        Assert.assertEquals(0, 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 4 with DeleteObjectsResult

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

the class GetRequestIdTest method testNormalGetRequestId.

@Test
public void testNormalGetRequestId() {
    final String key = "normal-get-request-id";
    final long inputStreamLength = 1024;
    final int requestIdLength = "572BF2F2207FB3397648E9F1".length();
    try {
        // put object
        PutObjectResult putObjectResult = ossClient.putObject(bucketName, key, genFixedLengthInputStream(inputStreamLength));
        Assert.assertEquals(putObjectResult.getRequestId().length(), requestIdLength);
        // get object
        OSSObject ossObject = ossClient.getObject(bucketName, key);
        ossObject.getObjectContent().close();
        Assert.assertEquals(ossObject.getRequestId().length(), requestIdLength);
        File file = new File("tmp");
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
        ObjectMetadata objectMeta = ossClient.getObject(getObjectRequest, file);
        Assert.assertEquals(objectMeta.getRequestId().length(), requestIdLength);
        // delete object
        ossClient.deleteObject(bucketName, key);
        // append object
        AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, key, file);
        appendObjectRequest.setPosition(0L);
        AppendObjectResult appendObjectResult = ossClient.appendObject(appendObjectRequest);
        Assert.assertEquals(appendObjectResult.getRequestId().length(), requestIdLength);
        // getSimplifiedObjectMeta
        SimplifiedObjectMeta simplifiedObjectMeta = ossClient.getSimplifiedObjectMeta(bucketName, key);
        Assert.assertEquals(simplifiedObjectMeta.getRequestId().length(), requestIdLength);
        // getObjectMetadata
        ObjectMetadata objectMetadata = ossClient.getObjectMetadata(bucketName, key);
        Assert.assertEquals(objectMetadata.getRequestId().length(), requestIdLength);
        // delete objects
        DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName);
        ArrayList<String> keys = new ArrayList<String>();
        keys.add(key);
        deleteObjectsRequest.setKeys(keys);
        DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(deleteObjectsRequest);
        Assert.assertEquals(deleteObjectsResult.getRequestId().length(), requestIdLength);
        // initiate multipart upload
        InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(bucketName, key);
        InitiateMultipartUploadResult initiateMultipartUploadResult = ossClient.initiateMultipartUpload(initiateMultipartUploadRequest);
        Assert.assertEquals(initiateMultipartUploadResult.getRequestId().length(), requestIdLength);
        // upload part
        UploadPartRequest uploadPartRequest = new UploadPartRequest(bucketName, key, initiateMultipartUploadResult.getUploadId(), 1, new FileInputStream(file), inputStreamLength);
        UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
        Assert.assertEquals(uploadPartResult.getRequestId().length(), requestIdLength);
        // complete multipart upload
        List<PartETag> partETags = new ArrayList<PartETag>();
        partETags.add(new PartETag(1, uploadPartResult.getETag()));
        CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, initiateMultipartUploadResult.getUploadId(), partETags);
        CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
        Assert.assertEquals(completeMultipartUploadResult.getRequestId().length(), requestIdLength);
        // copy object
        CopyObjectResult CopyObjectResult = ossClient.copyObject(bucketName, key, bucketName, key);
        Assert.assertEquals(CopyObjectResult.getRequestId().length(), requestIdLength);
        // initiate multipart copy
        InitiateMultipartUploadRequest initiateMultipartCopyRequest = new InitiateMultipartUploadRequest(bucketName, key);
        InitiateMultipartUploadResult initiateMultipartCopyResult = ossClient.initiateMultipartUpload(initiateMultipartCopyRequest);
        Assert.assertEquals(initiateMultipartCopyResult.getRequestId().length(), requestIdLength);
        // upload part copy
        UploadPartCopyRequest uploadPartCopyRequest = new UploadPartCopyRequest(bucketName, key, bucketName, key, initiateMultipartCopyResult.getUploadId(), 1, 0L, inputStreamLength);
        UploadPartCopyResult uploadPartCopyResult = ossClient.uploadPartCopy(uploadPartCopyRequest);
        Assert.assertEquals(uploadPartCopyResult.getRequestId().length(), requestIdLength);
        // abort multipart upload
        AbortMultipartUploadRequest AbortMultipartUploadRequest = new AbortMultipartUploadRequest(bucketName, key, initiateMultipartCopyResult.getUploadId());
        ossClient.abortMultipartUpload(AbortMultipartUploadRequest);
        ossClient.deleteObject(bucketName, key);
        file.delete();
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : SimplifiedObjectMeta(com.aliyun.oss.model.SimplifiedObjectMeta) InitiateMultipartUploadResult(com.aliyun.oss.model.InitiateMultipartUploadResult) ArrayList(java.util.ArrayList) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) PartETag(com.aliyun.oss.model.PartETag) AppendObjectResult(com.aliyun.oss.model.AppendObjectResult) UploadPartResult(com.aliyun.oss.model.UploadPartResult) UploadPartCopyResult(com.aliyun.oss.model.UploadPartCopyResult) CopyObjectResult(com.aliyun.oss.model.CopyObjectResult) UploadPartCopyRequest(com.aliyun.oss.model.UploadPartCopyRequest) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest) OSSObject(com.aliyun.oss.model.OSSObject) PutObjectResult(com.aliyun.oss.model.PutObjectResult) InitiateMultipartUploadRequest(com.aliyun.oss.model.InitiateMultipartUploadRequest) UploadPartRequest(com.aliyun.oss.model.UploadPartRequest) AbortMultipartUploadRequest(com.aliyun.oss.model.AbortMultipartUploadRequest) DeleteObjectsResult(com.aliyun.oss.model.DeleteObjectsResult) FileInputStream(java.io.FileInputStream) DeleteObjectsRequest(com.aliyun.oss.model.DeleteObjectsRequest) AppendObjectRequest(com.aliyun.oss.model.AppendObjectRequest) File(java.io.File) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Test(org.junit.Test)

Example 5 with DeleteObjectsResult

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

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