use of com.aliyun.oss.model.CompleteMultipartUploadRequest 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.CompleteMultipartUploadRequest 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.CompleteMultipartUploadRequest 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.CompleteMultipartUploadRequest in project aliyun-oss-java-sdk by aliyun.
the class ProgressBarTest method testMultipartsProgressBar.
@Ignore
public void testMultipartsProgressBar() throws Exception {
final String key = "multiparts-progress-bar";
final int instreamLength = 64 * 1024 * 1024;
ExecutorService executorService = Executors.newFixedThreadPool(5);
List<PartETag> partETags = Collections.synchronizedList(new ArrayList<PartETag>());
String uploadId = TestUtils.claimUploadId(ossClient, bucketName, key);
// 5MB
final long partSize = 5 * 1024 * 1024L;
final File tempFile = new File(genFixedLengthFile(instreamLength));
long fileLength = tempFile.length();
int partCount = (int) (fileLength / partSize);
if (fileLength % partSize != 0) {
partCount++;
}
for (int i = 0; i < partCount; i++) {
long startPos = i * partSize;
long curPartSize = (i + 1 == partCount) ? (fileLength - startPos) : partSize;
executorService.execute(new PartUploader(key, tempFile, startPos, curPartSize, i + 1, uploadId, partETags, new UploadPartProgressListener(i + 1)));
}
executorService.shutdown();
while (!executorService.isTerminated()) {
try {
executorService.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Assert.assertEquals(partCount, partETags.size());
Collections.sort(partETags, new Comparator<PartETag>() {
@Override
public int compare(PartETag p1, PartETag p2) {
return p1.getPartNumber() - p2.getPartNumber();
}
});
try {
CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
ossClient.completeMultipartUpload(completeMultipartUploadRequest);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.model.CompleteMultipartUploadRequest 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);
}
}
Aggregations