Search in sources :

Example 11 with UploadPartRequest

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

the class ImagePersistenceDemo method persistenceImageWithMultipart.

public static void persistenceImageWithMultipart(COSClient cosClient) throws FileNotFoundException {
    // 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");
    InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, key);
    InitiateMultipartUploadResult initResult = cosClient.initiateMultipartUpload(request);
    String uploadId = initResult.getUploadId();
    // 上传分块
    List<PartETag> partETags = new LinkedList<>();
    UploadPartRequest uploadPartRequest = new UploadPartRequest();
    uploadPartRequest.setBucketName(bucketName);
    uploadPartRequest.setKey(key);
    uploadPartRequest.setUploadId(uploadId);
    // 设置分块的数据来源输入流
    uploadPartRequest.setInputStream(new FileInputStream(localFile));
    // 设置分块的长度
    // 设置数据长度
    uploadPartRequest.setPartSize(localFile.length());
    // 假设要上传的part编号是10
    uploadPartRequest.setPartNumber(1);
    UploadPartResult uploadPartResult = cosClient.uploadPart(uploadPartRequest);
    PartETag partETag = uploadPartResult.getPartETag();
    partETags.add(partETag);
    // 合并分块并带上图像处理参数
    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);
    CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
    completeMultipartUploadRequest.setPicOperations(picOperations);
    CompleteMultipartUploadResult completeMultipartUploadResult = cosClient.completeMultipartUpload(completeMultipartUploadRequest);
    // 获取图片处理的结果
    CIUploadResult ciUploadResult = completeMultipartUploadResult.getCiUploadResult();
    System.out.println(completeMultipartUploadResult.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 : InitiateMultipartUploadResult(com.qcloud.cos.model.InitiateMultipartUploadResult) PicOperations(com.qcloud.cos.model.ciModel.persistence.PicOperations) InitiateMultipartUploadRequest(com.qcloud.cos.model.InitiateMultipartUploadRequest) UploadPartRequest(com.qcloud.cos.model.UploadPartRequest) CIUploadResult(com.qcloud.cos.model.ciModel.persistence.CIUploadResult) CompleteMultipartUploadResult(com.qcloud.cos.model.CompleteMultipartUploadResult) PartETag(com.qcloud.cos.model.PartETag) LinkedList(java.util.LinkedList) FileInputStream(java.io.FileInputStream) UploadPartResult(com.qcloud.cos.model.UploadPartResult) File(java.io.File) CIObject(com.qcloud.cos.model.ciModel.persistence.CIObject) CompleteMultipartUploadRequest(com.qcloud.cos.model.CompleteMultipartUploadRequest)

Aggregations

UploadPartRequest (com.qcloud.cos.model.UploadPartRequest)11 PartETag (com.qcloud.cos.model.PartETag)7 UploadPartResult (com.qcloud.cos.model.UploadPartResult)7 CompleteMultipartUploadRequest (com.qcloud.cos.model.CompleteMultipartUploadRequest)6 CompleteMultipartUploadResult (com.qcloud.cos.model.CompleteMultipartUploadResult)6 InitiateMultipartUploadRequest (com.qcloud.cos.model.InitiateMultipartUploadRequest)5 LinkedList (java.util.LinkedList)5 CosClientException (com.qcloud.cos.exception.CosClientException)4 CosServiceException (com.qcloud.cos.exception.CosServiceException)4 InitiateMultipartUploadResult (com.qcloud.cos.model.InitiateMultipartUploadResult)4 CIObject (com.qcloud.cos.model.ciModel.persistence.CIObject)3 CIUploadResult (com.qcloud.cos.model.ciModel.persistence.CIUploadResult)3 PicOperations (com.qcloud.cos.model.ciModel.persistence.PicOperations)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 COSClient (com.qcloud.cos.COSClient)2 ClientConfig (com.qcloud.cos.ClientConfig)2 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)2 COSCredentials (com.qcloud.cos.auth.COSCredentials)2