Search in sources :

Example 11 with UploadPartRequest

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

the class ObjectAclTest method testUploadMultipartsWithAclHeader.

@Test
public void testUploadMultipartsWithAclHeader() {
    final String key = "normal-upload-multiparts-with-acl-header";
    // 128KB
    final int partSize = 128 * 1024;
    final int partCount = 10;
    try {
        // Initial multipart upload
        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());
        }
        // Complete multipart upload
        CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
        completeMultipartUploadRequest.setObjectACL(CannedAccessControlList.PublicRead);
        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());
        // Verify uploaded objects acl
        ObjectAcl returnedACL = ossClient.getObjectAcl(bucketName, key);
        Assert.assertEquals(ObjectPermission.PublicRead, returnedACL.getPermission());
        Assert.assertEquals(returnedACL.getRequestId().length(), REQUEST_ID_LEN);
        Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : ObjectAcl(com.aliyun.oss.model.ObjectAcl) OSSObject(com.aliyun.oss.model.OSSObject) ByteArrayInputStream(java.io.ByteArrayInputStream) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) 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) IOException(java.io.IOException) UploadPartResult(com.aliyun.oss.model.UploadPartResult) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Test(org.junit.Test)

Example 12 with UploadPartRequest

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

the class CRCChecksumTest method testMutilUploadCRC.

@Test
public void testMutilUploadCRC() {
    String key = "mutil-upload-object-crc";
    try {
        List<PartETag> partETags = new ArrayList<PartETag>();
        // 初始化上传任务
        InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(bucketName, key);
        InitiateMultipartUploadResult initiateMultipartUploadResult = ossClient.initiateMultipartUpload(initiateMultipartUploadRequest);
        Assert.assertEquals(initiateMultipartUploadResult.getRequestId().length(), REQUEST_ID_LEN);
        String uploadId = initiateMultipartUploadResult.getUploadId();
        // 上传分片
        for (int i = 0; i < 5; i++) {
            long fileLen = 1024 * 100 * (i + 1);
            String filePath = TestUtils.genFixedLengthFile(fileLen);
            UploadPartRequest request = new UploadPartRequest(bucketName, key, uploadId, i + 1, new FileInputStream(filePath), fileLen);
            UploadPartResult uploadPartResult = ossClient.uploadPart(request);
            Assert.assertEquals(uploadPartResult.getRequestId().length(), REQUEST_ID_LEN);
            partETags.add(uploadPartResult.getPartETag());
            checkCRC(uploadPartResult);
        }
        // 提交上传任务,服务器返回整个文件的CRC,客户端没有计算
        CompleteMultipartUploadRequest CompleteMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
        CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(CompleteMultipartUploadRequest);
        Assert.assertEquals(completeMultipartUploadResult.getRequestId().length(), REQUEST_ID_LEN);
        checkCRC(completeMultipartUploadResult);
        ossClient.deleteObject(bucketName, key);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : InitiateMultipartUploadResult(com.aliyun.oss.model.InitiateMultipartUploadResult) ArrayList(java.util.ArrayList) InitiateMultipartUploadRequest(com.aliyun.oss.model.InitiateMultipartUploadRequest) UploadPartRequest(com.aliyun.oss.model.UploadPartRequest) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) PartETag(com.aliyun.oss.model.PartETag) FileInputStream(java.io.FileInputStream) InconsistentException(com.aliyun.oss.InconsistentException) UploadPartResult(com.aliyun.oss.model.UploadPartResult) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Test(org.junit.Test)

Example 13 with UploadPartRequest

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

the class CallbackTest method testMultipartUploadCallbackBody.

/**
 * callback body has variables, type is url
 */
@Test
public void testMultipartUploadCallbackBody() {
    String key = "multipart-upload-callback-body";
    try {
        String uploadId = claimUploadId(ossClient, bucketName, key);
        InputStream instream = genFixedLengthInputStream(instreamLength);
        List<PartETag> partETags = new ArrayList<PartETag>();
        UploadPartRequest uploadPartRequest = new UploadPartRequest();
        uploadPartRequest.setBucketName(bucketName);
        uploadPartRequest.setKey(key);
        uploadPartRequest.setInputStream(instream);
        uploadPartRequest.setPartNumber(1);
        uploadPartRequest.setPartSize(instreamLength);
        uploadPartRequest.setUploadId(uploadId);
        UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
        partETags.add(uploadPartResult.getPartETag());
        Callback callback = new Callback();
        callback.setCallbackUrl(callbackUrl);
        callback.setCallbackHost("oss-cn-hangzhou.aliyuncs.com");
        callback.setCallbackBody("bucket=${bucket}&object=${object}&etag=${etag}&size=${size}&mimeType=${mimeType}&imageInfo.height=${imageInfo.height}&imageInfo.width=${imageInfo.width}&imageInfo.format=${imageInfo.format}&my_var=${x:my_var}");
        callback.setCalbackBodyType(CalbackBodyType.URL);
        CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
        completeMultipartUploadRequest.setCallback(callback);
        CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
        byte[] buffer = new byte[bufferLength];
        int nRead = completeMultipartUploadResult.getResponse().getContent().read(buffer);
        completeMultipartUploadResult.getResponse().getContent().close();
        Assert.assertEquals(callbackResponse, new String(buffer, 0, nRead));
        OSSObject obj = ossClient.getObject(bucketName, key);
        Assert.assertEquals(key, obj.getKey());
        Assert.assertEquals(instreamLength, obj.getObjectMetadata().getContentLength());
        obj.forcedClose();
    } catch (Exception ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) 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) UploadPartResult(com.aliyun.oss.model.UploadPartResult) Callback(com.aliyun.oss.model.Callback) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Test(org.junit.Test)

Example 14 with UploadPartRequest

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

the class UploadPartTest method testUnormalCompleteMultipartUpload.

@Test
public void testUnormalCompleteMultipartUpload() {
    final String key = "unormal-complete-multipart-upload-object";
    // 128KB
    final int partSize = 128 * 1024;
    final int partCount = 10;
    try {
        String uploadId = claimUploadId(ossClient, bucketName, key);
        // Set length of parts less than minimum limit(100KB)
        final int invalidPartSize = 64 * 1024;
        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(invalidPartSize);
            uploadPartRequest.setUploadId(uploadId);
            UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
            partETags.add(uploadPartResult.getPartETag());
        }
        // Try to complete multipart upload with all uploaded parts
        try {
            CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
            ossClient.completeMultipartUpload(completeMultipartUploadRequest);
        } 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
        AbortMultipartUploadRequest abortMultipartUploadRequest = new AbortMultipartUploadRequest(bucketName, key, uploadId);
        ossClient.abortMultipartUpload(abortMultipartUploadRequest);
        // Reclaim upload id
        uploadId = claimUploadId(ossClient, bucketName, key);
        // Upload parts again
        partETags.clear();
        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);
            partETags.add(uploadPartResult.getPartETag());
        }
        // Given part's ETag not match with actual ETag
        final String invalidETag = "Invalid-ETag";
        final int partNumber = 4;
        PartETag originalPartETag = partETags.get(partNumber - 1);
        PartETag invalidPartETag = new PartETag(partNumber, invalidETag);
        partETags.set(partNumber - 1, invalidPartETag);
        try {
            CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
            ossClient.completeMultipartUpload(completeMultipartUploadRequest);
            Assert.fail("Complete multipart upload should not be successful");
        } catch (OSSException e) {
            Assert.assertEquals(OSSErrorCode.INVALID_PART, e.getErrorCode());
            Assert.assertTrue(e.getMessage().startsWith(INVALID_PART_ERR));
        } finally {
            partETags.set(partNumber - 1, originalPartETag);
        }
        // Try to complete multipart upload with non-existent part
        PartETag nonexistentPartETag = new PartETag(partCount + 1, invalidETag);
        partETags.add(nonexistentPartETag);
        try {
            CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
            ossClient.completeMultipartUpload(completeMultipartUploadRequest);
            Assert.fail("Complete multipart upload should not be successful");
        } catch (OSSException e) {
            Assert.assertEquals(OSSErrorCode.INVALID_PART, e.getErrorCode());
            Assert.assertTrue(e.getMessage().startsWith(INVALID_PART_ERR));
        } finally {
            partETags.remove(partCount);
        }
        // Complete multipart upload with all uploaded parts
        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);
        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) OSSException(com.aliyun.oss.OSSException) AbortMultipartUploadRequest(com.aliyun.oss.model.AbortMultipartUploadRequest) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) PartETag(com.aliyun.oss.model.PartETag) OSSException(com.aliyun.oss.OSSException) UploadPartResult(com.aliyun.oss.model.UploadPartResult) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Test(org.junit.Test)

Example 15 with UploadPartRequest

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

UploadPartRequest (com.aliyun.oss.model.UploadPartRequest)22 UploadPartResult (com.aliyun.oss.model.UploadPartResult)20 ArrayList (java.util.ArrayList)20 Test (org.junit.Test)20 OSSException (com.aliyun.oss.OSSException)18 CompleteMultipartUploadRequest (com.aliyun.oss.model.CompleteMultipartUploadRequest)18 PartETag (com.aliyun.oss.model.PartETag)18 InputStream (java.io.InputStream)18 TestUtils.genFixedLengthInputStream (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream)17 CompleteMultipartUploadResult (com.aliyun.oss.model.CompleteMultipartUploadResult)17 OSSObject (com.aliyun.oss.model.OSSObject)14 FileInputStream (java.io.FileInputStream)13 AbortMultipartUploadRequest (com.aliyun.oss.model.AbortMultipartUploadRequest)6 Callback (com.aliyun.oss.model.Callback)6 InitiateMultipartUploadRequest (com.aliyun.oss.model.InitiateMultipartUploadRequest)5 InitiateMultipartUploadResult (com.aliyun.oss.model.InitiateMultipartUploadResult)5 ListMultipartUploadsRequest (com.aliyun.oss.model.ListMultipartUploadsRequest)4 ListPartsRequest (com.aliyun.oss.model.ListPartsRequest)4 MultipartUpload (com.aliyun.oss.model.MultipartUpload)4 MultipartUploadListing (com.aliyun.oss.model.MultipartUploadListing)4