use of com.aliyun.oss.model.MultipartUploadListing 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());
}
}
use of com.aliyun.oss.model.MultipartUploadListing in project aliyun-oss-java-sdk by aliyun.
the class TestBase method abortAllMultipartUploads.
protected static void abortAllMultipartUploads(OSSClient client, String bucketName) {
if (!client.doesBucketExist(bucketName)) {
return;
}
String keyMarker = null;
String uploadIdMarker = null;
ListMultipartUploadsRequest listMultipartUploadsRequest = null;
MultipartUploadListing multipartUploadListing = null;
List<MultipartUpload> multipartUploads = null;
do {
listMultipartUploadsRequest = new ListMultipartUploadsRequest(bucketName);
listMultipartUploadsRequest.setKeyMarker(keyMarker);
listMultipartUploadsRequest.setUploadIdMarker(uploadIdMarker);
multipartUploadListing = client.listMultipartUploads(listMultipartUploadsRequest);
multipartUploads = multipartUploadListing.getMultipartUploads();
for (MultipartUpload mu : multipartUploads) {
String key = mu.getKey();
String uploadId = mu.getUploadId();
client.abortMultipartUpload(new AbortMultipartUploadRequest(bucketName, key, uploadId));
}
keyMarker = multipartUploadListing.getKeyMarker();
uploadIdMarker = multipartUploadListing.getUploadIdMarker();
} while (multipartUploadListing != null && multipartUploadListing.isTruncated());
}
use of com.aliyun.oss.model.MultipartUploadListing in project aliyun-oss-java-sdk by aliyun.
the class OSSResponseParserTest method testParseListMultipartUploads.
@Test
public void testParseListMultipartUploads() throws Exception {
String filename = "listMultipartUploads.xml";
InputStream in = getInputStream(filename);
MultipartUploadListing multipartUploadListing = ResponseParsers.parseListMultipartUploads(in);
Assert.assertEquals("dp7d8j2xfec1m984em9xmkgc_gan", multipartUploadListing.getBucketName());
Assert.assertNull(multipartUploadListing.getKeyMarker());
Assert.assertNull(multipartUploadListing.getUploadIdMarker());
Assert.assertEquals("test.rar", multipartUploadListing.getNextKeyMarker());
Assert.assertEquals("0004B984CE74CF65B555A38F98CCFF96", multipartUploadListing.getNextUploadIdMarker());
Assert.assertNull(multipartUploadListing.getDelimiter());
Assert.assertNull(multipartUploadListing.getPrefix());
Assert.assertEquals(1000, multipartUploadListing.getMaxUploads());
Assert.assertEquals(false, multipartUploadListing.isTruncated());
Assert.assertEquals("test.rar", multipartUploadListing.getMultipartUploads().get(0).getKey());
Assert.assertEquals("0004B9846579745A77D988FFFDDEAFC3", multipartUploadListing.getMultipartUploads().get(0).getUploadId());
Assert.assertEquals("Standard", multipartUploadListing.getMultipartUploads().get(0).getStorageClass());
Assert.assertEquals(DateUtil.parseIso8601Date("2012-02-22T02:36:36.000Z"), multipartUploadListing.getMultipartUploads().get(0).getInitiated());
in.close();
}
Aggregations