Search in sources :

Example 11 with UploadResult

use of com.qcloud.cos.model.UploadResult in project cos-java-sdk-v5 by tencentyun.

the class KMSEncryptionClientDemo method transferManagerDemo.

static void transferManagerDemo() {
    ExecutorService threadPool = Executors.newFixedThreadPool(32);
    // 传入一个threadpool, 若不传入线程池, 默认TransferManager中会生成一个单线程的线程池。
    TransferManager transferManager = new TransferManager(cosClient, threadPool);
    TransferManagerConfiguration transferManagerConfiguration = new TransferManagerConfiguration();
    transferManagerConfiguration.setMultipartUploadThreshold(1024 * 1024);
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, bigLocalFile);
    try {
        // 返回一个异步结果Upload, 可同步的调用waitForUploadResult等待upload结束, 成功返回UploadResult, 失败抛出异常.
        Upload upload = transferManager.upload(putObjectRequest);
        UploadResult uploadResult = upload.waitForUploadResult();
        System.out.println(uploadResult.getRequestId());
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
    try {
        // 返回一个异步结果Upload, 可同步的调用waitForUploadResult等待download结束, 成功返回DownloadResult, 失败抛出异常.
        Download download = transferManager.download(getObjectRequest, new File("downLen10m.txt"));
        download.waitForCompletion();
        System.out.println(download.getObjectMetadata().getRequestId());
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    transferManager.shutdownNow();
}
Also used : TransferManager(com.qcloud.cos.transfer.TransferManager) TransferManagerConfiguration(com.qcloud.cos.transfer.TransferManagerConfiguration) CosServiceException(com.qcloud.cos.exception.CosServiceException) CosClientException(com.qcloud.cos.exception.CosClientException) ExecutorService(java.util.concurrent.ExecutorService) Upload(com.qcloud.cos.transfer.Upload) UploadResult(com.qcloud.cos.model.UploadResult) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) Download(com.qcloud.cos.transfer.Download) File(java.io.File) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 12 with UploadResult

use of com.qcloud.cos.model.UploadResult in project cos-java-sdk-v5 by tencentyun.

the class AbstractCOSEncryptionClientTest method testTransferManagerUploadDownBigFile.

@Test
public void testTransferManagerUploadDownBigFile() throws IOException, CosServiceException, CosClientException, InterruptedException {
    if (!judgeUserInfoValid()) {
        return;
    }
    TransferManager transferManager = new TransferManager(cosclient);
    File localFile = buildTestFile(1024 * 1024 * 10L);
    File downFile = new File(localFile.getAbsolutePath() + ".down");
    String key = "ut/" + localFile.getName();
    String destKey = key + ".copy";
    try {
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, localFile);
        ObjectMetadata objectMetadata = new ObjectMetadata();
        putObjectRequest.setMetadata(objectMetadata);
        Upload upload = transferManager.upload(putObjectRequest);
        UploadResult uploadResult = upload.waitForUploadResult();
        assertTrue(uploadResult.getETag().contains("-"));
        assertNotNull(uploadResult.getRequestId());
        assertNotNull(uploadResult.getDateStr());
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
        Download download = transferManager.download(getObjectRequest, downFile);
        download.waitForCompletion();
        // check file
        assertEquals(Md5Utils.md5Hex(localFile), Md5Utils.md5Hex(downFile));
        CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucket, key, bucket, destKey);
        Copy copy = transferManager.copy(copyObjectRequest, cosclient, null);
        copy.waitForCompletion();
    } finally {
        // delete file on cos
        clearObject(key);
        // delete file on cos
        clearObject(destKey);
        if (localFile.exists()) {
            assertTrue(localFile.delete());
        }
        if (downFile.exists()) {
            assertTrue(downFile.delete());
        }
    }
}
Also used : TransferManager(com.qcloud.cos.transfer.TransferManager) CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) Copy(com.qcloud.cos.transfer.Copy) Upload(com.qcloud.cos.transfer.Upload) UploadResult(com.qcloud.cos.model.UploadResult) File(java.io.File) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) Download(com.qcloud.cos.transfer.Download) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) Test(org.junit.Test)

Example 13 with UploadResult

use of com.qcloud.cos.model.UploadResult in project cos-java-sdk-v5 by tencentyun.

the class TransferManagerDemo method uploadFile.

// 上传文件, 根据文件大小自动选择简单上传或者分块上传。
public static void uploadFile() {
    // 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"));
    clientConfig.setHttpProtocol(HttpProtocol.https);
    // 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 localFile = new File("src/test/resources/len30M.txt");
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
    try {
        // 返回一个异步结果Upload, 可同步的调用waitForUploadResult等待upload结束, 成功返回UploadResult, 失败抛出异常.
        long startTime = System.currentTimeMillis();
        Upload upload = transferManager.upload(putObjectRequest);
        showTransferProgress(upload);
        UploadResult uploadResult = upload.waitForUploadResult();
        long endTime = System.currentTimeMillis();
        System.out.println("used time: " + (endTime - startTime) / 1000);
        System.out.println(uploadResult.getETag());
        System.out.println(uploadResult.getCrc64Ecma());
    } 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) 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)

Example 14 with UploadResult

use of com.qcloud.cos.model.UploadResult 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)

Example 15 with UploadResult

use of com.qcloud.cos.model.UploadResult in project cos-java-sdk-v5 by tencentyun.

the class BlindWatermarkDemo method addBlindWatermarkWithTransferManager.

public static void addBlindWatermarkWithTransferManager(TransferManager transferManager) throws InterruptedException {
    // bucket名需包含appid
    // api 请参考:https://cloud.tencent.com/document/product/436/46782
    String bucketName = "examplebucket-1250000000";
    String key = "qrcode.png";
    File localFile = new File("E://qrcode.png");
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
    PicOperations picOperations = new PicOperations();
    picOperations.setIsPicInfo(1);
    List<PicOperations.Rule> ruleList = new LinkedList<>();
    PicOperations.Rule rule1 = new PicOperations.Rule();
    rule1.setBucket(bucketName);
    rule1.setFileId("qrcode-watermark.png");
    rule1.setRule("watermark/3/type/3/text/dGVuY2VudCBjbG91ZA==");
    ruleList.add(rule1);
    picOperations.setRules(ruleList);
    putObjectRequest.setPicOperations(picOperations);
    Upload upload = transferManager.upload(putObjectRequest);
    UploadResult uploadResult = upload.waitForUploadResult();
    CIUploadResult ciUploadResult = uploadResult.getCiUploadResult();
    System.out.println(uploadResult.getRequestId());
    System.out.println(ciUploadResult.getOriginalInfo().getEtag());
    for (CIObject ciObject : ciUploadResult.getProcessResults().getObjectList()) {
        System.out.println(ciObject.getLocation());
    }
}
Also used : PicOperations(com.qcloud.cos.model.ciModel.persistence.PicOperations) Upload(com.qcloud.cos.transfer.Upload) CIUploadResult(com.qcloud.cos.model.ciModel.persistence.CIUploadResult) CompleteMultipartUploadResult(com.qcloud.cos.model.CompleteMultipartUploadResult) UploadResult(com.qcloud.cos.model.UploadResult) CIUploadResult(com.qcloud.cos.model.ciModel.persistence.CIUploadResult) InitiateMultipartUploadResult(com.qcloud.cos.model.InitiateMultipartUploadResult) File(java.io.File) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) LinkedList(java.util.LinkedList) CIObject(com.qcloud.cos.model.ciModel.persistence.CIObject)

Aggregations

UploadResult (com.qcloud.cos.model.UploadResult)15 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)10 Upload (com.qcloud.cos.transfer.Upload)10 File (java.io.File)10 TransferManager (com.qcloud.cos.transfer.TransferManager)7 CosClientException (com.qcloud.cos.exception.CosClientException)6 CompleteMultipartUploadResult (com.qcloud.cos.model.CompleteMultipartUploadResult)6 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)5 MultipleFileUpload (com.qcloud.cos.transfer.MultipleFileUpload)5 CosServiceException (com.qcloud.cos.exception.CosServiceException)4 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)4 Download (com.qcloud.cos.transfer.Download)4 ExecutorService (java.util.concurrent.ExecutorService)4 COSClient (com.qcloud.cos.COSClient)3 ClientConfig (com.qcloud.cos.ClientConfig)3 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)3 COSCredentials (com.qcloud.cos.auth.COSCredentials)3 CopyObjectRequest (com.qcloud.cos.model.CopyObjectRequest)3 InitiateMultipartUploadResult (com.qcloud.cos.model.InitiateMultipartUploadResult)3 CIObject (com.qcloud.cos.model.ciModel.persistence.CIObject)3