use of com.aliyun.oss.model.PartListing 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();
}
}
use of com.aliyun.oss.model.PartListing 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.PartListing 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.PartListing 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.PartListing 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);
}
}
Aggregations