Search in sources :

Example 1 with MultipleFileUpload

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

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

the class TransferManagerTest method testTransferManagerUploadLocalDir.

@Test
public void testTransferManagerUploadLocalDir() 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();
    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));
    } finally {
        if (localFile1.exists()) {
            assertTrue(localFile1.delete());
        }
        if (localFile2.exists()) {
            assertTrue(localFile2.delete());
        }
        clearObject(key1);
        clearObject(key2);
    }
}
Also used : File(java.io.File) MultipleFileUpload(com.qcloud.cos.transfer.MultipleFileUpload) Test(org.junit.Test)

Example 3 with MultipleFileUpload

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

the class TransferManagerDemo method uploadDirectory.

// 批量上传
public static void uploadDirectory() {
    // 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);
    // 设置文件上传到 bucket 之后的前缀目录,设置为 “”,表示上传到 bucket 的根目录
    String cos_path = "/prefix";
    // 要上传的文件夹的绝对路径
    String dir_path = "/to/mydir";
    // 是否递归上传目录下的子目录,如果是 true,子目录下的文件也会上传,且cos上会保持目录结构
    Boolean recursive = false;
    try {
        // 返回一个异步结果Upload, 可同步的调用waitForUploadResult等待upload结束, 成功返回UploadResult, 失败抛出异常.
        MultipleFileUpload upload = transferManager.uploadDirectory(bucketName, cos_path, new File(dir_path), recursive);
        // 可以选择查看上传进度
        showTransferProgress(upload);
        // 或者阻塞等待完成
        upload.waitForCompletion();
        System.out.println("upload 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) 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) MultipleFileUpload(com.qcloud.cos.transfer.MultipleFileUpload)

Aggregations

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