use of com.aliyun.oss.model.ListPartsRequest in project aliyun-oss-java-sdk by aliyun.
the class UploadPartCopyTest method testNormalUploadPartCopy.
@Test
public void testNormalUploadPartCopy() {
final String sourceBucket = "normal-upload-part-copy-bucket-source";
final String targetBucket = "normal-upload-part-copy-bucket-target";
final String sourceKey = "normal-upload-part-copy-object-source";
final String targetKey = "normal-upload-part-copy-object-target";
// 128KB
final long partSize = 128 * 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);
// Upload part copy
final int partNumber = 1;
List<PartETag> partETags = new ArrayList<PartETag>();
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());
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.ListPartsRequest 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.ListPartsRequest 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.ListPartsRequest 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.ListPartsRequest 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