Search in sources :

Example 1 with MultipartUpload

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

the class ResponseParsers method parseListMultipartUploads.

/**
 * Unmarshall list multipart uploads response body to multipart upload
 * listing.
 */
@SuppressWarnings("unchecked")
public static MultipartUploadListing parseListMultipartUploads(InputStream responseBody) throws ResponseParseException {
    try {
        Element root = getXmlRootElement(responseBody);
        MultipartUploadListing multipartUploadListing = new MultipartUploadListing();
        multipartUploadListing.setBucketName(root.getChildText("Bucket"));
        multipartUploadListing.setMaxUploads(Integer.valueOf(root.getChildText("MaxUploads")));
        multipartUploadListing.setTruncated(Boolean.valueOf(root.getChildText("IsTruncated")));
        if (root.getChild("Delimiter") != null) {
            String delimiter = root.getChildText("Delimiter");
            if (!isNullOrEmpty(delimiter)) {
                multipartUploadListing.setDelimiter(delimiter);
            }
        }
        if (root.getChild("Prefix") != null) {
            String prefix = root.getChildText("Prefix");
            if (!isNullOrEmpty(prefix)) {
                multipartUploadListing.setPrefix(prefix);
            }
        }
        if (root.getChild("KeyMarker") != null) {
            String keyMarker = root.getChildText("KeyMarker");
            if (!isNullOrEmpty(keyMarker)) {
                multipartUploadListing.setKeyMarker(keyMarker);
            }
        }
        if (root.getChild("UploadIdMarker") != null) {
            String uploadIdMarker = root.getChildText("UploadIdMarker");
            if (!isNullOrEmpty(uploadIdMarker)) {
                multipartUploadListing.setUploadIdMarker(uploadIdMarker);
            }
        }
        if (root.getChild("NextKeyMarker") != null) {
            String nextKeyMarker = root.getChildText("NextKeyMarker");
            if (!isNullOrEmpty(nextKeyMarker)) {
                multipartUploadListing.setNextKeyMarker(nextKeyMarker);
            }
        }
        if (root.getChild("NextUploadIdMarker") != null) {
            String nextUploadIdMarker = root.getChildText("NextUploadIdMarker");
            if (!isNullOrEmpty(nextUploadIdMarker)) {
                multipartUploadListing.setNextUploadIdMarker(nextUploadIdMarker);
            }
        }
        List<Element> uploadElems = root.getChildren("Upload");
        for (Element elem : uploadElems) {
            if (elem.getChild("Initiated") == null) {
                continue;
            }
            MultipartUpload mu = new MultipartUpload();
            mu.setKey(elem.getChildText("Key"));
            mu.setUploadId(elem.getChildText("UploadId"));
            mu.setStorageClass(elem.getChildText("StorageClass"));
            mu.setInitiated(DateUtil.parseIso8601Date(elem.getChildText("Initiated")));
            multipartUploadListing.addMultipartUpload(mu);
        }
        List<Element> commonPrefixesElems = root.getChildren("CommonPrefixes");
        for (Element elem : commonPrefixesElems) {
            String prefix = elem.getChildText("Prefix");
            if (!isNullOrEmpty(prefix)) {
                multipartUploadListing.addCommonPrefix(prefix);
            }
        }
        return multipartUploadListing;
    } catch (JDOMParseException e) {
        throw new ResponseParseException(e.getPartialDocument() + ": " + e.getMessage(), e);
    } catch (Exception e) {
        throw new ResponseParseException(e.getMessage(), e);
    }
}
Also used : JDOMParseException(org.jdom.input.JDOMParseException) Element(org.jdom.Element) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException) MultipartUploadListing(com.aliyun.oss.model.MultipartUploadListing) MultipartUpload(com.aliyun.oss.model.MultipartUpload) ParseException(java.text.ParseException) JDOMParseException(org.jdom.input.JDOMParseException) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException)

Example 2 with MultipartUpload

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

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

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

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

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