Search in sources :

Example 1 with CopyResult

use of com.qcloud.cos.model.CopyResult in project cos-java-sdk-v5 by tencentyun.

the class TransferManagerTest method testTransferManagerCopySmallFileFromSameRegion.

// transfer manager对相同园区使用put object copy
@Ignore
public void testTransferManagerCopySmallFileFromSameRegion() throws CosServiceException, CosClientException, InterruptedException {
    if (!judgeUserInfoValid()) {
        return;
    }
    COSCredentials srcCred = new BasicCOSCredentials(secretId, secretKey);
    String srcRegion = region;
    ClientConfig srcClientConfig = new ClientConfig(new Region(srcRegion));
    COSClient srcCOSClient = new COSClient(srcCred, srcClientConfig);
    String srcBucketName = bucket;
    String srcKey = "ut_copy/len1G.txt";
    String destKey = "ut_copy_dest/len1G_2.txt";
    CopyObjectRequest copyObjectRequest = new CopyObjectRequest(srcBucketName, srcKey, bucket, destKey);
    Copy copy = transferManager.copy(copyObjectRequest, srcCOSClient, null);
    CopyResult copyResult = copy.waitForCopyResult();
    assertNotNull(copyResult.getRequestId());
    assertNotNull(copyResult.getDateStr());
}
Also used : CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Copy(com.qcloud.cos.transfer.Copy) Region(com.qcloud.cos.region.Region) CopyResult(com.qcloud.cos.model.CopyResult) Ignore(org.junit.Ignore)

Example 2 with CopyResult

use of com.qcloud.cos.model.CopyResult in project cos-java-sdk-v5 by tencentyun.

the class CompleteMultipartCopy method call.

@Override
public CopyResult call() throws Exception {
    CompleteMultipartUploadResult res;
    try {
        CompleteMultipartUploadRequest req = new CompleteMultipartUploadRequest(origReq.getDestinationBucketName(), origReq.getDestinationKey(), uploadId, collectPartETags()).withGeneralProgressListener(origReq.getGeneralProgressListener());
        ObjectMetadata origMeta = origReq.getNewObjectMetadata();
        if (origMeta != null) {
            ObjectMetadata objMeta = req.getObjectMetadata();
            if (objMeta == null) {
                objMeta = new ObjectMetadata();
            }
            objMeta.setUserMetadata(origMeta.getUserMetadata());
            req.setObjectMetadata(objMeta);
        }
        TransferManagerUtils.populateEndpointAddr(origReq, req);
        res = cos.completeMultipartUpload(req);
    } catch (Exception e) {
        publishProgress(listener, ProgressEventType.TRANSFER_FAILED_EVENT);
        throw e;
    }
    CopyResult copyResult = new CopyResult();
    copyResult.setSourceBucketName(origReq.getSourceBucketName());
    copyResult.setSourceKey(origReq.getSourceKey());
    copyResult.setDestinationBucketName(res.getBucketName());
    copyResult.setDestinationKey(res.getKey());
    copyResult.setETag(res.getETag());
    copyResult.setVersionId(res.getVersionId());
    copyResult.setRequestId(res.getRequestId());
    copyResult.setDateStr(res.getDateStr());
    copyResult.setCrc64Ecma(res.getCrc64Ecma());
    monitor.copyComplete();
    return copyResult;
}
Also used : CompleteMultipartUploadResult(com.qcloud.cos.model.CompleteMultipartUploadResult) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) CosClientException(com.qcloud.cos.exception.CosClientException) CompleteMultipartUploadRequest(com.qcloud.cos.model.CompleteMultipartUploadRequest) CopyResult(com.qcloud.cos.model.CopyResult)

Example 3 with CopyResult

use of com.qcloud.cos.model.CopyResult in project cos-java-sdk-v5 by tencentyun.

the class CopyCallable method copyInOneChunk.

/**
 * Performs the copy of the COS object from source bucket to destination bucket. The COS object
 * is copied to destination in one single request.
 *
 * @returns CopyResult response information from the server.
 */
private CopyResult copyInOneChunk() {
    CopyObjectResult copyObjectResult = cos.copyObject(copyObjectRequest);
    if (copyObjectResult == null) {
        return null;
    }
    CopyResult copyResult = new CopyResult();
    copyResult.setSourceBucketName(copyObjectRequest.getSourceBucketName());
    copyResult.setSourceKey(copyObjectRequest.getSourceKey());
    copyResult.setDestinationBucketName(copyObjectRequest.getDestinationBucketName());
    copyResult.setDestinationKey(copyObjectRequest.getDestinationKey());
    copyResult.setETag(copyObjectResult.getETag());
    copyResult.setVersionId(copyObjectResult.getVersionId());
    copyResult.setRequestId(copyObjectResult.getRequestId());
    copyResult.setDateStr(copyObjectResult.getDateStr());
    copyResult.setCrc64Ecma(copyObjectResult.getCrc64Ecma());
    return copyResult;
}
Also used : CopyObjectResult(com.qcloud.cos.model.CopyObjectResult) CopyResult(com.qcloud.cos.model.CopyResult)

Example 4 with CopyResult

use of com.qcloud.cos.model.CopyResult in project cos-java-sdk-v5 by tencentyun.

the class CopyMonitor method call.

@Override
public CopyResult call() throws Exception {
    try {
        CopyResult result = multipartCopyCallable.call();
        if (result == null) {
            futures.addAll(multipartCopyCallable.getFutures());
            futureReference.set(threadPool.submit(new CompleteMultipartCopy(multipartCopyCallable.getMultipartUploadId(), cos, origReq, futures, listener, this)));
        } else {
            copyComplete();
        }
        return result;
    } catch (CancellationException e) {
        transfer.setState(TransferState.Canceled);
        publishProgress(listener, ProgressEventType.TRANSFER_CANCELED_EVENT);
        throw new CosClientException("Upload canceled");
    } catch (Exception e) {
        transfer.setState(TransferState.Failed);
        publishProgress(listener, ProgressEventType.TRANSFER_FAILED_EVENT);
        throw e;
    }
}
Also used : CancellationException(java.util.concurrent.CancellationException) CosClientException(com.qcloud.cos.exception.CosClientException) CosClientException(com.qcloud.cos.exception.CosClientException) CancellationException(java.util.concurrent.CancellationException) CopyResult(com.qcloud.cos.model.CopyResult)

Example 5 with CopyResult

use of com.qcloud.cos.model.CopyResult in project cos-java-sdk-v5 by tencentyun.

the class TransferManagerDemo method copyFileForSameRegion.

// copy接口支持根据文件大小自动选择copy或者分块copy
// 以下代码展示同园区拷贝, 即将同园区的文件拷贝到另一个园区
public static void copyFileForSameRegion() {
    // 1 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
    // 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1"));
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    ExecutorService threadPool = Executors.newFixedThreadPool(32);
    // 传入一个threadpool, 若不传入线程池, 默认TransferManager中会生成一个单线程的线程池。
    TransferManager transferManager = new TransferManager(cosclient, threadPool);
    TransferManagerConfiguration transferManagerConfiguration = new TransferManagerConfiguration();
    transferManagerConfiguration.setMultipartCopyThreshold(20 * 1024 * 1024);
    transferManager.setConfiguration(transferManagerConfiguration);
    // 要拷贝的bucket region, 支持跨园区拷贝
    Region srcBucketRegion = new Region("ap-beijing-1");
    // 源bucket, bucket名需包含appid
    String srcBucketName = "srcBucket-1251668577";
    // 要拷贝的源文件
    String srcKey = "aaa/bbb.txt";
    // 目的bucket, bucket名需包含appid
    String destBucketName = "destBucket-1251668577";
    // 要拷贝的目的文件
    String destKey = "ccc/ddd.txt";
    CopyObjectRequest copyObjectRequest = new CopyObjectRequest(srcBucketRegion, srcBucketName, srcKey, destBucketName, destKey);
    try {
        Copy copy = transferManager.copy(copyObjectRequest);
        // 返回一个异步结果copy, 可同步的调用waitForCopyResult等待copy结束, 成功返回CopyResult, 失败抛出异常.
        CopyResult copyResult = copy.waitForCopyResult();
        System.out.println(copyResult.getCrc64Ecma());
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    transferManager.shutdownNow();
    cosclient.shutdown();
}
Also used : TransferManager(com.qcloud.cos.transfer.TransferManager) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosClientException(com.qcloud.cos.exception.CosClientException) COSClient(com.qcloud.cos.COSClient) CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) TransferManagerConfiguration(com.qcloud.cos.transfer.TransferManagerConfiguration) CosServiceException(com.qcloud.cos.exception.CosServiceException) Copy(com.qcloud.cos.transfer.Copy) ExecutorService(java.util.concurrent.ExecutorService) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) CopyResult(com.qcloud.cos.model.CopyResult)

Aggregations

CopyResult (com.qcloud.cos.model.CopyResult)11 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)7 COSCredentials (com.qcloud.cos.auth.COSCredentials)7 CopyObjectRequest (com.qcloud.cos.model.CopyObjectRequest)7 Region (com.qcloud.cos.region.Region)7 Copy (com.qcloud.cos.transfer.Copy)7 CosClientException (com.qcloud.cos.exception.CosClientException)6 COSClient (com.qcloud.cos.COSClient)4 ClientConfig (com.qcloud.cos.ClientConfig)4 CosServiceException (com.qcloud.cos.exception.CosServiceException)4 TransferManager (com.qcloud.cos.transfer.TransferManager)4 ExecutorService (java.util.concurrent.ExecutorService)4 Ignore (org.junit.Ignore)3 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)2 TransferManagerConfiguration (com.qcloud.cos.transfer.TransferManagerConfiguration)2 CompleteMultipartUploadRequest (com.qcloud.cos.model.CompleteMultipartUploadRequest)1 CompleteMultipartUploadResult (com.qcloud.cos.model.CompleteMultipartUploadResult)1 CopyObjectResult (com.qcloud.cos.model.CopyObjectResult)1 HashMap (java.util.HashMap)1 CancellationException (java.util.concurrent.CancellationException)1