use of com.qcloud.cos.model.UploadResult in project cos-java-sdk-v5 by tencentyun.
the class TransferManagerTest method testTransferManagerUploadDownBigFile.
@Test
public void testTransferManagerUploadDownBigFile() throws IOException, CosServiceException, CosClientException, InterruptedException {
if (!judgeUserInfoValid()) {
return;
}
TransferManager transferManager = new TransferManager(cosclient);
File localFile = buildTestFile(1024 * 1024 * 10L);
File downFile = new File(localFile.getAbsolutePath() + ".down");
String key = "ut/" + localFile.getName();
String destKey = key + ".copy";
try {
PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, localFile);
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setServerSideEncryption("AES256");
putObjectRequest.setMetadata(objectMetadata);
Upload upload = transferManager.upload(putObjectRequest);
UploadResult uploadResult = upload.waitForUploadResult();
assertTrue(uploadResult.getETag().contains("-"));
assertTrue(upload.isResumeableMultipartUploadAfterFailed());
assertNotNull(upload.getResumeableMultipartUploadId());
assertNotNull(uploadResult.getRequestId());
assertNotNull(uploadResult.getDateStr());
GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
Download download = transferManager.download(getObjectRequest, downFile);
download.waitForCompletion();
// check file
assertEquals(Md5Utils.md5Hex(localFile), Md5Utils.md5Hex(downFile));
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucket, key, bucket, destKey);
Copy copy = transferManager.copy(copyObjectRequest, cosclient, null);
copy.waitForCompletion();
} finally {
// delete file on cos
clearObject(key);
// delete file on cos
clearObject(destKey);
if (localFile.exists()) {
assertTrue(localFile.delete());
}
if (downFile.exists()) {
assertTrue(downFile.delete());
}
}
}
use of com.qcloud.cos.model.UploadResult in project cos-java-sdk-v5 by tencentyun.
the class TransferManagerTest method testTransferManagerUploadDownCopySmallFile.
@Test
public void testTransferManagerUploadDownCopySmallFile() throws IOException, CosServiceException, CosClientException, InterruptedException {
if (!judgeUserInfoValid()) {
return;
}
TransferManager transferManager = new TransferManager(cosclient);
File localFile = buildTestFile(1024 * 1024 * 2L);
File downFile = new File(localFile.getAbsolutePath() + ".down");
String key = "ut/" + localFile.getName();
String destKey = key + ".copy";
try {
PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, localFile);
Upload upload = transferManager.upload(putObjectRequest);
UploadResult uploadResult = upload.waitForUploadResult();
// head object
headSimpleObject(key, localFile.length(), Md5Utils.md5Hex(localFile));
assertEquals(Md5Utils.md5Hex(localFile), uploadResult.getETag());
assertFalse(upload.isResumeableMultipartUploadAfterFailed());
assertNull(upload.getResumeableMultipartUploadId());
assertNotNull(uploadResult.getRequestId());
assertNotNull(uploadResult.getDateStr());
GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
Download download = transferManager.download(getObjectRequest, downFile);
download.waitForCompletion();
// check file
assertEquals(Md5Utils.md5Hex(localFile), Md5Utils.md5Hex(downFile));
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucket, key, bucket, destKey);
Copy copy = transferManager.copy(copyObjectRequest, cosclient, null);
copy.waitForCompletion();
} finally {
// clear object
clearObject(key);
// clear dest object
clearObject(destKey);
// delete smallfile
if (localFile.exists()) {
assertTrue(localFile.delete());
}
if (downFile.exists()) {
assertTrue(downFile.delete());
}
}
}
use of com.qcloud.cos.model.UploadResult in project cos-java-sdk-v5 by tencentyun.
the class UploadCallable method uploadInOneChunk.
/**
* Uploads the given request in a single chunk and returns the result.
*/
private UploadResult uploadInOneChunk() {
PutObjectResult putObjectResult = cos.putObject(origReq);
UploadResult uploadResult = new UploadResult();
uploadResult.setBucketName(origReq.getBucketName());
uploadResult.setKey(origReq.getKey());
uploadResult.setETag(putObjectResult.getETag());
uploadResult.setVersionId(putObjectResult.getVersionId());
uploadResult.setRequestId(putObjectResult.getRequestId());
uploadResult.setDateStr(putObjectResult.getDateStr());
uploadResult.setCrc64Ecma(putObjectResult.getCrc64Ecma());
uploadResult.setCiUploadResult(putObjectResult.getCiUploadResult());
return uploadResult;
}
use of com.qcloud.cos.model.UploadResult in project cos-java-sdk-v5 by tencentyun.
the class UploadCallable method uploadPartsInSeries.
/**
* Uploads all parts in the request in serial in this thread, then completes the upload and
* returns the result.
*/
private UploadResult uploadPartsInSeries(UploadPartRequestFactory requestFactory) {
final List<PartETag> partETags = new ArrayList<PartETag>();
while (requestFactory.hasMoreRequests()) {
if (threadPool.isShutdown())
throw new CancellationException("TransferManager has been shutdown");
UploadPartRequest uploadPartRequest = requestFactory.getNextUploadPartRequest();
// Mark the stream in case we need to reset it
InputStream inputStream = uploadPartRequest.getInputStream();
if (inputStream != null && inputStream.markSupported()) {
if (uploadPartRequest.getPartSize() >= Integer.MAX_VALUE) {
inputStream.mark(Integer.MAX_VALUE);
} else {
inputStream.mark((int) uploadPartRequest.getPartSize());
}
}
partETags.add(cos.uploadPart(uploadPartRequest).getPartETag());
}
CompleteMultipartUploadRequest req = new CompleteMultipartUploadRequest(origReq.getBucketName(), origReq.getKey(), multipartUploadId, partETags).withGeneralProgressListener(origReq.getGeneralProgressListener());
ObjectMetadata origMeta = origReq.getMetadata();
if (origMeta != null) {
ObjectMetadata objMeta = req.getObjectMetadata();
if (objMeta == null) {
objMeta = new ObjectMetadata();
}
objMeta.setUserMetadata(origMeta.getUserMetadata());
req.setObjectMetadata(objMeta);
}
if (origReq.getPicOperations() != null) {
req.setPicOperations(origReq.getPicOperations());
}
TransferManagerUtils.populateEndpointAddr(origReq, req);
CompleteMultipartUploadResult res = cos.completeMultipartUpload(req);
UploadResult uploadResult = new UploadResult();
uploadResult.setBucketName(res.getBucketName());
uploadResult.setKey(res.getKey());
uploadResult.setETag(res.getETag());
uploadResult.setVersionId(res.getVersionId());
uploadResult.setRequestId(res.getRequestId());
uploadResult.setDateStr(res.getDateStr());
uploadResult.setCrc64Ecma(res.getCrc64Ecma());
uploadResult.setCiUploadResult(res.getCiUploadResult());
return uploadResult;
}
use of com.qcloud.cos.model.UploadResult in project cos-java-sdk-v5 by tencentyun.
the class UploadImpl method waitForUploadResult.
/**
* Waits for this upload to complete and returns the result of this upload. This is a blocking
* call. Be prepared to handle errors when calling this method. Any errors that occurred during
* the asynchronous transfer will be re-thrown through this method.
*
* @return The result of this transfer.
*
* @throws CosClientException If any errors were encountered in the client while making the
* request or handling the response.
* @throws CosServiceException If any errors occurred in Qcloud COS while processing the
* request.
* @throws InterruptedException If this thread is interrupted while waiting for the upload to
* complete.
*/
public UploadResult waitForUploadResult() throws CosClientException, CosServiceException, InterruptedException {
try {
UploadResult result = null;
while (!monitor.isDone() || result == null) {
Future<?> f = monitor.getFuture();
result = (UploadResult) f.get();
}
return result;
} catch (ExecutionException e) {
rethrowExecutionException(e);
return null;
}
}
Aggregations