use of com.qcloud.cos.transfer.PersistableDownload in project cos-java-sdk-v5 by tencentyun.
the class TransferManagerDemo method pauseDownloadFileAndResume.
// 将文件下载到本地(中途暂停并继续开始)
public static void pauseDownloadFileAndResume() {
// 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(32);
// 传入一个threadpool, 若不传入线程池, 默认TransferManager中会生成一个单线程的线程池。
TransferManager transferManager = new TransferManager(cosclient, threadPool);
String key = "aaa/bbb.txt";
File downloadFile = new File("src/test/resources/download.txt");
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
try {
// 返回一个异步结果copy, 可同步的调用waitForCompletion等待download结束, 成功返回void, 失败抛出异常.
Download download = transferManager.download(getObjectRequest, downloadFile);
Thread.sleep(5000L);
PersistableDownload persistableDownload = download.pause();
download = transferManager.resumeDownload(persistableDownload);
showTransferProgress(download);
download.waitForCompletion();
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
transferManager.shutdownNow();
cosclient.shutdown();
}
Aggregations