Search in sources :

Example 6 with MultipartUpload

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

the class UploadPartTest method testNormalListMultipartUploads.

@Test
public void testNormalListMultipartUploads() {
    try {
        // Add LIST_UPLOAD_MAX_RETURNS + 1 + lv2KeyCount objects to bucket
        List<String> existingKeys = new ArrayList<String>();
        final int lv1KeyCount = 101;
        final int lv2KeyCount = 11;
        final int multipartUploadCount = LIST_UPLOAD_MAX_RETURNS + 1 + lv2KeyCount;
        final String lv0KeyPrefix = "normal-list-multiparts-lv0-objects-";
        final String lv1KeyPrefix = "normal-list-multiparts-lv0-objects/lv1-objects-";
        final String lv2KeyPrefix = "normal-list-multiparts-lv0-objects/lv1-objects/lv2-objects-";
        for (int i = 0; i <= LIST_UPLOAD_MAX_RETURNS; i++) {
            if (i % 10 != 0) {
                existingKeys.add(lv0KeyPrefix + i);
            } else {
                existingKeys.add(lv1KeyPrefix + i);
                if (i % 100 == 0) {
                    existingKeys.add(lv2KeyPrefix + i);
                }
            }
        }
        // Upload single part for each multipart upload
        // 128B
        final int partSize = 128;
        List<String> uploadIds = new ArrayList<String>(multipartUploadCount);
        for (int i = 0; i < multipartUploadCount; i++) {
            String key = existingKeys.get(i);
            String uploadId = claimUploadId(ossClient, bucketName, key);
            uploadIds.add(uploadId);
            InputStream instream = genFixedLengthInputStream(partSize);
            UploadPartRequest uploadPartRequest = new UploadPartRequest();
            uploadPartRequest.setBucketName(bucketName);
            uploadPartRequest.setKey(key);
            uploadPartRequest.setInputStream(instream);
            uploadPartRequest.setPartNumber(1);
            uploadPartRequest.setPartSize(partSize);
            uploadPartRequest.setUploadId(uploadId);
            UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
            Assert.assertEquals(1, uploadPartResult.getPartNumber());
            Assert.assertEquals(uploadPartResult.getRequestId().length(), REQUEST_ID_LEN);
        }
        // List multipart uploads without any conditions
        ListMultipartUploadsRequest listMultipartUploadsRequest = new ListMultipartUploadsRequest(bucketName);
        MultipartUploadListing multipartUploadListing = ossClient.listMultipartUploads(listMultipartUploadsRequest);
        Assert.assertEquals(bucketName, multipartUploadListing.getBucketName());
        Assert.assertEquals(LIST_UPLOAD_MAX_RETURNS, multipartUploadListing.getMaxUploads());
        Assert.assertTrue(multipartUploadListing.isTruncated());
        Assert.assertNotNull(multipartUploadListing.getNextKeyMarker());
        Assert.assertNotNull(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);
        List<MultipartUpload> multipartUploads = multipartUploadListing.getMultipartUploads();
        Assert.assertEquals(LIST_UPLOAD_MAX_RETURNS, multipartUploads.size());
        for (int i = 0; i < LIST_UPLOAD_MAX_RETURNS; i++) {
            Assert.assertTrue(existingKeys.contains(multipartUploads.get(i).getKey()));
            Assert.assertTrue(uploadIds.contains(multipartUploads.get(i).getUploadId()));
        }
        String keyMarker = multipartUploadListing.getNextKeyMarker();
        String uploadIdMarker = multipartUploadListing.getNextUploadIdMarker();
        listMultipartUploadsRequest.setKeyMarker(keyMarker);
        listMultipartUploadsRequest.setUploadIdMarker(uploadIdMarker);
        multipartUploadListing = ossClient.listMultipartUploads(listMultipartUploadsRequest);
        Assert.assertEquals(bucketName, multipartUploadListing.getBucketName());
        Assert.assertEquals(LIST_UPLOAD_MAX_RETURNS, multipartUploadListing.getMaxUploads());
        Assert.assertFalse(multipartUploadListing.isTruncated());
        Assert.assertNotNull(multipartUploadListing.getNextKeyMarker());
        Assert.assertNotNull(multipartUploadListing.getNextUploadIdMarker());
        Assert.assertNull(multipartUploadListing.getDelimiter());
        Assert.assertNull(multipartUploadListing.getPrefix());
        Assert.assertNull(multipartUploadListing.getKeyMarker());
        Assert.assertNull(multipartUploadListing.getUploadIdMarker());
        multipartUploads = multipartUploadListing.getMultipartUploads();
        Assert.assertEquals(multipartUploadListing.getRequestId().length(), REQUEST_ID_LEN);
        Assert.assertEquals(multipartUploadCount - LIST_UPLOAD_MAX_RETURNS, multipartUploads.size());
        for (int i = 0; i < (multipartUploadCount - LIST_UPLOAD_MAX_RETURNS); i++) {
            Assert.assertTrue(existingKeys.contains(multipartUploads.get(i).getKey()));
            Assert.assertTrue(uploadIds.contains(multipartUploads.get(i).getUploadId()));
        }
        // List 'max-uploads' multipart uploads with 'prefix'
        final int maxUploads = 100;
        listMultipartUploadsRequest = new ListMultipartUploadsRequest(bucketName);
        listMultipartUploadsRequest.setMaxUploads(maxUploads);
        listMultipartUploadsRequest.setPrefix(lv1KeyPrefix);
        multipartUploadListing = ossClient.listMultipartUploads(listMultipartUploadsRequest);
        Assert.assertEquals(bucketName, multipartUploadListing.getBucketName());
        Assert.assertEquals(maxUploads, multipartUploadListing.getMaxUploads());
        Assert.assertTrue(multipartUploadListing.isTruncated());
        Assert.assertNotNull(multipartUploadListing.getNextKeyMarker());
        Assert.assertNotNull(multipartUploadListing.getNextUploadIdMarker());
        Assert.assertNull(multipartUploadListing.getDelimiter());
        Assert.assertEquals(lv1KeyPrefix, multipartUploadListing.getPrefix());
        Assert.assertNull(multipartUploadListing.getKeyMarker());
        Assert.assertNull(multipartUploadListing.getUploadIdMarker());
        Assert.assertEquals(multipartUploadListing.getRequestId().length(), REQUEST_ID_LEN);
        multipartUploads = multipartUploadListing.getMultipartUploads();
        Assert.assertEquals(maxUploads, multipartUploads.size());
        for (int i = 0; i < maxUploads; i++) {
            Assert.assertTrue(existingKeys.contains(multipartUploads.get(i).getKey()));
            Assert.assertTrue(uploadIds.contains(multipartUploads.get(i).getUploadId()));
        }
        keyMarker = multipartUploadListing.getNextKeyMarker();
        uploadIdMarker = multipartUploadListing.getNextUploadIdMarker();
        listMultipartUploadsRequest.setKeyMarker(keyMarker);
        listMultipartUploadsRequest.setUploadIdMarker(uploadIdMarker);
        multipartUploadListing = ossClient.listMultipartUploads(listMultipartUploadsRequest);
        Assert.assertEquals(bucketName, multipartUploadListing.getBucketName());
        Assert.assertEquals(maxUploads, multipartUploadListing.getMaxUploads());
        Assert.assertFalse(multipartUploadListing.isTruncated());
        Assert.assertNotNull(multipartUploadListing.getNextKeyMarker());
        Assert.assertNotNull(multipartUploadListing.getNextUploadIdMarker());
        Assert.assertNull(multipartUploadListing.getDelimiter());
        Assert.assertEquals(lv1KeyPrefix, multipartUploadListing.getPrefix());
        Assert.assertNull(multipartUploadListing.getKeyMarker());
        Assert.assertNull(multipartUploadListing.getUploadIdMarker());
        Assert.assertEquals(multipartUploadListing.getRequestId().length(), REQUEST_ID_LEN);
        multipartUploads = multipartUploadListing.getMultipartUploads();
        Assert.assertEquals(lv1KeyCount - maxUploads, multipartUploads.size());
        for (int i = 0; i < (lv1KeyCount - maxUploads); i++) {
            Assert.assertTrue(existingKeys.contains(multipartUploads.get(i).getKey()));
            Assert.assertTrue(uploadIds.contains(multipartUploads.get(i).getUploadId()));
        }
        // List object with 'prefix' and 'delimiter'
        final String delimiter = "/";
        final String keyPrefix0 = "normal-list-multiparts-lv0-objects/";
        final String keyPrefix1 = "normal-list-multiparts-lv0-objects/lv1-objects/";
        listMultipartUploadsRequest = new ListMultipartUploadsRequest(bucketName);
        listMultipartUploadsRequest.setPrefix(keyPrefix0);
        listMultipartUploadsRequest.setDelimiter(delimiter);
        multipartUploadListing = ossClient.listMultipartUploads(listMultipartUploadsRequest);
        Assert.assertEquals(bucketName, multipartUploadListing.getBucketName());
        Assert.assertEquals(LIST_UPLOAD_MAX_RETURNS, multipartUploadListing.getMaxUploads());
        Assert.assertFalse(multipartUploadListing.isTruncated());
        Assert.assertNotNull(multipartUploadListing.getNextKeyMarker());
        Assert.assertNotNull(multipartUploadListing.getNextUploadIdMarker());
        Assert.assertEquals(delimiter, multipartUploadListing.getDelimiter());
        Assert.assertEquals(keyPrefix0, multipartUploadListing.getPrefix());
        Assert.assertNull(multipartUploadListing.getKeyMarker());
        Assert.assertNull(multipartUploadListing.getUploadIdMarker());
        Assert.assertEquals(multipartUploadListing.getRequestId().length(), REQUEST_ID_LEN);
        multipartUploads = multipartUploadListing.getMultipartUploads();
        Assert.assertEquals(lv1KeyCount, multipartUploads.size());
        for (int i = 0; i < lv1KeyCount; i++) {
            Assert.assertTrue(existingKeys.contains(multipartUploads.get(i).getKey()));
            Assert.assertTrue(uploadIds.contains(multipartUploads.get(i).getUploadId()));
        }
        Assert.assertEquals(1, multipartUploadListing.getCommonPrefixes().size());
        Assert.assertEquals(keyPrefix1, multipartUploadListing.getCommonPrefixes().get(0));
        // Abort all incompleted multipart uploads
        for (int i = 0; i < multipartUploadCount; i++) {
            AbortMultipartUploadRequest abortMultipartUploadRequest = new AbortMultipartUploadRequest(bucketName, existingKeys.get(i), uploadIds.get(i));
            ossClient.abortMultipartUpload(abortMultipartUploadRequest);
        }
        // List all incompleted 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_UPLOAD_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);
    } 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) MultipartUploadListing(com.aliyun.oss.model.MultipartUploadListing) AbortMultipartUploadRequest(com.aliyun.oss.model.AbortMultipartUploadRequest) MultipartUpload(com.aliyun.oss.model.MultipartUpload) OSSException(com.aliyun.oss.OSSException) ListMultipartUploadsRequest(com.aliyun.oss.model.ListMultipartUploadsRequest) UploadPartResult(com.aliyun.oss.model.UploadPartResult) Test(org.junit.Test)

Example 7 with MultipartUpload

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

the class TestBase method abortAllMultipartUploads.

protected static void abortAllMultipartUploads(OSSClient client, String bucketName) {
    if (!client.doesBucketExist(bucketName)) {
        return;
    }
    String keyMarker = null;
    String uploadIdMarker = null;
    ListMultipartUploadsRequest listMultipartUploadsRequest = null;
    MultipartUploadListing multipartUploadListing = null;
    List<MultipartUpload> multipartUploads = null;
    do {
        listMultipartUploadsRequest = new ListMultipartUploadsRequest(bucketName);
        listMultipartUploadsRequest.setKeyMarker(keyMarker);
        listMultipartUploadsRequest.setUploadIdMarker(uploadIdMarker);
        multipartUploadListing = client.listMultipartUploads(listMultipartUploadsRequest);
        multipartUploads = multipartUploadListing.getMultipartUploads();
        for (MultipartUpload mu : multipartUploads) {
            String key = mu.getKey();
            String uploadId = mu.getUploadId();
            client.abortMultipartUpload(new AbortMultipartUploadRequest(bucketName, key, uploadId));
        }
        keyMarker = multipartUploadListing.getKeyMarker();
        uploadIdMarker = multipartUploadListing.getUploadIdMarker();
    } while (multipartUploadListing != null && multipartUploadListing.isTruncated());
}
Also used : MultipartUploadListing(com.aliyun.oss.model.MultipartUploadListing) AbortMultipartUploadRequest(com.aliyun.oss.model.AbortMultipartUploadRequest) MultipartUpload(com.aliyun.oss.model.MultipartUpload) ListMultipartUploadsRequest(com.aliyun.oss.model.ListMultipartUploadsRequest)

Aggregations

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