Search in sources :

Example 36 with CosServiceException

use of com.qcloud.cos.exception.CosServiceException 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)

Example 37 with CosServiceException

use of com.qcloud.cos.exception.CosServiceException in project cos-java-sdk-v5 by tencentyun.

the class ImagePersistenceDemo method persistenceImage.

public static void persistenceImage(COSClient cosClient) {
    // bucket名需包含appid
    // api 请参考 https://cloud.tencent.com/document/product/436/54050
    String bucketName = "examplebucket-1250000000";
    String key = "test.jpg";
    File localFile = new File("E://test.jpg");
    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("test-1.jpg");
    rule1.setRule("imageMogr2/rotate/90");
    ruleList.add(rule1);
    PicOperations.Rule rule2 = new PicOperations.Rule();
    rule2.setBucket(bucketName);
    rule2.setFileId("test-2.jpg");
    rule2.setRule("imageMogr2/rotate/180");
    ruleList.add(rule2);
    picOperations.setRules(ruleList);
    putObjectRequest.setPicOperations(picOperations);
    try {
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        CIUploadResult ciUploadResult = putObjectResult.getCiUploadResult();
        System.out.println(putObjectResult.getRequestId());
        System.out.println(ciUploadResult.getOriginalInfo().getEtag());
        for (CIObject ciObject : ciUploadResult.getProcessResults().getObjectList()) {
            System.out.println(ciObject.getLocation());
            System.out.println(ciObject.getEtag());
        }
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    }
}
Also used : PutObjectResult(com.qcloud.cos.model.PutObjectResult) CosClientException(com.qcloud.cos.exception.CosClientException) PicOperations(com.qcloud.cos.model.ciModel.persistence.PicOperations) CIUploadResult(com.qcloud.cos.model.ciModel.persistence.CIUploadResult) LinkedList(java.util.LinkedList) CosServiceException(com.qcloud.cos.exception.CosServiceException) File(java.io.File) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) CIObject(com.qcloud.cos.model.ciModel.persistence.CIObject)

Example 38 with CosServiceException

use of com.qcloud.cos.exception.CosServiceException in project cos-java-sdk-v5 by tencentyun.

the class ImagePersistenceDemo method persistenceImagePost.

public static void persistenceImagePost(COSClient cosClient) {
    String bucketName = "examplebucket-1250000000";
    String key = "test.jpg";
    ImageProcessRequest imageReq = new ImageProcessRequest(bucketName, key);
    PicOperations picOperations = new PicOperations();
    picOperations.setIsPicInfo(1);
    List<PicOperations.Rule> ruleList = new LinkedList<>();
    PicOperations.Rule rule1 = new PicOperations.Rule();
    rule1.setBucket(bucketName);
    rule1.setFileId("test-1.jpg");
    rule1.setRule("imageMogr2/rotate/90");
    ruleList.add(rule1);
    PicOperations.Rule rule2 = new PicOperations.Rule();
    rule2.setBucket(bucketName);
    rule2.setFileId("test-2.jpg");
    rule2.setRule("imageMogr2/rotate/180");
    ruleList.add(rule2);
    picOperations.setRules(ruleList);
    imageReq.setPicOperations(picOperations);
    try {
        CIUploadResult ciUploadResult = cosClient.processImage(imageReq);
        System.out.println(ciUploadResult.getOriginalInfo().getEtag());
        for (CIObject ciObject : ciUploadResult.getProcessResults().getObjectList()) {
            System.out.println(ciObject.getLocation());
            System.out.println(ciObject.getEtag());
        }
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    }
}
Also used : CosServiceException(com.qcloud.cos.exception.CosServiceException) CosClientException(com.qcloud.cos.exception.CosClientException) PicOperations(com.qcloud.cos.model.ciModel.persistence.PicOperations) CIUploadResult(com.qcloud.cos.model.ciModel.persistence.CIUploadResult) ImageProcessRequest(com.qcloud.cos.model.ciModel.common.ImageProcessRequest) LinkedList(java.util.LinkedList) CIObject(com.qcloud.cos.model.ciModel.persistence.CIObject)

Example 39 with CosServiceException

use of com.qcloud.cos.exception.CosServiceException in project cos-java-sdk-v5 by tencentyun.

the class QRCodeDemo method identifyQrCode.

public static void identifyQrCode(COSClient cosClient) {
    // bucket名需包含appid
    // api 请参考 https://cloud.tencent.com/document/product/436/54070
    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-1.png");
    rule1.setRule("QRcode/cover/1");
    ruleList.add(rule1);
    picOperations.setRules(ruleList);
    putObjectRequest.setPicOperations(picOperations);
    try {
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        CIUploadResult ciUploadResult = putObjectResult.getCiUploadResult();
        System.out.println(putObjectResult.getRequestId());
        System.out.println(ciUploadResult.getOriginalInfo().getEtag());
        for (CIObject ciObject : ciUploadResult.getProcessResults().getObjectList()) {
            System.out.println(ciObject.getLocation());
        }
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    }
}
Also used : PutObjectResult(com.qcloud.cos.model.PutObjectResult) CosClientException(com.qcloud.cos.exception.CosClientException) PicOperations(com.qcloud.cos.model.ciModel.persistence.PicOperations) CIUploadResult(com.qcloud.cos.model.ciModel.persistence.CIUploadResult) LinkedList(java.util.LinkedList) CosServiceException(com.qcloud.cos.exception.CosServiceException) File(java.io.File) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) CIObject(com.qcloud.cos.model.ciModel.persistence.CIObject)

Example 40 with CosServiceException

use of com.qcloud.cos.exception.CosServiceException in project cos-java-sdk-v5 by tencentyun.

the class BlindWatermarkDemo method addBlindWatermarkToExistImage.

public static void addBlindWatermarkToExistImage(COSClient cosClient) {
    // bucket名需包含appid
    // api 请参考:https://cloud.tencent.com/document/product/436/46782
    String bucketName = "examplebucket-1250000000";
    String key = "image/dog.jpg";
    ImageProcessRequest imageProcessRequest = new ImageProcessRequest(bucketName, key);
    PicOperations picOperations = new PicOperations();
    picOperations.setIsPicInfo(1);
    List<PicOperations.Rule> ruleList = new LinkedList<>();
    PicOperations.Rule rule = new PicOperations.Rule();
    rule.setBucket(bucketName);
    rule.setFileId("/image/result/dog.jpg");
    // 使用盲水印功能,水印图的宽高不得超过原图的1/8
    rule.setRule("watermark/3/type/2/image/aHR0cDovL2V4YW1wbGVidWNrZXQtMTI1MDAwMDAwMC5jb3MuYXAtZ3Vhbmd6aG91Lm15cWNsb3VkLmNvbS9zaHVpeWluLnBuZw==");
    ruleList.add(rule);
    picOperations.setRules(ruleList);
    imageProcessRequest.setPicOperations(picOperations);
    try {
        CIUploadResult ciUploadResult = cosClient.processImage(imageProcessRequest);
        System.out.println(ciUploadResult.getOriginalInfo().getEtag());
        for (CIObject ciObject : ciUploadResult.getProcessResults().getObjectList()) {
            System.out.println(ciObject.getLocation());
        }
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    }
}
Also used : CosServiceException(com.qcloud.cos.exception.CosServiceException) CosClientException(com.qcloud.cos.exception.CosClientException) PicOperations(com.qcloud.cos.model.ciModel.persistence.PicOperations) CIUploadResult(com.qcloud.cos.model.ciModel.persistence.CIUploadResult) ImageProcessRequest(com.qcloud.cos.model.ciModel.common.ImageProcessRequest) LinkedList(java.util.LinkedList) CIObject(com.qcloud.cos.model.ciModel.persistence.CIObject)

Aggregations

CosServiceException (com.qcloud.cos.exception.CosServiceException)82 CosClientException (com.qcloud.cos.exception.CosClientException)64 COSClient (com.qcloud.cos.COSClient)37 ClientConfig (com.qcloud.cos.ClientConfig)37 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)37 COSCredentials (com.qcloud.cos.auth.COSCredentials)37 Region (com.qcloud.cos.region.Region)37 File (java.io.File)28 IOException (java.io.IOException)20 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)15 TransferManager (com.qcloud.cos.transfer.TransferManager)14 ExecutorService (java.util.concurrent.ExecutorService)14 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)13 ByteArrayInputStream (java.io.ByteArrayInputStream)13 PutObjectResult (com.qcloud.cos.model.PutObjectResult)12 MultiObjectDeleteException (com.qcloud.cos.exception.MultiObjectDeleteException)11 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)11 ArrayList (java.util.ArrayList)10 LinkedList (java.util.LinkedList)10 Test (org.junit.Test)10