use of com.qcloud.cos.transfer.MultipleFileDownload in project cos-java-sdk-v5 by tencentyun.
the class TransferManagerTest method testTransferManagerUploadDownloadDir.
@Test
public void testTransferManagerUploadDownloadDir() throws IOException, CosServiceException, CosClientException, InterruptedException {
if (!judgeUserInfoValid()) {
return;
}
String folderPrefix = "ut_uploaddir/";
File localFile1 = buildTestFile(1L);
File localFile2 = buildTestFile(1024L);
String key1 = folderPrefix + localFile1.getName();
String key2 = folderPrefix + localFile2.getName();
String downloadDirName = "ut_download_dir";
File downloaddir = new File(downloadDirName);
if (!downloaddir.exists()) {
downloaddir.mkdir();
}
File downloadFile1 = new File(downloadDirName + "/" + folderPrefix + localFile1.getName());
File downloadFile2 = new File(downloadDirName + "/" + folderPrefix + localFile2.getName());
try {
MultipleFileUpload multipleFileUpload = transferManager.uploadDirectory(bucket, folderPrefix, tmpDir, true);
multipleFileUpload.waitForCompletion();
headSimpleObject(key1, localFile1.length(), Md5Utils.md5Hex(localFile1));
headSimpleObject(key2, localFile2.length(), Md5Utils.md5Hex(localFile2));
MultipleFileDownload multipleFileDownload = transferManager.downloadDirectory(bucket, folderPrefix, downloaddir);
multipleFileDownload.waitForCompletion();
assertTrue(downloadFile1.exists());
assertTrue(downloadFile2.exists());
assertEquals(Md5Utils.md5Hex(localFile1), Md5Utils.md5Hex(downloadFile1));
assertEquals(Md5Utils.md5Hex(localFile2), Md5Utils.md5Hex(downloadFile2));
} finally {
if (localFile1.exists()) {
assertTrue(localFile1.delete());
}
if (localFile2.exists()) {
assertTrue(localFile2.delete());
}
if (downloadFile1.exists()) {
assertTrue(downloadFile1.delete());
}
if (downloadFile2.exists()) {
assertTrue(downloadFile2.delete());
}
clearObject(key1);
clearObject(key2);
deleteDir(downloaddir);
}
}
use of com.qcloud.cos.transfer.MultipleFileDownload in project cos-java-sdk-v5 by tencentyun.
the class TransferManagerDemo method downloadDirectory.
// 批量下载
public static void downloadDirectory() {
// 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);
// 设置要下载的对象的前缀(相当于cos上的一个目录),如果设置成 "",则下载整个 bucket。
String cos_path = "/prefix";
// 要保存下载的文件的文件夹的绝对路径
String dir_path = "/to/mydir";
try {
// 返回一个异步结果download, 可同步的调用waitForUploadResult等待download结束.
MultipleFileDownload download = transferManager.downloadDirectory(bucketName, cos_path, new File(dir_path));
// 可以选择查看下载进度
showTransferProgress(download);
// 或者阻塞等待完成
download.waitForCompletion();
System.out.println("download directory done.");
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
transferManager.shutdownNow();
cosclient.shutdown();
}
Aggregations