Search in sources :

Example 1 with PartSummary

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

the class MultipartUploadSample method listAllParts.

private static void listAllParts(String uploadId) {
    System.out.println("Listing all parts......");
    ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, key, uploadId);
    PartListing partListing = client.listParts(listPartsRequest);
    int partCount = partListing.getParts().size();
    for (int i = 0; i < partCount; i++) {
        PartSummary partSummary = partListing.getParts().get(i);
        System.out.println("\tPart#" + partSummary.getPartNumber() + ", ETag=" + partSummary.getETag());
    }
    System.out.println();
}
Also used : ListPartsRequest(com.aliyun.oss.model.ListPartsRequest) PartListing(com.aliyun.oss.model.PartListing) PartSummary(com.aliyun.oss.model.PartSummary)

Example 2 with PartSummary

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

the class OSSResponseParserTest method testParseListParts.

@Test
public void testParseListParts() throws Exception {
    String filename = "listParts.xml";
    InputStream in = getInputStream(filename);
    PartListing partListing = ResponseParsers.parseListParts(in);
    Assert.assertEquals("dp7d8j2xfec1m984em9xmkgc_gan", partListing.getBucketName());
    Assert.assertEquals("test.rar", partListing.getKey());
    Assert.assertEquals("0004B98692BB2A28C897B642CFAC1DCE", partListing.getUploadId());
    Assert.assertEquals("Standard", partListing.getStorageClass());
    Assert.assertEquals(3, partListing.getNextPartNumberMarker().intValue());
    Assert.assertEquals(1000, partListing.getMaxParts().intValue());
    Assert.assertEquals(false, partListing.isTruncated());
    List<PartSummary> parts = partListing.getParts();
    Assert.assertNotNull(parts);
    Assert.assertTrue(parts.size() == 1);
    PartSummary part = parts.get(0);
    Assert.assertEquals(3, part.getPartNumber());
    Assert.assertEquals(DateUtil.parseIso8601Date("2012-02-22T05:12:29.000Z"), part.getLastModified());
    Assert.assertEquals("4B4BEAF5BC622FC89D29BF0E3B70B730", part.getETag());
    Assert.assertEquals(3996796L, part.getSize());
    in.close();
}
Also used : InputStream(java.io.InputStream) PartListing(com.aliyun.oss.model.PartListing) PartSummary(com.aliyun.oss.model.PartSummary) Test(org.junit.Test)

Example 3 with PartSummary

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

the class UploadPartTest method testNormalUploadMultiparts.

@Test
public void testNormalUploadMultiparts() {
    final String key = "normal-upload-multiparts-object";
    // 128KB
    final int partSize = 128 * 1024;
    final int partCount = 10;
    try {
        String uploadId = claimUploadId(ossClient, bucketName, key);
        // 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
        ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, key, uploadId);
        PartListing 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, partListing.getKey());
        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);
        // List single multipart upload under this bucket
        ListMultipartUploadsRequest listMultipartUploadsRequest = new ListMultipartUploadsRequest(bucketName);
        MultipartUploadListing multipartUploadListing = ossClient.listMultipartUploads(listMultipartUploadsRequest);
        Assert.assertEquals(bucketName, multipartUploadListing.getBucketName());
        Assert.assertEquals(LIST_PART_MAX_RETURNS, multipartUploadListing.getMaxUploads());
        Assert.assertFalse(multipartUploadListing.isTruncated());
        Assert.assertEquals(key, multipartUploadListing.getNextKeyMarker());
        Assert.assertEquals(uploadId, multipartUploadListing.getNextUploadIdMarker());
        Assert.assertNull(multipartUploadListing.getDelimiter());
        Assert.assertNull(multipartUploadListing.getPrefix());
        Assert.assertNull(multipartUploadListing.getKeyMarker());
        Assert.assertNull(multipartUploadListing.getUploadIdMarker());
        List<MultipartUpload> multipartUploads = multipartUploadListing.getMultipartUploads();
        Assert.assertEquals(1, multipartUploads.size());
        Assert.assertEquals(key, multipartUploads.get(0).getKey());
        Assert.assertEquals(uploadId, multipartUploads.get(0).getUploadId());
        Assert.assertEquals(multipartUploadListing.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);
        // List single multipart uploads under this bucket again
        listMultipartUploadsRequest = new ListMultipartUploadsRequest(bucketName);
        multipartUploadListing = ossClient.listMultipartUploads(listMultipartUploadsRequest);
        Assert.assertEquals(0, multipartUploadListing.getMultipartUploads().size());
        Assert.assertEquals(bucketName, multipartUploadListing.getBucketName());
        Assert.assertEquals(LIST_PART_MAX_RETURNS, multipartUploadListing.getMaxUploads());
        Assert.assertFalse(multipartUploadListing.isTruncated());
        Assert.assertNull(multipartUploadListing.getNextKeyMarker());
        Assert.assertNull(multipartUploadListing.getNextUploadIdMarker());
        Assert.assertNull(multipartUploadListing.getDelimiter());
        Assert.assertNull(multipartUploadListing.getPrefix());
        Assert.assertNull(multipartUploadListing.getKeyMarker());
        Assert.assertNull(multipartUploadListing.getUploadIdMarker());
        Assert.assertEquals(multipartUploadListing.getRequestId().length(), REQUEST_ID_LEN);
        // Get uploaded object
        OSSObject o = ossClient.getObject(bucketName, key);
        final long objectSize = partCount * partSize;
        Assert.assertEquals(objectSize, o.getObjectMetadata().getContentLength());
        Assert.assertEquals(calcMultipartsETag(partETags), o.getObjectMetadata().getETag());
        Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) 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) MultipartUploadListing(com.aliyun.oss.model.MultipartUploadListing) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) MultipartUpload(com.aliyun.oss.model.MultipartUpload) PartETag(com.aliyun.oss.model.PartETag) OSSException(com.aliyun.oss.OSSException) PartListing(com.aliyun.oss.model.PartListing) ListMultipartUploadsRequest(com.aliyun.oss.model.ListMultipartUploadsRequest) 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 4 with PartSummary

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

the class UploadPartTest method testNormalListParts.

@Test
public void testNormalListParts() {
    final String key = "normal-list-parts-object";
    // 128KB
    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 without any special conditions
        listPartsRequest = new ListPartsRequest(bucketName, key, uploadId);
        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, partListing.getKey());
        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);
        // List 'max-parts' parts each time
        final int maxParts = 15;
        listPartsRequest = new ListPartsRequest(bucketName, key, uploadId);
        listPartsRequest.setMaxParts(maxParts);
        partListing = ossClient.listParts(listPartsRequest);
        Assert.assertEquals(maxParts, partListing.getParts().size());
        for (int i = 0; i < maxParts; 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(partSize, ps.getSize());
        }
        Assert.assertEquals(bucketName, partListing.getBucketName());
        Assert.assertEquals(key, partListing.getKey());
        Assert.assertEquals(uploadId, partListing.getUploadId());
        Assert.assertEquals(maxParts, partListing.getMaxParts().intValue());
        Assert.assertEquals(maxParts, partListing.getNextPartNumberMarker().intValue());
        Assert.assertTrue(partListing.isTruncated());
        Assert.assertEquals(partListing.getRequestId().length(), REQUEST_ID_LEN);
        // List 'max-parts' parts with 'part-number-marker'
        final int partNumberMarker = 20;
        listPartsRequest.setPartNumberMarker(partNumberMarker);
        partListing = ossClient.listParts(listPartsRequest);
        Assert.assertEquals(partCount - partNumberMarker, partListing.getParts().size());
        for (int i = 0; i < (partCount - partNumberMarker); i++) {
            PartSummary ps = partListing.getParts().get(i);
            PartETag eTag = partETags.get(partNumberMarker + i);
            Assert.assertEquals(eTag.getPartNumber(), ps.getPartNumber());
            Assert.assertEquals(eTag.getETag(), ps.getETag());
            Assert.assertEquals(partSize, ps.getSize());
        }
        Assert.assertEquals(bucketName, partListing.getBucketName());
        Assert.assertEquals(key, partListing.getKey());
        Assert.assertEquals(uploadId, partListing.getUploadId());
        Assert.assertEquals(maxParts, 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);
        // Get uploaded object
        OSSObject o = ossClient.getObject(bucketName, key);
        final long objectSize = partCount * partSize;
        Assert.assertEquals(objectSize, o.getObjectMetadata().getContentLength());
        Assert.assertEquals(calcMultipartsETag(partETags), o.getObjectMetadata().getETag());
        Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) 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 5 with PartSummary

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

Aggregations

PartListing (com.aliyun.oss.model.PartListing)6 PartSummary (com.aliyun.oss.model.PartSummary)6 ListPartsRequest (com.aliyun.oss.model.ListPartsRequest)4 InputStream (java.io.InputStream)4 Test (org.junit.Test)4 OSSException (com.aliyun.oss.OSSException)3 TestUtils.genFixedLengthInputStream (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream)3 CompleteMultipartUploadRequest (com.aliyun.oss.model.CompleteMultipartUploadRequest)3 CompleteMultipartUploadResult (com.aliyun.oss.model.CompleteMultipartUploadResult)3 PartETag (com.aliyun.oss.model.PartETag)3 UploadPartRequest (com.aliyun.oss.model.UploadPartRequest)3 UploadPartResult (com.aliyun.oss.model.UploadPartResult)3 FileInputStream (java.io.FileInputStream)3 ArrayList (java.util.ArrayList)3 OSSObject (com.aliyun.oss.model.OSSObject)2 ResponseParseException (com.aliyun.oss.common.parser.ResponseParseException)1 ListMultipartUploadsRequest (com.aliyun.oss.model.ListMultipartUploadsRequest)1 MultipartUpload (com.aliyun.oss.model.MultipartUpload)1 MultipartUploadListing (com.aliyun.oss.model.MultipartUploadListing)1 ParseException (java.text.ParseException)1