use of com.aliyun.oss.model.CompleteMultipartUploadRequest in project aliyun-oss-java-sdk by aliyun.
the class UploadPartCopyTest method testUnormalUploadPartCopy.
@Test
public void testUnormalUploadPartCopy() {
final String sourceBucket = "unormal-upload-part-copy-bucket-source";
final String targetBucket = "unormal-upload-part-copy-bucket-target";
final String sourceKey = "unormal-upload-part-copy-object-source";
final String targetKey = "unormal-upload-part-copy-object-target";
// Set length of parts less than minimum limit(100KB)
// 64KB
final long partSize = 64 * 1024;
try {
ossClient.createBucket(sourceBucket);
ossClient.createBucket(targetBucket);
waitForCacheExpiration(5);
// Put object into source bucket
String eTag = null;
try {
InputStream instream = genFixedLengthInputStream(partSize);
PutObjectResult result = ossClient.putObject(sourceBucket, sourceKey, instream, null);
eTag = result.getETag();
} catch (Exception e) {
Assert.fail(e.getMessage());
}
// Claim upload id for target bucket
String uploadId = claimUploadId(ossClient, targetBucket, targetKey);
// Copy part under non-existent source bucket
final String nonexistentSourceBucket = "nonexistent-source-bucket";
final int partNumber = 1;
try {
UploadPartCopyRequest uploadPartCopyRequest = new UploadPartCopyRequest(nonexistentSourceBucket, sourceKey, targetBucket, targetKey);
uploadPartCopyRequest.setPartNumber(partNumber);
uploadPartCopyRequest.setUploadId(uploadId);
ossClient.uploadPartCopy(uploadPartCopyRequest);
Assert.fail("Upload part copy should not be successfuly");
} catch (OSSException e) {
Assert.assertEquals(OSSErrorCode.NO_SUCH_BUCKET, e.getErrorCode());
Assert.assertTrue(e.getMessage().startsWith(NO_SUCH_BUCKET_ERR));
}
// Copy part from non-existent source key
final String nonexistentSourceKey = "nonexistent-source-key";
try {
UploadPartCopyRequest uploadPartCopyRequest = new UploadPartCopyRequest(sourceBucket, nonexistentSourceKey, targetBucket, targetKey);
uploadPartCopyRequest.setPartNumber(partNumber);
uploadPartCopyRequest.setUploadId(uploadId);
ossClient.uploadPartCopy(uploadPartCopyRequest);
Assert.fail("Upload part copy should not be successfuly");
} catch (OSSException e) {
Assert.assertEquals(OSSErrorCode.NO_SUCH_KEY, e.getErrorCode());
Assert.assertTrue(e.getMessage().startsWith(NO_SUCH_KEY_ERR));
}
// Copy part to non-existent target bucket
final String nonexistentTargetBucket = "nonexistent-target-key";
try {
UploadPartCopyRequest uploadPartCopyRequest = new UploadPartCopyRequest(sourceBucket, sourceKey, nonexistentTargetBucket, targetKey);
uploadPartCopyRequest.setPartNumber(partNumber);
uploadPartCopyRequest.setUploadId(uploadId);
ossClient.uploadPartCopy(uploadPartCopyRequest);
Assert.fail("Upload part copy should not be successfuly");
} catch (OSSException e) {
Assert.assertEquals(OSSErrorCode.NO_SUCH_BUCKET, e.getErrorCode());
Assert.assertTrue(e.getMessage().startsWith(NO_SUCH_BUCKET_ERR));
}
// Upload part copy
List<PartETag> partETags = new ArrayList<PartETag>();
try {
UploadPartCopyRequest uploadPartCopyRequest = new UploadPartCopyRequest(sourceBucket, sourceKey, targetBucket, targetKey);
uploadPartCopyRequest.setPartNumber(partNumber);
uploadPartCopyRequest.setUploadId(uploadId);
UploadPartCopyResult uploadPartCopyResult = ossClient.uploadPartCopy(uploadPartCopyRequest);
partETags.add(uploadPartCopyResult.getPartETag());
Assert.assertEquals(eTag, uploadPartCopyResult.getETag());
Assert.assertEquals(partNumber, uploadPartCopyResult.getPartNumber());
uploadPartCopyRequest = new UploadPartCopyRequest(sourceBucket, sourceKey, targetBucket, targetKey);
uploadPartCopyRequest.setPartNumber(partNumber + 1);
uploadPartCopyRequest.setUploadId(uploadId);
uploadPartCopyResult = ossClient.uploadPartCopy(uploadPartCopyRequest);
partETags.add(uploadPartCopyResult.getPartETag());
Assert.assertEquals(eTag, uploadPartCopyResult.getETag());
Assert.assertEquals(partNumber + 1, uploadPartCopyResult.getPartNumber());
ListPartsRequest listPartsRequest = new ListPartsRequest(targetBucket, targetKey, uploadId);
PartListing partListing = ossClient.listParts(listPartsRequest);
Assert.assertEquals(2, partListing.getParts().size());
Assert.assertEquals(targetBucket, partListing.getBucketName());
Assert.assertEquals(targetKey, partListing.getKey());
Assert.assertEquals(uploadId, partListing.getUploadId());
Assert.assertEquals(LIST_PART_MAX_RETURNS, partListing.getMaxParts().intValue());
Assert.assertNotNull(partListing.getNextPartNumberMarker());
Assert.assertFalse(partListing.isTruncated());
} catch (Exception e) {
Assert.fail(e.getMessage());
}
// Try to complete multipart upload with all uploaded parts
try {
CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(targetBucket, targetKey, uploadId, partETags);
ossClient.completeMultipartUpload(completeMultipartUploadRequest);
Assert.fail("Upload part copy should not be successfuly");
} catch (OSSException e) {
Assert.assertEquals(OSSErrorCode.ENTITY_TOO_SMALL, e.getErrorCode());
Assert.assertTrue(e.getMessage().startsWith(ENTITY_TOO_SMALL_ERR));
}
// Abort the incompleted multipart upload
try {
AbortMultipartUploadRequest abortMultipartUploadRequest = new AbortMultipartUploadRequest(targetBucket, targetKey, uploadId);
ossClient.abortMultipartUpload(abortMultipartUploadRequest);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
} catch (Exception e) {
Assert.fail(e.getMessage());
} finally {
deleteBucketWithObjects(ossClient, sourceBucket);
deleteBucketWithObjects(ossClient, targetBucket);
}
}
use of com.aliyun.oss.model.CompleteMultipartUploadRequest in project aliyun-oss-java-sdk by aliyun.
the class UploadPartCopyTest method testNormalUploadPartCopyByRange.
@Test
public void testNormalUploadPartCopyByRange() {
final String sourceBucket = "normal-upload-part-copy-by-range-bucket-source";
final String targetBucket = "normal-upload-part-copy-by-range-bucket-target";
final String sourceKey = "normal-upload-part-copy-by-range-object-source";
final String targetKey = "normal-upload-part-copy-by-range-object-target";
// 128KB
final long partSize = 128 * 1024;
try {
ossClient.createBucket(sourceBucket);
ossClient.createBucket(targetBucket);
waitForCacheExpiration(5);
// Put object into source bucket
final long inputStreamLength = partSize * 4;
try {
InputStream instream = genFixedLengthInputStream(inputStreamLength);
ossClient.putObject(sourceBucket, sourceKey, instream, null);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
// Claim upload id for target bucket
String uploadId = claimUploadId(ossClient, targetBucket, targetKey);
// Upload part copy
final int partNumber = 1;
final long beginIndex = partSize;
List<PartETag> partETags = new ArrayList<PartETag>();
UploadPartCopyRequest uploadPartCopyRequest = new UploadPartCopyRequest(sourceBucket, sourceKey, targetBucket, targetKey);
uploadPartCopyRequest.setPartNumber(partNumber);
uploadPartCopyRequest.setUploadId(uploadId);
uploadPartCopyRequest.setBeginIndex(beginIndex);
uploadPartCopyRequest.setPartSize(partSize);
UploadPartCopyResult uploadPartCopyResult = ossClient.uploadPartCopy(uploadPartCopyRequest);
partETags.add(uploadPartCopyResult.getPartETag());
Assert.assertEquals(partNumber, uploadPartCopyResult.getPartNumber());
Assert.assertEquals(uploadPartCopyResult.getRequestId().length(), REQUEST_ID_LEN);
ListPartsRequest listPartsRequest = new ListPartsRequest(targetBucket, targetKey, uploadId);
PartListing partListing = ossClient.listParts(listPartsRequest);
Assert.assertEquals(1, partListing.getParts().size());
Assert.assertEquals(targetBucket, partListing.getBucketName());
Assert.assertEquals(targetKey, partListing.getKey());
Assert.assertEquals(uploadId, partListing.getUploadId());
Assert.assertEquals(LIST_PART_MAX_RETURNS, partListing.getMaxParts().intValue());
Assert.assertNotNull(partListing.getNextPartNumberMarker());
Assert.assertFalse(partListing.isTruncated());
Assert.assertEquals(partListing.getRequestId().length(), REQUEST_ID_LEN);
// Complete multipart upload
CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(targetBucket, targetKey, uploadId, partETags);
CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
Assert.assertEquals(composeLocation(ossClient, OSS_TEST_ENDPOINT, targetBucket, targetKey), completeMultipartUploadResult.getLocation());
Assert.assertEquals(targetBucket, completeMultipartUploadResult.getBucketName());
Assert.assertEquals(targetKey, completeMultipartUploadResult.getKey());
Assert.assertEquals(calcMultipartsETag(partETags), completeMultipartUploadResult.getETag());
Assert.assertEquals(completeMultipartUploadResult.getRequestId().length(), REQUEST_ID_LEN);
// Get uploaded object
OSSObject o = ossClient.getObject(targetBucket, targetKey);
final long objectSize = 1 * partSize;
Assert.assertEquals(objectSize, o.getObjectMetadata().getContentLength());
Assert.assertEquals(calcMultipartsETag(partETags), o.getObjectMetadata().getETag());
Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
} catch (Exception e) {
Assert.fail(e.getMessage());
} finally {
deleteBucketWithObjects(ossClient, sourceBucket);
deleteBucketWithObjects(ossClient, targetBucket);
}
}
use of com.aliyun.oss.model.CompleteMultipartUploadRequest in project aliyun-oss-java-sdk by aliyun.
the class UploadPartCopyTest method testNormalUploadPartCopyWithSpecialChars.
@Test
public void testNormalUploadPartCopyWithSpecialChars() {
final String sourceBucket = "normal-upload-part-copy-by-range-bucket-source";
final String targetBucket = "normal-upload-part-copy-by-range-bucket-target";
final String sourceKey = "测\\r试-中.~,+\"'*&¥#@%!(文)+字符|?/.zip";
final String targetKey = "测\\r试-中.~,+\"'*&¥#@%!(文)+字符|?-2.zip";
// 128KB
final long partSize = 128 * 1024;
try {
ossClient.createBucket(sourceBucket);
ossClient.createBucket(targetBucket);
// Put object into source bucket
final long inputStreamLength = partSize * 4;
try {
InputStream instream = genFixedLengthInputStream(inputStreamLength);
ossClient.putObject(sourceBucket, sourceKey, instream, null);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
// Claim upload id for target bucket
String uploadId = claimUploadId(ossClient, targetBucket, targetKey);
// Upload part copy
final int partNumber = 1;
final long beginIndex = partSize;
List<PartETag> partETags = new ArrayList<PartETag>();
UploadPartCopyRequest uploadPartCopyRequest = new UploadPartCopyRequest(sourceBucket, sourceKey, targetBucket, targetKey);
uploadPartCopyRequest.setPartNumber(partNumber);
uploadPartCopyRequest.setUploadId(uploadId);
uploadPartCopyRequest.setBeginIndex(beginIndex);
uploadPartCopyRequest.setPartSize(partSize);
UploadPartCopyResult uploadPartCopyResult = ossClient.uploadPartCopy(uploadPartCopyRequest);
partETags.add(uploadPartCopyResult.getPartETag());
Assert.assertEquals(partNumber, uploadPartCopyResult.getPartNumber());
Assert.assertEquals(uploadPartCopyResult.getRequestId().length(), REQUEST_ID_LEN);
ListPartsRequest listPartsRequest = new ListPartsRequest(targetBucket, targetKey, uploadId);
PartListing partListing = ossClient.listParts(listPartsRequest);
Assert.assertEquals(1, partListing.getParts().size());
Assert.assertEquals(targetBucket, partListing.getBucketName());
Assert.assertEquals(targetKey, partListing.getKey());
Assert.assertEquals(uploadId, partListing.getUploadId());
Assert.assertEquals(LIST_PART_MAX_RETURNS, partListing.getMaxParts().intValue());
Assert.assertNotNull(partListing.getNextPartNumberMarker());
Assert.assertFalse(partListing.isTruncated());
Assert.assertEquals(partListing.getRequestId().length(), REQUEST_ID_LEN);
// Complete multipart upload
CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(targetBucket, targetKey, uploadId, partETags);
CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
Assert.assertEquals(composeLocation(ossClient, OSS_TEST_ENDPOINT, targetBucket, targetKey), completeMultipartUploadResult.getLocation());
Assert.assertEquals(targetBucket, completeMultipartUploadResult.getBucketName());
Assert.assertEquals(targetKey, completeMultipartUploadResult.getKey());
Assert.assertEquals(calcMultipartsETag(partETags), completeMultipartUploadResult.getETag());
Assert.assertEquals(completeMultipartUploadResult.getRequestId().length(), REQUEST_ID_LEN);
// Get uploaded object
OSSObject o = ossClient.getObject(targetBucket, targetKey);
final long objectSize = 1 * partSize;
Assert.assertEquals(objectSize, o.getObjectMetadata().getContentLength());
Assert.assertEquals(calcMultipartsETag(partETags), o.getObjectMetadata().getETag());
Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
} catch (Exception e) {
Assert.fail(e.getMessage());
} finally {
deleteBucketWithObjects(ossClient, sourceBucket);
deleteBucketWithObjects(ossClient, targetBucket);
}
}
use of com.aliyun.oss.model.CompleteMultipartUploadRequest 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());
}
}
use of com.aliyun.oss.model.CompleteMultipartUploadRequest 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());
}
}
Aggregations