Search in sources :

Example 6 with UploadPartRequest

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

the class UploadPartTest method testUploadPartWithChunked.

@Test
public void testUploadPartWithChunked() {
    final String key = "upload-part-with-chunked-object";
    // 128KB
    final int partSize = 128 * 1024;
    final int partCount = 10;
    String uploadId = null;
    String filePath = null;
    {
        try {
            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.setUseChunkEncoding(true);
                uploadPartRequest.setUploadId(uploadId);
                UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
                Assert.assertEquals(uploadPartResult.getRequestId().length(), REQUEST_ID_LEN);
                partETags.add(uploadPartResult.getPartETag());
            }
            // 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());
        }
    }
    {
        try {
            uploadId = claimUploadId(ossClient, bucketName, key);
            filePath = genFixedLengthFile(partSize * partCount);
            // Upload parts
            List<PartETag> partETags = new ArrayList<PartETag>();
            for (int i = 0; i < partCount; i++) {
                InputStream instream = new FileInputStream(new File(filePath));
                instream.skip(i * partSize);
                UploadPartRequest uploadPartRequest = new UploadPartRequest();
                uploadPartRequest.setBucketName(bucketName);
                uploadPartRequest.setKey(key);
                uploadPartRequest.setInputStream(instream);
                uploadPartRequest.setPartNumber(i + 1);
                uploadPartRequest.setPartSize(partSize);
                uploadPartRequest.setUseChunkEncoding(true);
                uploadPartRequest.setUploadId(uploadId);
                UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
                Assert.assertEquals(uploadPartResult.getRequestId().length(), REQUEST_ID_LEN);
                partETags.add(uploadPartResult.getPartETag());
            }
            // 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());
        } finally {
            removeFile(filePath);
        }
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) UploadPartRequest(com.aliyun.oss.model.UploadPartRequest) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) PartETag(com.aliyun.oss.model.PartETag) OSSException(com.aliyun.oss.OSSException) FileInputStream(java.io.FileInputStream) UploadPartResult(com.aliyun.oss.model.UploadPartResult) ArrayList(java.util.ArrayList) List(java.util.List) TestUtils.genFixedLengthFile(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthFile) TestUtils.removeFile(com.aliyun.oss.integrationtests.TestUtils.removeFile) File(java.io.File) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Test(org.junit.Test)

Example 7 with UploadPartRequest

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

the class UploadPartTest method testNormalCompleteMultipartUpload.

@Test
public void testNormalCompleteMultipartUpload() {
    final String key = "normal-complete-multipart-upload-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());
        }
        // 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);
        // 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);
            Assert.assertEquals(uploadPartResult.getRequestId().length(), REQUEST_ID_LEN);
            partETags.add(uploadPartResult.getPartETag());
        }
        // Complete multipart upload with some discontinuous parts
        List<PartETag> discontinuousPartETags = new ArrayList<PartETag>();
        discontinuousPartETags.add(partETags.get(0));
        discontinuousPartETags.add(partETags.get(4));
        discontinuousPartETags.add(partETags.get(7));
        completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, discontinuousPartETags);
        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(discontinuousPartETags), completeMultipartUploadResult.getETag());
        Assert.assertEquals(completeMultipartUploadResult.getRequestId().length(), REQUEST_ID_LEN);
        // Get uploaded object again
        o = ossClient.getObject(bucketName, key);
        objectSize = discontinuousPartETags.size() * partSize;
        Assert.assertEquals(objectSize, o.getObjectMetadata().getContentLength());
        Assert.assertEquals(calcMultipartsETag(discontinuousPartETags), 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) UploadPartResult(com.aliyun.oss.model.UploadPartResult) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Test(org.junit.Test)

Example 8 with UploadPartRequest

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

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

the class SecurityTokenTest method testObjectOperationsWithToken.

@Test
public void testObjectOperationsWithToken() throws JSONException {
    List<String> actions = new ArrayList<String>();
    List<String> resources = new ArrayList<String>();
    // Put bucket with valid security token
    final String bucketName = "test-object-operations-with-token-bucket0";
    actions.add("oss:PutBucket");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName);
    OSSClient sessionClient = createSessionClient(actions, resources);
    try {
        sessionClient.createBucket(bucketName);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // Put object
    final String key = "test-object-operations-with-token-key0";
    final long instreamLength = 1024;
    InputStream instream = null;
    actions.add("oss:PutObject");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName + "/*");
    sessionClient = createSessionClient(actions, resources);
    try {
        instream = genFixedLengthInputStream(instreamLength);
        sessionClient.putObject(bucketName, key, instream, null);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        if (instream != null) {
            try {
                instream.close();
            } catch (IOException e) {
            }
        }
        sessionClient.shutdown();
    }
    // Get object
    actions.add("oss:GetObject");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName + "/*");
    sessionClient = createSessionClient(actions, resources);
    try {
        OSSObject o = sessionClient.getObject(bucketName, key);
        Assert.assertEquals(instreamLength, o.getObjectMetadata().getContentLength());
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // Copy object
    actions.add("oss:GetObject");
    actions.add("oss:PutObject");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName + "/*");
    sessionClient = createSessionClient(actions, resources);
    try {
        sessionClient.copyObject(bucketName, key, bucketName, key + DUMMY_SUFFIX);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // Initiate multipart upload and upload single part
    // 128KB
    final int partSize = 128 * 1024;
    String uploadId = null;
    List<PartETag> partETags = new ArrayList<PartETag>();
    actions.add("oss:PutObject");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName + "/*");
    sessionClient = createSessionClient(actions, resources);
    try {
        InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, key);
        InitiateMultipartUploadResult result = sessionClient.initiateMultipartUpload(request);
        instream = genFixedLengthInputStream(partSize);
        uploadId = result.getUploadId();
        // Upload single part
        UploadPartRequest uploadPartRequest = new UploadPartRequest();
        uploadPartRequest.setBucketName(bucketName);
        uploadPartRequest.setKey(key);
        uploadPartRequest.setInputStream(instream);
        uploadPartRequest.setPartNumber(1);
        uploadPartRequest.setPartSize(partSize);
        uploadPartRequest.setUploadId(uploadId);
        UploadPartResult uploadPartResult = sessionClient.uploadPart(uploadPartRequest);
        partETags.add(uploadPartResult.getPartETag());
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // List parts
    actions.add("oss:ListParts");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName + "/*");
    sessionClient = createSessionClient(actions, resources);
    try {
        ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, key, uploadId);
        PartListing partListing = sessionClient.listParts(listPartsRequest);
        Assert.assertEquals(1, partListing.getParts().size());
        Assert.assertEquals(bucketName, partListing.getBucketName());
        Assert.assertEquals(key, partListing.getKey());
        Assert.assertEquals(uploadId, partListing.getUploadId());
        Assert.assertEquals(1000, partListing.getMaxParts().intValue());
        Assert.assertEquals(1, partListing.getNextPartNumberMarker().intValue());
        Assert.assertFalse(partListing.isTruncated());
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // Complete multipart
    actions.add("oss:PutObject");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName + "/*");
    sessionClient = createSessionClient(actions, resources);
    try {
        CompleteMultipartUploadRequest request = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
        sessionClient.completeMultipartUpload(request);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    // Cleanup objects and bucket
    actions.add("oss:DeleteObject");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName + "/*");
    sessionClient = createSessionClient(actions, resources);
    try {
        sessionClient.deleteObject(bucketName, key);
        sessionClient.deleteObject(bucketName, key + DUMMY_SUFFIX);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
    actions.add("oss:DeleteBucket");
    resources.add("acs:oss:*:" + STS_USER + ":" + bucketName);
    sessionClient = createSessionClient(actions, resources);
    try {
        sessionClient.deleteBucket(bucketName);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        actions.clear();
        resources.clear();
        sessionClient.shutdown();
    }
}
Also used : InitiateMultipartUploadResult(com.aliyun.oss.model.InitiateMultipartUploadResult) OSSObject(com.aliyun.oss.model.OSSObject) OSSClient(com.aliyun.oss.OSSClient) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) InitiateMultipartUploadRequest(com.aliyun.oss.model.InitiateMultipartUploadRequest) UploadPartRequest(com.aliyun.oss.model.UploadPartRequest) IOException(java.io.IOException) OSSException(com.aliyun.oss.OSSException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) PartETag(com.aliyun.oss.model.PartETag) PartListing(com.aliyun.oss.model.PartListing) ListPartsRequest(com.aliyun.oss.model.ListPartsRequest) UploadPartResult(com.aliyun.oss.model.UploadPartResult) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Test(org.junit.Test)

Example 10 with UploadPartRequest

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

the class GetRequestIdTest method testNormalGetRequestId.

@Test
public void testNormalGetRequestId() {
    final String key = "normal-get-request-id";
    final long inputStreamLength = 1024;
    final int requestIdLength = "572BF2F2207FB3397648E9F1".length();
    try {
        // put object
        PutObjectResult putObjectResult = ossClient.putObject(bucketName, key, genFixedLengthInputStream(inputStreamLength));
        Assert.assertEquals(putObjectResult.getRequestId().length(), requestIdLength);
        // get object
        OSSObject ossObject = ossClient.getObject(bucketName, key);
        ossObject.getObjectContent().close();
        Assert.assertEquals(ossObject.getRequestId().length(), requestIdLength);
        File file = new File("tmp");
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
        ObjectMetadata objectMeta = ossClient.getObject(getObjectRequest, file);
        Assert.assertEquals(objectMeta.getRequestId().length(), requestIdLength);
        // delete object
        ossClient.deleteObject(bucketName, key);
        // append object
        AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, key, file);
        appendObjectRequest.setPosition(0L);
        AppendObjectResult appendObjectResult = ossClient.appendObject(appendObjectRequest);
        Assert.assertEquals(appendObjectResult.getRequestId().length(), requestIdLength);
        // getSimplifiedObjectMeta
        SimplifiedObjectMeta simplifiedObjectMeta = ossClient.getSimplifiedObjectMeta(bucketName, key);
        Assert.assertEquals(simplifiedObjectMeta.getRequestId().length(), requestIdLength);
        // getObjectMetadata
        ObjectMetadata objectMetadata = ossClient.getObjectMetadata(bucketName, key);
        Assert.assertEquals(objectMetadata.getRequestId().length(), requestIdLength);
        // delete objects
        DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName);
        ArrayList<String> keys = new ArrayList<String>();
        keys.add(key);
        deleteObjectsRequest.setKeys(keys);
        DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(deleteObjectsRequest);
        Assert.assertEquals(deleteObjectsResult.getRequestId().length(), requestIdLength);
        // initiate multipart upload
        InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(bucketName, key);
        InitiateMultipartUploadResult initiateMultipartUploadResult = ossClient.initiateMultipartUpload(initiateMultipartUploadRequest);
        Assert.assertEquals(initiateMultipartUploadResult.getRequestId().length(), requestIdLength);
        // upload part
        UploadPartRequest uploadPartRequest = new UploadPartRequest(bucketName, key, initiateMultipartUploadResult.getUploadId(), 1, new FileInputStream(file), inputStreamLength);
        UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
        Assert.assertEquals(uploadPartResult.getRequestId().length(), requestIdLength);
        // complete multipart upload
        List<PartETag> partETags = new ArrayList<PartETag>();
        partETags.add(new PartETag(1, uploadPartResult.getETag()));
        CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, initiateMultipartUploadResult.getUploadId(), partETags);
        CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
        Assert.assertEquals(completeMultipartUploadResult.getRequestId().length(), requestIdLength);
        // copy object
        CopyObjectResult CopyObjectResult = ossClient.copyObject(bucketName, key, bucketName, key);
        Assert.assertEquals(CopyObjectResult.getRequestId().length(), requestIdLength);
        // initiate multipart copy
        InitiateMultipartUploadRequest initiateMultipartCopyRequest = new InitiateMultipartUploadRequest(bucketName, key);
        InitiateMultipartUploadResult initiateMultipartCopyResult = ossClient.initiateMultipartUpload(initiateMultipartCopyRequest);
        Assert.assertEquals(initiateMultipartCopyResult.getRequestId().length(), requestIdLength);
        // upload part copy
        UploadPartCopyRequest uploadPartCopyRequest = new UploadPartCopyRequest(bucketName, key, bucketName, key, initiateMultipartCopyResult.getUploadId(), 1, 0L, inputStreamLength);
        UploadPartCopyResult uploadPartCopyResult = ossClient.uploadPartCopy(uploadPartCopyRequest);
        Assert.assertEquals(uploadPartCopyResult.getRequestId().length(), requestIdLength);
        // abort multipart upload
        AbortMultipartUploadRequest AbortMultipartUploadRequest = new AbortMultipartUploadRequest(bucketName, key, initiateMultipartCopyResult.getUploadId());
        ossClient.abortMultipartUpload(AbortMultipartUploadRequest);
        ossClient.deleteObject(bucketName, key);
        file.delete();
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : SimplifiedObjectMeta(com.aliyun.oss.model.SimplifiedObjectMeta) InitiateMultipartUploadResult(com.aliyun.oss.model.InitiateMultipartUploadResult) ArrayList(java.util.ArrayList) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) PartETag(com.aliyun.oss.model.PartETag) AppendObjectResult(com.aliyun.oss.model.AppendObjectResult) UploadPartResult(com.aliyun.oss.model.UploadPartResult) UploadPartCopyResult(com.aliyun.oss.model.UploadPartCopyResult) CopyObjectResult(com.aliyun.oss.model.CopyObjectResult) UploadPartCopyRequest(com.aliyun.oss.model.UploadPartCopyRequest) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest) OSSObject(com.aliyun.oss.model.OSSObject) PutObjectResult(com.aliyun.oss.model.PutObjectResult) InitiateMultipartUploadRequest(com.aliyun.oss.model.InitiateMultipartUploadRequest) UploadPartRequest(com.aliyun.oss.model.UploadPartRequest) AbortMultipartUploadRequest(com.aliyun.oss.model.AbortMultipartUploadRequest) DeleteObjectsResult(com.aliyun.oss.model.DeleteObjectsResult) FileInputStream(java.io.FileInputStream) DeleteObjectsRequest(com.aliyun.oss.model.DeleteObjectsRequest) AppendObjectRequest(com.aliyun.oss.model.AppendObjectRequest) File(java.io.File) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) 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