Search in sources :

Example 1 with TransferManagerConfiguration

use of com.qcloud.cos.transfer.TransferManagerConfiguration 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)

Example 2 with TransferManagerConfiguration

use of com.qcloud.cos.transfer.TransferManagerConfiguration in project cos-java-sdk-v5 by tencentyun.

the class KMSEncryptionClientDemo method transferManagerDemo.

static void transferManagerDemo() {
    ExecutorService threadPool = Executors.newFixedThreadPool(32);
    // 传入一个threadpool, 若不传入线程池, 默认TransferManager中会生成一个单线程的线程池。
    TransferManager transferManager = new TransferManager(cosClient, threadPool);
    TransferManagerConfiguration transferManagerConfiguration = new TransferManagerConfiguration();
    transferManagerConfiguration.setMultipartUploadThreshold(1024 * 1024);
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, bigLocalFile);
    try {
        // 返回一个异步结果Upload, 可同步的调用waitForUploadResult等待upload结束, 成功返回UploadResult, 失败抛出异常.
        Upload upload = transferManager.upload(putObjectRequest);
        UploadResult uploadResult = upload.waitForUploadResult();
        System.out.println(uploadResult.getRequestId());
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
    try {
        // 返回一个异步结果Upload, 可同步的调用waitForUploadResult等待download结束, 成功返回DownloadResult, 失败抛出异常.
        Download download = transferManager.download(getObjectRequest, new File("downLen10m.txt"));
        download.waitForCompletion();
        System.out.println(download.getObjectMetadata().getRequestId());
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    transferManager.shutdownNow();
}
Also used : TransferManager(com.qcloud.cos.transfer.TransferManager) TransferManagerConfiguration(com.qcloud.cos.transfer.TransferManagerConfiguration) CosServiceException(com.qcloud.cos.exception.CosServiceException) CosClientException(com.qcloud.cos.exception.CosClientException) ExecutorService(java.util.concurrent.ExecutorService) Upload(com.qcloud.cos.transfer.Upload) UploadResult(com.qcloud.cos.model.UploadResult) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) Download(com.qcloud.cos.transfer.Download) File(java.io.File) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 3 with TransferManagerConfiguration

use of com.qcloud.cos.transfer.TransferManagerConfiguration in project simpleFS by shengdingbox.

the class QCloudOssApiClient method check.

@Override
protected void check() {
    // 自定义线程池大小,建议在客户端与 COS 网络充足(例如使用腾讯云的 CVM,同地域上传 COS)的情况下,设置成16或32即可,可较充分的利用网络资源
    // 对于使用公网传输且网络带宽质量不高的情况,建议减小该值,避免因网速过慢,造成请求超时。
    ExecutorService threadPool = Executors.newFixedThreadPool(32);
    // 传入一个 threadpool, 若不传入线程池,默认 TransferManager 中会生成一个单线程的线程池。
    TransferManager transferManager = new TransferManager(cosClient, threadPool);
    // 设置高级接口的配置项
    // 分块上传阈值和分块大小分别为 5MB 和 1MB
    TransferManagerConfiguration transferManagerConfiguration = new TransferManagerConfiguration();
    transferManagerConfiguration.setMultipartUploadThreshold(5 * 1024 * 1024);
    transferManagerConfiguration.setMinimumUploadPartSize(1 * 1024 * 1024);
    transferManager.setConfiguration(transferManagerConfiguration);
    this.transferManager = transferManager;
}
Also used : TransferManager(com.qcloud.cos.transfer.TransferManager) TransferManagerConfiguration(com.qcloud.cos.transfer.TransferManagerConfiguration) ExecutorService(java.util.concurrent.ExecutorService)

Example 4 with TransferManagerConfiguration

use of com.qcloud.cos.transfer.TransferManagerConfiguration in project cos-java-sdk-v5 by tencentyun.

the class TransferManagerDemo method copyFileSetMetadata.

public static void copyFileSetMetadata() {
    // 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-guangzhou"));
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    ClientConfig srcClientConfig = new ClientConfig(new Region("ap-shanghai"));
    COSClient srcCosclient = new COSClient(cred, srcClientConfig);
    ExecutorService threadPool = Executors.newFixedThreadPool(2);
    // 传入一个threadpool, 若不传入线程池, 默认TransferManager中会生成一个单线程的线程池。
    TransferManager transferManager = new TransferManager(cosclient, threadPool);
    TransferManagerConfiguration transferManagerConfiguration = new TransferManagerConfiguration();
    transferManagerConfiguration.setMultipartCopyThreshold(5 * 1024 * 1024);
    transferManager.setConfiguration(transferManagerConfiguration);
    // 要拷贝的bucket region, 支持跨园区拷贝
    Region srcBucketRegion = new Region("ap-shanghai");
    // 源bucket, bucket名需包含appid
    String srcBucketName = "mysrcbucket-123456789";
    // 要拷贝的源文件
    String srcKey = "aaa/bbb.txt";
    // 目的bucket, bucket名需包含appid
    String destBucketName = "mydestbucekt-123456789";
    // 要拷贝的目的文件
    String destKey = "bbb/ccc.txt";
    ObjectMetadata objectMetadata = new ObjectMetadata();
    Map<String, String> userMeta = new HashMap<String, String>();
    userMeta.put("usermeta", "hello-mult-copy");
    objectMetadata.setUserMetadata(userMeta);
    CopyObjectRequest copyObjectRequest = new CopyObjectRequest(srcBucketRegion, srcBucketName, srcKey, destBucketName, destKey);
    System.out.println(copyObjectRequest.getDestinationBucketName());
    copyObjectRequest.setNewObjectMetadata(objectMetadata);
    try {
        Copy copy = transferManager.copy(copyObjectRequest, srcCosclient, null);
        // 返回一个异步结果copy, 可同步的调用waitForCopyResult等待copy结束, 成功返回CopyResult, 失败抛出异常.
        // showTransferProgress(copy);
        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) HashMap(java.util.HashMap) 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) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) CopyResult(com.qcloud.cos.model.CopyResult)

Aggregations

TransferManager (com.qcloud.cos.transfer.TransferManager)4 TransferManagerConfiguration (com.qcloud.cos.transfer.TransferManagerConfiguration)4 ExecutorService (java.util.concurrent.ExecutorService)4 CosClientException (com.qcloud.cos.exception.CosClientException)3 CosServiceException (com.qcloud.cos.exception.CosServiceException)3 COSClient (com.qcloud.cos.COSClient)2 ClientConfig (com.qcloud.cos.ClientConfig)2 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)2 COSCredentials (com.qcloud.cos.auth.COSCredentials)2 CopyObjectRequest (com.qcloud.cos.model.CopyObjectRequest)2 CopyResult (com.qcloud.cos.model.CopyResult)2 Region (com.qcloud.cos.region.Region)2 Copy (com.qcloud.cos.transfer.Copy)2 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)1 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)1 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)1 UploadResult (com.qcloud.cos.model.UploadResult)1 Download (com.qcloud.cos.transfer.Download)1 Upload (com.qcloud.cos.transfer.Upload)1 File (java.io.File)1