Search in sources :

Example 1 with ListMultipartUploadsRequest

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

the class OSSClientRequestTest method testListMutilpartUploadsRequest.

@Test
public void testListMutilpartUploadsRequest() throws Exception {
    final ListMultipartUploadsRequest request = new ListMultipartUploadsRequest(bucketName);
    TestAction test1 = new TestAction() {

        public void run() throws Exception {
            multipartOp.listMultipartUploads(request);
        }
    };
    executeTest(test1, HttpMethod.GET, bucketName + "." + endpoint.getHost(), "?uploads", null);
    request.setMaxUploads(100);
    request.setDelimiter(";");
    request.setKeyMarker("KEYMARKER1");
    request.setPrefix("PREFIX-");
    request.setUploadIdMarker("UPLOAD123");
    String expectedPath = "?uploads" + "&delimiter=" + HttpUtil.urlEncode(request.getDelimiter(), OSSConstants.DEFAULT_CHARSET_NAME) + "&key-marker=" + request.getKeyMarker() + "&max-uploads=" + request.getMaxUploads().toString() + "&prefix=" + request.getPrefix() + "&upload-id-marker=" + request.getUploadIdMarker();
    executeTest(test1, HttpMethod.GET, bucketName + "." + endpoint.getHost(), expectedPath, null);
}
Also used : ListMultipartUploadsRequest(com.aliyun.oss.model.ListMultipartUploadsRequest) Test(org.junit.Test)

Example 2 with ListMultipartUploadsRequest

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

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

the class UploadPartTest method testNormalListMultipartUploadsWithEncoding.

@Test
public void testNormalListMultipartUploadsWithEncoding() {
    try {
        // Add LIST_UPLOAD_MAX_RETURNS + 1 + lv2KeyCount objects to bucket
        List<String> existingKeys = new ArrayList<String>();
        final int lv2KeyCount = 11;
        final int multipartUploadCount = LIST_UPLOAD_MAX_RETURNS + 1 + lv2KeyCount;
        final String lv0KeyPrefix = "常记溪亭日暮,沉醉不知归路。";
        final String lv1KeyPrefix = "常记溪亭日暮,沉醉不知归路。/昨夜雨疏风骤,浓睡不消残酒。-";
        final String lv2KeyPrefix = "常记溪亭日暮,沉醉不知归路。/昨夜雨疏风骤,浓睡不消残酒。/湖上风来波浩渺,秋已暮、红稀香少。-";
        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);
        listMultipartUploadsRequest.setEncodingType(DEFAULT_ENCODING_TYPE);
        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(URLDecoder.decode(multipartUploads.get(i).getKey(), "UTF-8")));
            Assert.assertTrue(uploadIds.contains(multipartUploads.get(i).getUploadId()));
        }
        // 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);
        }
    } 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 4 with ListMultipartUploadsRequest

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

the class UploadPartTest method testNormalUploadSinglePart.

@Test
public void testNormalUploadSinglePart() {
    final String key = "normal-upload-single-part-object";
    // 128KB
    final int partSize = 128 * 1024;
    try {
        String uploadId = claimUploadId(ossClient, bucketName, key);
        InputStream instream = genFixedLengthInputStream(partSize);
        // Upload single part
        UploadPartRequest uploadPartRequest = new UploadPartRequest();
        uploadPartRequest.setBucketName(bucketName);
        uploadPartRequest.setKey(key);
        uploadPartRequest.setInputStream(instream);
        uploadPartRequest.setPartNumber(2);
        uploadPartRequest.setPartSize(partSize);
        uploadPartRequest.setUploadId(uploadId);
        ossClient.uploadPart(uploadPartRequest);
        // 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);
        // Abort multipart upload
        AbortMultipartUploadRequest abortMultipartUploadRequest = new AbortMultipartUploadRequest(bucketName, key, uploadId);
        ossClient.abortMultipartUpload(abortMultipartUploadRequest);
        // List single multipart upload 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);
    } 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) 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) Test(org.junit.Test)

Example 5 with ListMultipartUploadsRequest

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

the class UploadPartTest method testDeleteAllBuckets.

@Ignore
public void testDeleteAllBuckets() {
    try {
        List<Bucket> returnedBuckets = ossClient.listBuckets();
        for (Bucket bkt : returnedBuckets) {
            String bktName = bkt.getName();
            String keyMarker = null;
            String uploadIdMarker = null;
            ListMultipartUploadsRequest listMultipartUploadsRequest = null;
            MultipartUploadListing multipartUploadListing = null;
            List<MultipartUpload> multipartUploads = null;
            do {
                listMultipartUploadsRequest = new ListMultipartUploadsRequest(bktName);
                listMultipartUploadsRequest.setKeyMarker(keyMarker);
                listMultipartUploadsRequest.setUploadIdMarker(uploadIdMarker);
                multipartUploadListing = ossClient.listMultipartUploads(listMultipartUploadsRequest);
                multipartUploads = multipartUploadListing.getMultipartUploads();
                for (MultipartUpload mu : multipartUploads) {
                    String key = mu.getKey();
                    String uploadId = mu.getUploadId();
                    ossClient.abortMultipartUpload(new AbortMultipartUploadRequest(bktName, key, uploadId));
                }
                keyMarker = multipartUploadListing.getKeyMarker();
                uploadIdMarker = multipartUploadListing.getUploadIdMarker();
            } while (multipartUploadListing != null && multipartUploadListing.isTruncated());
            deleteBucketWithObjects(ossClient, bktName);
        }
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : Bucket(com.aliyun.oss.model.Bucket) 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) Ignore(org.junit.Ignore)

Aggregations

ListMultipartUploadsRequest (com.aliyun.oss.model.ListMultipartUploadsRequest)8 OSSException (com.aliyun.oss.OSSException)6 AbortMultipartUploadRequest (com.aliyun.oss.model.AbortMultipartUploadRequest)6 MultipartUpload (com.aliyun.oss.model.MultipartUpload)6 MultipartUploadListing (com.aliyun.oss.model.MultipartUploadListing)6 Test (org.junit.Test)6 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 UploadPartResult (com.aliyun.oss.model.UploadPartResult)3 ArrayList (java.util.ArrayList)3 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 PartSummary (com.aliyun.oss.model.PartSummary)1