Search in sources :

Example 1 with PersistableUpload

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

the class TransferManagerDemo method pauseUploadFileAndResume.

// 上传文件(上传过程中暂停, 并继续上传)
public static void pauseUploadFileAndResume() {
    // 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);
    // bucket名需包含appid
    String bucketName = "mybucket-1251668577";
    ExecutorService threadPool = Executors.newFixedThreadPool(4);
    // 传入一个threadpool, 若不传入线程池, 默认TransferManager中会生成一个单线程的线程池。
    TransferManager transferManager = new TransferManager(cosclient, threadPool);
    String key = "aaa/bbb.txt";
    File localFile = new File("src/test/resources/len30M.txt");
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
    try {
        // 返回一个异步结果Upload, 可同步的调用waitForUploadResult等待upload结束, 成功返回UploadResult, 失败抛出异常.
        Upload upload = transferManager.upload(putObjectRequest);
        Thread.sleep(10000);
        PersistableUpload persistableUpload = upload.pause();
        upload = transferManager.resumeUpload(persistableUpload);
        showTransferProgress(upload);
        UploadResult uploadResult = upload.waitForUploadResult();
        System.out.println(uploadResult.getETag());
    } 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) PersistableUpload(com.qcloud.cos.transfer.PersistableUpload) PersistableUpload(com.qcloud.cos.transfer.PersistableUpload) MultipleFileUpload(com.qcloud.cos.transfer.MultipleFileUpload) Upload(com.qcloud.cos.transfer.Upload) COSClient(com.qcloud.cos.COSClient) CosServiceException(com.qcloud.cos.exception.CosServiceException) ExecutorService(java.util.concurrent.ExecutorService) Region(com.qcloud.cos.region.Region) UploadResult(com.qcloud.cos.model.UploadResult) ClientConfig(com.qcloud.cos.ClientConfig) File(java.io.File) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Aggregations

COSClient (com.qcloud.cos.COSClient)1 ClientConfig (com.qcloud.cos.ClientConfig)1 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)1 COSCredentials (com.qcloud.cos.auth.COSCredentials)1 CosClientException (com.qcloud.cos.exception.CosClientException)1 CosServiceException (com.qcloud.cos.exception.CosServiceException)1 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)1 UploadResult (com.qcloud.cos.model.UploadResult)1 Region (com.qcloud.cos.region.Region)1 MultipleFileUpload (com.qcloud.cos.transfer.MultipleFileUpload)1 PersistableUpload (com.qcloud.cos.transfer.PersistableUpload)1 TransferManager (com.qcloud.cos.transfer.TransferManager)1 Upload (com.qcloud.cos.transfer.Upload)1 File (java.io.File)1 ExecutorService (java.util.concurrent.ExecutorService)1