Search in sources :

Example 1 with MultipleFileDownload

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);
    }
}
Also used : MultipleFileDownload(com.qcloud.cos.transfer.MultipleFileDownload) File(java.io.File) MultipleFileUpload(com.qcloud.cos.transfer.MultipleFileUpload) Test(org.junit.Test)

Example 2 with MultipleFileDownload

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();
}
Also used : TransferManager(com.qcloud.cos.transfer.TransferManager) MultipleFileDownload(com.qcloud.cos.transfer.MultipleFileDownload) 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) CosServiceException(com.qcloud.cos.exception.CosServiceException) ExecutorService(java.util.concurrent.ExecutorService) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) File(java.io.File)

Aggregations

MultipleFileDownload (com.qcloud.cos.transfer.MultipleFileDownload)2 File (java.io.File)2 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 Region (com.qcloud.cos.region.Region)1 MultipleFileUpload (com.qcloud.cos.transfer.MultipleFileUpload)1 TransferManager (com.qcloud.cos.transfer.TransferManager)1 ExecutorService (java.util.concurrent.ExecutorService)1 Test (org.junit.Test)1