Search in sources :

Example 6 with UploadPartCopyRequest

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

the class UploadPartCopyTest method testUnormalUploadPartCopy.

@Test
public void testUnormalUploadPartCopy() {
    final String sourceBucket = "unormal-upload-part-copy-bucket-source";
    final String targetBucket = "unormal-upload-part-copy-bucket-target";
    final String sourceKey = "unormal-upload-part-copy-object-source";
    final String targetKey = "unormal-upload-part-copy-object-target";
    // Set length of parts less than minimum limit(100KB)
    // 64KB
    final long partSize = 64 * 1024;
    try {
        ossClient.createBucket(sourceBucket);
        ossClient.createBucket(targetBucket);
        waitForCacheExpiration(5);
        // Put object into source bucket
        String eTag = null;
        try {
            InputStream instream = genFixedLengthInputStream(partSize);
            PutObjectResult result = ossClient.putObject(sourceBucket, sourceKey, instream, null);
            eTag = result.getETag();
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
        // Claim upload id for target bucket
        String uploadId = claimUploadId(ossClient, targetBucket, targetKey);
        // Copy part under non-existent source bucket
        final String nonexistentSourceBucket = "nonexistent-source-bucket";
        final int partNumber = 1;
        try {
            UploadPartCopyRequest uploadPartCopyRequest = new UploadPartCopyRequest(nonexistentSourceBucket, sourceKey, targetBucket, targetKey);
            uploadPartCopyRequest.setPartNumber(partNumber);
            uploadPartCopyRequest.setUploadId(uploadId);
            ossClient.uploadPartCopy(uploadPartCopyRequest);
            Assert.fail("Upload part copy should not be successfuly");
        } catch (OSSException e) {
            Assert.assertEquals(OSSErrorCode.NO_SUCH_BUCKET, e.getErrorCode());
            Assert.assertTrue(e.getMessage().startsWith(NO_SUCH_BUCKET_ERR));
        }
        // Copy part from non-existent source key
        final String nonexistentSourceKey = "nonexistent-source-key";
        try {
            UploadPartCopyRequest uploadPartCopyRequest = new UploadPartCopyRequest(sourceBucket, nonexistentSourceKey, targetBucket, targetKey);
            uploadPartCopyRequest.setPartNumber(partNumber);
            uploadPartCopyRequest.setUploadId(uploadId);
            ossClient.uploadPartCopy(uploadPartCopyRequest);
            Assert.fail("Upload part copy should not be successfuly");
        } catch (OSSException e) {
            Assert.assertEquals(OSSErrorCode.NO_SUCH_KEY, e.getErrorCode());
            Assert.assertTrue(e.getMessage().startsWith(NO_SUCH_KEY_ERR));
        }
        // Copy part to non-existent target bucket
        final String nonexistentTargetBucket = "nonexistent-target-key";
        try {
            UploadPartCopyRequest uploadPartCopyRequest = new UploadPartCopyRequest(sourceBucket, sourceKey, nonexistentTargetBucket, targetKey);
            uploadPartCopyRequest.setPartNumber(partNumber);
            uploadPartCopyRequest.setUploadId(uploadId);
            ossClient.uploadPartCopy(uploadPartCopyRequest);
            Assert.fail("Upload part copy should not be successfuly");
        } catch (OSSException e) {
            Assert.assertEquals(OSSErrorCode.NO_SUCH_BUCKET, e.getErrorCode());
            Assert.assertTrue(e.getMessage().startsWith(NO_SUCH_BUCKET_ERR));
        }
        // Upload part copy
        List<PartETag> partETags = new ArrayList<PartETag>();
        try {
            UploadPartCopyRequest uploadPartCopyRequest = new UploadPartCopyRequest(sourceBucket, sourceKey, targetBucket, targetKey);
            uploadPartCopyRequest.setPartNumber(partNumber);
            uploadPartCopyRequest.setUploadId(uploadId);
            UploadPartCopyResult uploadPartCopyResult = ossClient.uploadPartCopy(uploadPartCopyRequest);
            partETags.add(uploadPartCopyResult.getPartETag());
            Assert.assertEquals(eTag, uploadPartCopyResult.getETag());
            Assert.assertEquals(partNumber, uploadPartCopyResult.getPartNumber());
            uploadPartCopyRequest = new UploadPartCopyRequest(sourceBucket, sourceKey, targetBucket, targetKey);
            uploadPartCopyRequest.setPartNumber(partNumber + 1);
            uploadPartCopyRequest.setUploadId(uploadId);
            uploadPartCopyResult = ossClient.uploadPartCopy(uploadPartCopyRequest);
            partETags.add(uploadPartCopyResult.getPartETag());
            Assert.assertEquals(eTag, uploadPartCopyResult.getETag());
            Assert.assertEquals(partNumber + 1, uploadPartCopyResult.getPartNumber());
            ListPartsRequest listPartsRequest = new ListPartsRequest(targetBucket, targetKey, uploadId);
            PartListing partListing = ossClient.listParts(listPartsRequest);
            Assert.assertEquals(2, partListing.getParts().size());
            Assert.assertEquals(targetBucket, partListing.getBucketName());
            Assert.assertEquals(targetKey, partListing.getKey());
            Assert.assertEquals(uploadId, partListing.getUploadId());
            Assert.assertEquals(LIST_PART_MAX_RETURNS, partListing.getMaxParts().intValue());
            Assert.assertNotNull(partListing.getNextPartNumberMarker());
            Assert.assertFalse(partListing.isTruncated());
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
        // Try to complete multipart upload with all uploaded parts
        try {
            CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(targetBucket, targetKey, uploadId, partETags);
            ossClient.completeMultipartUpload(completeMultipartUploadRequest);
            Assert.fail("Upload part copy should not be successfuly");
        } catch (OSSException e) {
            Assert.assertEquals(OSSErrorCode.ENTITY_TOO_SMALL, e.getErrorCode());
            Assert.assertTrue(e.getMessage().startsWith(ENTITY_TOO_SMALL_ERR));
        }
        // Abort the incompleted multipart upload
        try {
            AbortMultipartUploadRequest abortMultipartUploadRequest = new AbortMultipartUploadRequest(targetBucket, targetKey, uploadId);
            ossClient.abortMultipartUpload(abortMultipartUploadRequest);
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        deleteBucketWithObjects(ossClient, sourceBucket);
        deleteBucketWithObjects(ossClient, targetBucket);
    }
}
Also used : PutObjectResult(com.aliyun.oss.model.PutObjectResult) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) OSSException(com.aliyun.oss.OSSException) AbortMultipartUploadRequest(com.aliyun.oss.model.AbortMultipartUploadRequest) OSSException(com.aliyun.oss.OSSException) PartETag(com.aliyun.oss.model.PartETag) PartListing(com.aliyun.oss.model.PartListing) ListPartsRequest(com.aliyun.oss.model.ListPartsRequest) UploadPartCopyResult(com.aliyun.oss.model.UploadPartCopyResult) UploadPartCopyRequest(com.aliyun.oss.model.UploadPartCopyRequest) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Test(org.junit.Test)

Example 7 with UploadPartCopyRequest

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

the class UploadPartCopyTest method testNormalUploadPartCopyByRange.

@Test
public void testNormalUploadPartCopyByRange() {
    final String sourceBucket = "normal-upload-part-copy-by-range-bucket-source";
    final String targetBucket = "normal-upload-part-copy-by-range-bucket-target";
    final String sourceKey = "normal-upload-part-copy-by-range-object-source";
    final String targetKey = "normal-upload-part-copy-by-range-object-target";
    // 128KB
    final long partSize = 128 * 1024;
    try {
        ossClient.createBucket(sourceBucket);
        ossClient.createBucket(targetBucket);
        waitForCacheExpiration(5);
        // Put object into source bucket
        final long inputStreamLength = partSize * 4;
        try {
            InputStream instream = genFixedLengthInputStream(inputStreamLength);
            ossClient.putObject(sourceBucket, sourceKey, instream, null);
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
        // Claim upload id for target bucket
        String uploadId = claimUploadId(ossClient, targetBucket, targetKey);
        // Upload part copy
        final int partNumber = 1;
        final long beginIndex = partSize;
        List<PartETag> partETags = new ArrayList<PartETag>();
        UploadPartCopyRequest uploadPartCopyRequest = new UploadPartCopyRequest(sourceBucket, sourceKey, targetBucket, targetKey);
        uploadPartCopyRequest.setPartNumber(partNumber);
        uploadPartCopyRequest.setUploadId(uploadId);
        uploadPartCopyRequest.setBeginIndex(beginIndex);
        uploadPartCopyRequest.setPartSize(partSize);
        UploadPartCopyResult uploadPartCopyResult = ossClient.uploadPartCopy(uploadPartCopyRequest);
        partETags.add(uploadPartCopyResult.getPartETag());
        Assert.assertEquals(partNumber, uploadPartCopyResult.getPartNumber());
        Assert.assertEquals(uploadPartCopyResult.getRequestId().length(), REQUEST_ID_LEN);
        ListPartsRequest listPartsRequest = new ListPartsRequest(targetBucket, targetKey, uploadId);
        PartListing partListing = ossClient.listParts(listPartsRequest);
        Assert.assertEquals(1, partListing.getParts().size());
        Assert.assertEquals(targetBucket, partListing.getBucketName());
        Assert.assertEquals(targetKey, partListing.getKey());
        Assert.assertEquals(uploadId, partListing.getUploadId());
        Assert.assertEquals(LIST_PART_MAX_RETURNS, partListing.getMaxParts().intValue());
        Assert.assertNotNull(partListing.getNextPartNumberMarker());
        Assert.assertFalse(partListing.isTruncated());
        Assert.assertEquals(partListing.getRequestId().length(), REQUEST_ID_LEN);
        // Complete multipart upload
        CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(targetBucket, targetKey, uploadId, partETags);
        CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
        Assert.assertEquals(composeLocation(ossClient, OSS_TEST_ENDPOINT, targetBucket, targetKey), completeMultipartUploadResult.getLocation());
        Assert.assertEquals(targetBucket, completeMultipartUploadResult.getBucketName());
        Assert.assertEquals(targetKey, completeMultipartUploadResult.getKey());
        Assert.assertEquals(calcMultipartsETag(partETags), completeMultipartUploadResult.getETag());
        Assert.assertEquals(completeMultipartUploadResult.getRequestId().length(), REQUEST_ID_LEN);
        // Get uploaded object
        OSSObject o = ossClient.getObject(targetBucket, targetKey);
        final long objectSize = 1 * 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());
    } finally {
        deleteBucketWithObjects(ossClient, sourceBucket);
        deleteBucketWithObjects(ossClient, targetBucket);
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) OSSException(com.aliyun.oss.OSSException) PartETag(com.aliyun.oss.model.PartETag) PartListing(com.aliyun.oss.model.PartListing) ListPartsRequest(com.aliyun.oss.model.ListPartsRequest) UploadPartCopyResult(com.aliyun.oss.model.UploadPartCopyResult) UploadPartCopyRequest(com.aliyun.oss.model.UploadPartCopyRequest) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Test(org.junit.Test)

Example 8 with UploadPartCopyRequest

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

the class UploadPartCopyTest method testNormalUploadPartCopyWithSpecialChars.

@Test
public void testNormalUploadPartCopyWithSpecialChars() {
    final String sourceBucket = "normal-upload-part-copy-by-range-bucket-source";
    final String targetBucket = "normal-upload-part-copy-by-range-bucket-target";
    final String sourceKey = "测\\r试-中.~,+\"'*&¥#@%!(文)+字符|?/.zip";
    final String targetKey = "测\\r试-中.~,+\"'*&¥#@%!(文)+字符|?-2.zip";
    // 128KB
    final long partSize = 128 * 1024;
    try {
        ossClient.createBucket(sourceBucket);
        ossClient.createBucket(targetBucket);
        // Put object into source bucket
        final long inputStreamLength = partSize * 4;
        try {
            InputStream instream = genFixedLengthInputStream(inputStreamLength);
            ossClient.putObject(sourceBucket, sourceKey, instream, null);
        } catch (Exception e) {
            Assert.fail(e.getMessage());
        }
        // Claim upload id for target bucket
        String uploadId = claimUploadId(ossClient, targetBucket, targetKey);
        // Upload part copy
        final int partNumber = 1;
        final long beginIndex = partSize;
        List<PartETag> partETags = new ArrayList<PartETag>();
        UploadPartCopyRequest uploadPartCopyRequest = new UploadPartCopyRequest(sourceBucket, sourceKey, targetBucket, targetKey);
        uploadPartCopyRequest.setPartNumber(partNumber);
        uploadPartCopyRequest.setUploadId(uploadId);
        uploadPartCopyRequest.setBeginIndex(beginIndex);
        uploadPartCopyRequest.setPartSize(partSize);
        UploadPartCopyResult uploadPartCopyResult = ossClient.uploadPartCopy(uploadPartCopyRequest);
        partETags.add(uploadPartCopyResult.getPartETag());
        Assert.assertEquals(partNumber, uploadPartCopyResult.getPartNumber());
        Assert.assertEquals(uploadPartCopyResult.getRequestId().length(), REQUEST_ID_LEN);
        ListPartsRequest listPartsRequest = new ListPartsRequest(targetBucket, targetKey, uploadId);
        PartListing partListing = ossClient.listParts(listPartsRequest);
        Assert.assertEquals(1, partListing.getParts().size());
        Assert.assertEquals(targetBucket, partListing.getBucketName());
        Assert.assertEquals(targetKey, partListing.getKey());
        Assert.assertEquals(uploadId, partListing.getUploadId());
        Assert.assertEquals(LIST_PART_MAX_RETURNS, partListing.getMaxParts().intValue());
        Assert.assertNotNull(partListing.getNextPartNumberMarker());
        Assert.assertFalse(partListing.isTruncated());
        Assert.assertEquals(partListing.getRequestId().length(), REQUEST_ID_LEN);
        // Complete multipart upload
        CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(targetBucket, targetKey, uploadId, partETags);
        CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
        Assert.assertEquals(composeLocation(ossClient, OSS_TEST_ENDPOINT, targetBucket, targetKey), completeMultipartUploadResult.getLocation());
        Assert.assertEquals(targetBucket, completeMultipartUploadResult.getBucketName());
        Assert.assertEquals(targetKey, completeMultipartUploadResult.getKey());
        Assert.assertEquals(calcMultipartsETag(partETags), completeMultipartUploadResult.getETag());
        Assert.assertEquals(completeMultipartUploadResult.getRequestId().length(), REQUEST_ID_LEN);
        // Get uploaded object
        OSSObject o = ossClient.getObject(targetBucket, targetKey);
        final long objectSize = 1 * 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());
    } finally {
        deleteBucketWithObjects(ossClient, sourceBucket);
        deleteBucketWithObjects(ossClient, targetBucket);
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) OSSException(com.aliyun.oss.OSSException) PartETag(com.aliyun.oss.model.PartETag) PartListing(com.aliyun.oss.model.PartListing) ListPartsRequest(com.aliyun.oss.model.ListPartsRequest) UploadPartCopyResult(com.aliyun.oss.model.UploadPartCopyResult) UploadPartCopyRequest(com.aliyun.oss.model.UploadPartCopyRequest) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Test(org.junit.Test)

Aggregations

CompleteMultipartUploadRequest (com.aliyun.oss.model.CompleteMultipartUploadRequest)8 PartETag (com.aliyun.oss.model.PartETag)8 UploadPartCopyRequest (com.aliyun.oss.model.UploadPartCopyRequest)8 UploadPartCopyResult (com.aliyun.oss.model.UploadPartCopyResult)8 ArrayList (java.util.ArrayList)8 OSSException (com.aliyun.oss.OSSException)7 CompleteMultipartUploadResult (com.aliyun.oss.model.CompleteMultipartUploadResult)6 Test (org.junit.Test)6 TestUtils.genFixedLengthInputStream (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream)5 ListPartsRequest (com.aliyun.oss.model.ListPartsRequest)5 OSSObject (com.aliyun.oss.model.OSSObject)5 PartListing (com.aliyun.oss.model.PartListing)5 InputStream (java.io.InputStream)5 PutObjectResult (com.aliyun.oss.model.PutObjectResult)4 AbortMultipartUploadRequest (com.aliyun.oss.model.AbortMultipartUploadRequest)3 InitiateMultipartUploadRequest (com.aliyun.oss.model.InitiateMultipartUploadRequest)3 InitiateMultipartUploadResult (com.aliyun.oss.model.InitiateMultipartUploadResult)3 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)3 ClientException (com.aliyun.oss.ClientException)2 GetObjectRequest (com.aliyun.oss.model.GetObjectRequest)2