Search in sources :

Example 1 with UploadResult

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());
        }
    }
}
Also used : TransferManager(com.qcloud.cos.transfer.TransferManager) CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) Copy(com.qcloud.cos.transfer.Copy) MultipleFileUpload(com.qcloud.cos.transfer.MultipleFileUpload) Upload(com.qcloud.cos.transfer.Upload) UploadResult(com.qcloud.cos.model.UploadResult) File(java.io.File) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) MultipleFileDownload(com.qcloud.cos.transfer.MultipleFileDownload) Download(com.qcloud.cos.transfer.Download) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) Test(org.junit.Test)

Example 2 with UploadResult

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());
        }
    }
}
Also used : TransferManager(com.qcloud.cos.transfer.TransferManager) CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) Copy(com.qcloud.cos.transfer.Copy) MultipleFileUpload(com.qcloud.cos.transfer.MultipleFileUpload) Upload(com.qcloud.cos.transfer.Upload) UploadResult(com.qcloud.cos.model.UploadResult) File(java.io.File) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) MultipleFileDownload(com.qcloud.cos.transfer.MultipleFileDownload) Download(com.qcloud.cos.transfer.Download) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) Test(org.junit.Test)

Example 3 with UploadResult

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;
}
Also used : PutObjectResult(com.qcloud.cos.model.PutObjectResult) CompleteMultipartUploadResult(com.qcloud.cos.model.CompleteMultipartUploadResult) UploadResult(com.qcloud.cos.model.UploadResult)

Example 4 with 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;
}
Also used : CancellationException(java.util.concurrent.CancellationException) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) UploadPartRequest(com.qcloud.cos.model.UploadPartRequest) CompleteMultipartUploadResult(com.qcloud.cos.model.CompleteMultipartUploadResult) CompleteMultipartUploadResult(com.qcloud.cos.model.CompleteMultipartUploadResult) UploadResult(com.qcloud.cos.model.UploadResult) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) PartETag(com.qcloud.cos.model.PartETag) CompleteMultipartUploadRequest(com.qcloud.cos.model.CompleteMultipartUploadRequest)

Example 5 with 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;
    }
}
Also used : UploadResult(com.qcloud.cos.model.UploadResult) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

UploadResult (com.qcloud.cos.model.UploadResult)15 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)10 Upload (com.qcloud.cos.transfer.Upload)10 File (java.io.File)10 TransferManager (com.qcloud.cos.transfer.TransferManager)7 CosClientException (com.qcloud.cos.exception.CosClientException)6 CompleteMultipartUploadResult (com.qcloud.cos.model.CompleteMultipartUploadResult)6 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)5 MultipleFileUpload (com.qcloud.cos.transfer.MultipleFileUpload)5 CosServiceException (com.qcloud.cos.exception.CosServiceException)4 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)4 Download (com.qcloud.cos.transfer.Download)4 ExecutorService (java.util.concurrent.ExecutorService)4 COSClient (com.qcloud.cos.COSClient)3 ClientConfig (com.qcloud.cos.ClientConfig)3 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)3 COSCredentials (com.qcloud.cos.auth.COSCredentials)3 CopyObjectRequest (com.qcloud.cos.model.CopyObjectRequest)3 InitiateMultipartUploadResult (com.qcloud.cos.model.InitiateMultipartUploadResult)3 CIObject (com.qcloud.cos.model.ciModel.persistence.CIObject)3