use of com.aliyun.oss.model.UploadPartRequest in project aliyun-oss-java-sdk by aliyun.
the class ObjectAclTest method testUploadMultipartsWithAclHeader.
@Test
public void testUploadMultipartsWithAclHeader() {
final String key = "normal-upload-multiparts-with-acl-header";
// 128KB
final int partSize = 128 * 1024;
final int partCount = 10;
try {
// Initial multipart upload
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());
}
// Complete multipart upload
CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
completeMultipartUploadRequest.setObjectACL(CannedAccessControlList.PublicRead);
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());
// Verify uploaded objects acl
ObjectAcl returnedACL = ossClient.getObjectAcl(bucketName, key);
Assert.assertEquals(ObjectPermission.PublicRead, returnedACL.getPermission());
Assert.assertEquals(returnedACL.getRequestId().length(), REQUEST_ID_LEN);
Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.model.UploadPartRequest in project aliyun-oss-java-sdk by aliyun.
the class CRCChecksumTest method testMutilUploadCRC.
@Test
public void testMutilUploadCRC() {
String key = "mutil-upload-object-crc";
try {
List<PartETag> partETags = new ArrayList<PartETag>();
// 初始化上传任务
InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(bucketName, key);
InitiateMultipartUploadResult initiateMultipartUploadResult = ossClient.initiateMultipartUpload(initiateMultipartUploadRequest);
Assert.assertEquals(initiateMultipartUploadResult.getRequestId().length(), REQUEST_ID_LEN);
String uploadId = initiateMultipartUploadResult.getUploadId();
// 上传分片
for (int i = 0; i < 5; i++) {
long fileLen = 1024 * 100 * (i + 1);
String filePath = TestUtils.genFixedLengthFile(fileLen);
UploadPartRequest request = new UploadPartRequest(bucketName, key, uploadId, i + 1, new FileInputStream(filePath), fileLen);
UploadPartResult uploadPartResult = ossClient.uploadPart(request);
Assert.assertEquals(uploadPartResult.getRequestId().length(), REQUEST_ID_LEN);
partETags.add(uploadPartResult.getPartETag());
checkCRC(uploadPartResult);
}
// 提交上传任务,服务器返回整个文件的CRC,客户端没有计算
CompleteMultipartUploadRequest CompleteMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(CompleteMultipartUploadRequest);
Assert.assertEquals(completeMultipartUploadResult.getRequestId().length(), REQUEST_ID_LEN);
checkCRC(completeMultipartUploadResult);
ossClient.deleteObject(bucketName, key);
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.model.UploadPartRequest in project aliyun-oss-java-sdk by aliyun.
the class CallbackTest method testMultipartUploadCallbackBody.
/**
* callback body has variables, type is url
*/
@Test
public void testMultipartUploadCallbackBody() {
String key = "multipart-upload-callback-body";
try {
String uploadId = claimUploadId(ossClient, bucketName, key);
InputStream instream = genFixedLengthInputStream(instreamLength);
List<PartETag> partETags = new ArrayList<PartETag>();
UploadPartRequest uploadPartRequest = new UploadPartRequest();
uploadPartRequest.setBucketName(bucketName);
uploadPartRequest.setKey(key);
uploadPartRequest.setInputStream(instream);
uploadPartRequest.setPartNumber(1);
uploadPartRequest.setPartSize(instreamLength);
uploadPartRequest.setUploadId(uploadId);
UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
partETags.add(uploadPartResult.getPartETag());
Callback callback = new Callback();
callback.setCallbackUrl(callbackUrl);
callback.setCallbackHost("oss-cn-hangzhou.aliyuncs.com");
callback.setCallbackBody("bucket=${bucket}&object=${object}&etag=${etag}&size=${size}&mimeType=${mimeType}&imageInfo.height=${imageInfo.height}&imageInfo.width=${imageInfo.width}&imageInfo.format=${imageInfo.format}&my_var=${x:my_var}");
callback.setCalbackBodyType(CalbackBodyType.URL);
CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
completeMultipartUploadRequest.setCallback(callback);
CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
byte[] buffer = new byte[bufferLength];
int nRead = completeMultipartUploadResult.getResponse().getContent().read(buffer);
completeMultipartUploadResult.getResponse().getContent().close();
Assert.assertEquals(callbackResponse, new String(buffer, 0, nRead));
OSSObject obj = ossClient.getObject(bucketName, key);
Assert.assertEquals(key, obj.getKey());
Assert.assertEquals(instreamLength, obj.getObjectMetadata().getContentLength());
obj.forcedClose();
} catch (Exception ex) {
Assert.fail(ex.getMessage());
}
}
use of com.aliyun.oss.model.UploadPartRequest 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.UploadPartRequest 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