use of com.aliyun.oss.model.ListPartsRequest in project aliyun-oss-java-sdk by aliyun.
the class MultipartUploadSample method listAllParts.
private static void listAllParts(String uploadId) {
System.out.println("Listing all parts......");
ListPartsRequest listPartsRequest = new ListPartsRequest(bucketName, key, uploadId);
PartListing partListing = client.listParts(listPartsRequest);
int partCount = partListing.getParts().size();
for (int i = 0; i < partCount; i++) {
PartSummary partSummary = partListing.getParts().get(i);
System.out.println("\tPart#" + partSummary.getPartNumber() + ", ETag=" + partSummary.getETag());
}
System.out.println();
}
use of com.aliyun.oss.model.ListPartsRequest in project aliyun-oss-java-sdk by aliyun.
the class UploadPartCopyTest method testUnormalUploadPartCopyByRange.
@Test
public void testUnormalUploadPartCopyByRange() {
final String sourceBucket = "unormal-upload-part-copy-by-range-bucket-source";
final String targetBucket = "unormal-upload-part-copy-by-range-bucket-target";
final String sourceKey = "unormal-upload-part-copy-by-range-object-source";
final String targetKey = "unormal-upload-part-copy-by-range-object-target";
// 128KB
final long partSize = 128 * 1024;
try {
ossClient.createBucket(sourceBucket);
ossClient.createBucket(targetBucket);
// Put object into source bucket
final long inputStreamLength = partSize * 4;
String eTag = null;
try {
InputStream instream = genFixedLengthInputStream(inputStreamLength);
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 with invalid copy range
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);
// Illegal copy range([beginIndex, begin + inputStreamLength]), just copy entire object
uploadPartCopyRequest.setPartSize(inputStreamLength);
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 = inputStreamLength;
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 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());
}
}
use of com.aliyun.oss.model.ListPartsRequest in project aliyun-oss-java-sdk by aliyun.
the class UploadPartTest method testNormalListParts.
@Test
public void testNormalListParts() {
final String key = "normal-list-parts-object";
// 128KB
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 without any special conditions
listPartsRequest = new ListPartsRequest(bucketName, key, uploadId);
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 'max-parts' parts each time
final int maxParts = 15;
listPartsRequest = new ListPartsRequest(bucketName, key, uploadId);
listPartsRequest.setMaxParts(maxParts);
partListing = ossClient.listParts(listPartsRequest);
Assert.assertEquals(maxParts, partListing.getParts().size());
for (int i = 0; i < maxParts; 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(partSize, ps.getSize());
}
Assert.assertEquals(bucketName, partListing.getBucketName());
Assert.assertEquals(key, partListing.getKey());
Assert.assertEquals(uploadId, partListing.getUploadId());
Assert.assertEquals(maxParts, partListing.getMaxParts().intValue());
Assert.assertEquals(maxParts, partListing.getNextPartNumberMarker().intValue());
Assert.assertTrue(partListing.isTruncated());
Assert.assertEquals(partListing.getRequestId().length(), REQUEST_ID_LEN);
// List 'max-parts' parts with 'part-number-marker'
final int partNumberMarker = 20;
listPartsRequest.setPartNumberMarker(partNumberMarker);
partListing = ossClient.listParts(listPartsRequest);
Assert.assertEquals(partCount - partNumberMarker, partListing.getParts().size());
for (int i = 0; i < (partCount - partNumberMarker); i++) {
PartSummary ps = partListing.getParts().get(i);
PartETag eTag = partETags.get(partNumberMarker + i);
Assert.assertEquals(eTag.getPartNumber(), ps.getPartNumber());
Assert.assertEquals(eTag.getETag(), ps.getETag());
Assert.assertEquals(partSize, ps.getSize());
}
Assert.assertEquals(bucketName, partListing.getBucketName());
Assert.assertEquals(key, partListing.getKey());
Assert.assertEquals(uploadId, partListing.getUploadId());
Assert.assertEquals(maxParts, 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);
// 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());
}
}
use of com.aliyun.oss.model.ListPartsRequest 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();
}
}
Aggregations