Search in sources :

Example 11 with PartListing

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

the class UploadPartTest method testNormalListPartsWithEncoding.

@Test
public void testNormalListPartsWithEncoding() {
    final String key = "normal-list-parts-常记溪亭日暮,沉醉不知归路";
    final int partSize = 128 * 1024;
    final int partCount = 25;
    try {
        String uploadId = claimUploadId(ossClient, bucketName, key);
        // List parts under empty bucket
        ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, key, uploadId);
        PartListing partListing = ossClient.listParts(listPartsRequest);
        Assert.assertEquals(0, partListing.getParts().size());
        Assert.assertEquals(bucketName, partListing.getBucketName());
        Assert.assertEquals(key, partListing.getKey());
        Assert.assertEquals(uploadId, partListing.getUploadId());
        Assert.assertEquals(LIST_PART_MAX_RETURNS, partListing.getMaxParts().intValue());
        Assert.assertNull(partListing.getNextPartNumberMarker());
        Assert.assertFalse(partListing.isTruncated());
        Assert.assertEquals(partListing.getRequestId().length(), REQUEST_ID_LEN);
        // Upload parts
        List<PartETag> partETags = new ArrayList<PartETag>();
        for (int i = 0; i < partCount; i++) {
            InputStream instream = genFixedLengthInputStream(partSize);
            UploadPartRequest uploadPartRequest = new UploadPartRequest();
            uploadPartRequest.setBucketName(bucketName);
            uploadPartRequest.setKey(key);
            uploadPartRequest.setInputStream(instream);
            uploadPartRequest.setPartNumber(i + 1);
            uploadPartRequest.setPartSize(partSize);
            uploadPartRequest.setUploadId(uploadId);
            UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
            Assert.assertEquals(uploadPartResult.getRequestId().length(), REQUEST_ID_LEN);
            partETags.add(uploadPartResult.getPartETag());
        }
        // List parts with encoding
        listPartsRequest = new ListPartsRequest(bucketName, key, uploadId);
        listPartsRequest.setEncodingType(DEFAULT_ENCODING_TYPE);
        partListing = ossClient.listParts(listPartsRequest);
        Assert.assertEquals(partCount, partListing.getParts().size());
        for (int i = 0; i < partCount; i++) {
            PartSummary ps = partListing.getParts().get(i);
            PartETag eTag = partETags.get(i);
            Assert.assertEquals(eTag.getPartNumber(), ps.getPartNumber());
            Assert.assertEquals(eTag.getETag(), ps.getETag());
        }
        Assert.assertEquals(bucketName, partListing.getBucketName());
        Assert.assertEquals(key, URLDecoder.decode(partListing.getKey(), "UTF-8"));
        Assert.assertEquals(uploadId, partListing.getUploadId());
        Assert.assertEquals(LIST_PART_MAX_RETURNS, partListing.getMaxParts().intValue());
        Assert.assertEquals(partCount, partListing.getNextPartNumberMarker().intValue());
        Assert.assertFalse(partListing.isTruncated());
        Assert.assertEquals(partListing.getRequestId().length(), REQUEST_ID_LEN);
        // Complete multipart upload
        CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
        CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
        Assert.assertEquals(composeLocation(ossClient, OSS_TEST_ENDPOINT, bucketName, key), completeMultipartUploadResult.getLocation());
        Assert.assertEquals(bucketName, completeMultipartUploadResult.getBucketName());
        Assert.assertEquals(key, completeMultipartUploadResult.getKey());
        Assert.assertEquals(calcMultipartsETag(partETags), completeMultipartUploadResult.getETag());
        Assert.assertEquals(completeMultipartUploadResult.getRequestId().length(), REQUEST_ID_LEN);
        ossClient.deleteObject(bucketName, key);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) UploadPartRequest(com.aliyun.oss.model.UploadPartRequest) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) PartETag(com.aliyun.oss.model.PartETag) OSSException(com.aliyun.oss.OSSException) PartListing(com.aliyun.oss.model.PartListing) ListPartsRequest(com.aliyun.oss.model.ListPartsRequest) UploadPartResult(com.aliyun.oss.model.UploadPartResult) PartSummary(com.aliyun.oss.model.PartSummary) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Test(org.junit.Test)

Example 12 with PartListing

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

the class ResponseParsers method parseListParts.

/**
 * Unmarshall list parts response body to part listing.
 */
@SuppressWarnings("unchecked")
public static PartListing parseListParts(InputStream responseBody) throws ResponseParseException {
    try {
        Element root = getXmlRootElement(responseBody);
        PartListing partListing = new PartListing();
        partListing.setBucketName(root.getChildText("Bucket"));
        partListing.setKey(root.getChildText("Key"));
        partListing.setUploadId(root.getChildText("UploadId"));
        partListing.setStorageClass(root.getChildText("StorageClass"));
        partListing.setMaxParts(Integer.valueOf(root.getChildText("MaxParts")));
        partListing.setTruncated(Boolean.valueOf(root.getChildText("IsTruncated")));
        if (root.getChild("PartNumberMarker") != null) {
            String partNumberMarker = root.getChildText("PartNumberMarker");
            if (!isNullOrEmpty(partNumberMarker)) {
                partListing.setPartNumberMarker(Integer.valueOf(partNumberMarker));
            }
        }
        if (root.getChild("NextPartNumberMarker") != null) {
            String nextPartNumberMarker = root.getChildText("NextPartNumberMarker");
            if (!isNullOrEmpty(nextPartNumberMarker)) {
                partListing.setNextPartNumberMarker(Integer.valueOf(nextPartNumberMarker));
            }
        }
        List<Element> partElems = root.getChildren("Part");
        for (Element elem : partElems) {
            PartSummary ps = new PartSummary();
            ps.setPartNumber(Integer.valueOf(elem.getChildText("PartNumber")));
            ps.setLastModified(DateUtil.parseIso8601Date(elem.getChildText("LastModified")));
            ps.setETag(trimQuotes(elem.getChildText("ETag")));
            ps.setSize(Integer.valueOf(elem.getChildText("Size")));
            partListing.addPart(ps);
        }
        return partListing;
    } 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) ParseException(java.text.ParseException) JDOMParseException(org.jdom.input.JDOMParseException) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException) PartListing(com.aliyun.oss.model.PartListing) PartSummary(com.aliyun.oss.model.PartSummary)

Aggregations

PartListing (com.aliyun.oss.model.PartListing)12 ListPartsRequest (com.aliyun.oss.model.ListPartsRequest)10 InputStream (java.io.InputStream)10 Test (org.junit.Test)10 OSSException (com.aliyun.oss.OSSException)9 TestUtils.genFixedLengthInputStream (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream)9 CompleteMultipartUploadRequest (com.aliyun.oss.model.CompleteMultipartUploadRequest)9 PartETag (com.aliyun.oss.model.PartETag)9 ArrayList (java.util.ArrayList)9 CompleteMultipartUploadResult (com.aliyun.oss.model.CompleteMultipartUploadResult)7 OSSObject (com.aliyun.oss.model.OSSObject)7 PartSummary (com.aliyun.oss.model.PartSummary)6 UploadPartCopyRequest (com.aliyun.oss.model.UploadPartCopyRequest)5 UploadPartCopyResult (com.aliyun.oss.model.UploadPartCopyResult)5 UploadPartRequest (com.aliyun.oss.model.UploadPartRequest)4 UploadPartResult (com.aliyun.oss.model.UploadPartResult)4 PutObjectResult (com.aliyun.oss.model.PutObjectResult)3 FileInputStream (java.io.FileInputStream)3 OSSClient (com.aliyun.oss.OSSClient)1 ResponseParseException (com.aliyun.oss.common.parser.ResponseParseException)1