Search in sources :

Example 16 with PutObjectRequest

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

the class TemporyTokenDemo method UseTemporyTokenUploadAndDownload.

// 使用临时秘钥进行上传和下载
public static void UseTemporyTokenUploadAndDownload() {
    // 使用云api秘钥,可以获取一个临时secret id,secret key和session token,
    BasicSessionCredentials cred = getSessionCredential();
    // 设置区域, 这里设置为北京一区
    ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1"));
    // 生成cos客户端对象
    COSClient cosClient = new COSClient(cred, clientConfig);
    // 上传的bucket名字
    String bucketName = "rabbitliutj-1000000";
    // 上传object, 建议20M以下的文件使用该接口
    File localFile = new File("src/test/resources/len5M.txt");
    String key = "upload_single_demo5M.txt";
    // 上传
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
    ObjectMetadata objectMetadata = new ObjectMetadata();
    PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
    System.out.println(putObjectResult.getMetadata());
    // 下载
    File downFile = new File("src/test/resources/len5M_down.txt");
    GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
    ObjectMetadata downObjectMeta = cosClient.getObject(getObjectRequest, downFile);
    // 关闭客户端(关闭后台线程)
    cosClient.shutdown();
}
Also used : COSClient(com.qcloud.cos.COSClient) BasicSessionCredentials(com.qcloud.cos.auth.BasicSessionCredentials) PutObjectResult(com.qcloud.cos.model.PutObjectResult) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) File(java.io.File) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 17 with PutObjectRequest

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

the class TransferManagerDemo method multipartUploadWithMetaData.

public static void multipartUploadWithMetaData() {
    // 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-shanghai"));
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket名需包含appid
    String bucketName = "mybucket-1251668577";
    ExecutorService threadPool = Executors.newFixedThreadPool(2);
    // 传入一个threadpool, 若不传入线程池, 默认TransferManager中会生成一个单线程的线程池。
    TransferManager transferManager = new TransferManager(cosclient, threadPool);
    String key = "aaa/bbb.txt";
    File localFile = new File("src/test/resources/len20M.txt");
    ObjectMetadata objectMetadata = new ObjectMetadata();
    Map<String, String> userMeta = new HashMap<String, String>();
    userMeta.put("usermeta", "hello-mult");
    objectMetadata.setUserMetadata(userMeta);
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
    putObjectRequest.withMetadata(objectMetadata);
    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) HashMap(java.util.HashMap) 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) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 18 with PutObjectRequest

use of com.qcloud.cos.model.PutObjectRequest 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 19 with PutObjectRequest

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

the class ImagePersistenceDemo method persistenceImageWithTransferManager.

public static void persistenceImageWithTransferManager(TransferManager transferManager) throws InterruptedException {
    // bucket名需包含appid
    // api 请参考:https://cloud.tencent.com/document/product/436/46782
    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);
    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());
        System.out.println(ciObject.getEtag());
    }
}
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)

Example 20 with PutObjectRequest

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

Aggregations

PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)45 File (java.io.File)30 PutObjectResult (com.qcloud.cos.model.PutObjectResult)25 CosServiceException (com.qcloud.cos.exception.CosServiceException)17 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)17 CosClientException (com.qcloud.cos.exception.CosClientException)15 COSClient (com.qcloud.cos.COSClient)13 ClientConfig (com.qcloud.cos.ClientConfig)13 Region (com.qcloud.cos.region.Region)13 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)12 COSCredentials (com.qcloud.cos.auth.COSCredentials)12 Upload (com.qcloud.cos.transfer.Upload)12 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)10 UploadResult (com.qcloud.cos.model.UploadResult)10 ByteArrayInputStream (java.io.ByteArrayInputStream)8 LinkedList (java.util.LinkedList)8 Test (org.junit.Test)8 CIObject (com.qcloud.cos.model.ciModel.persistence.CIObject)7 CIUploadResult (com.qcloud.cos.model.ciModel.persistence.CIUploadResult)7 PicOperations (com.qcloud.cos.model.ciModel.persistence.PicOperations)7