Search in sources :

Example 1 with InitiateMultipartUploadRequest

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

the class KmsUploadDemo method MultipartUploadWithKmsMeta.

public static void MultipartUploadWithKmsMeta() {
    COSCredentials cred = new BasicCOSCredentials("SECRET_ID", "SECRET_KEY");
    // 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
    // 设置使用https请求
    clientConfig.setHttpProtocol(HttpProtocol.https);
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket名需包含appid
    String bucketName = "mybucket-1251668577";
    String key = "aaa/bbb.txt";
    String kmsKeyId = "your-kms-key-id";
    String encryptionContext = Base64.encodeAsString("{\"Ssekmstest\":\"Ssekmstest\"}".getBytes());
    InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(bucketName, key);
    SSECOSKeyManagementParams ssecosKeyManagementParams = new SSECOSKeyManagementParams(kmsKeyId, encryptionContext);
    // 服务端加密场景下,返回的etag不再代表文件的md5,所以需要去掉客户端的md5校验
    // 如有需要,可获取crc64,自行校验
    System.setProperty(SkipMd5CheckStrategy.DISABLE_PUT_OBJECT_MD5_VALIDATION_PROPERTY, "true");
    initiateMultipartUploadRequest.setSSECOSKeyManagementParams(ssecosKeyManagementParams);
    try {
        InitiateMultipartUploadResult initiateMultipartUploadResult = cosclient.initiateMultipartUpload(initiateMultipartUploadRequest);
        List<PartETag> partETags = new LinkedList<>();
        for (int i = 0; i < 2; i++) {
            byte[] data = new byte[1024 * 1024];
            UploadPartRequest uploadPartRequest = new UploadPartRequest();
            uploadPartRequest.setBucketName(bucketName);
            uploadPartRequest.setKey(key);
            uploadPartRequest.setUploadId(initiateMultipartUploadResult.getUploadId());
            // 设置分块的数据来源输入流
            uploadPartRequest.setInputStream(new ByteArrayInputStream(data));
            // 设置分块的长度
            // 设置数据长度
            uploadPartRequest.setPartSize(data.length);
            // 假设要上传的part编号是10
            uploadPartRequest.setPartNumber(i + 1);
            UploadPartResult uploadPartResult = cosclient.uploadPart(uploadPartRequest);
            PartETag partETag = uploadPartResult.getPartETag();
            partETags.add(partETag);
        }
        CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, initiateMultipartUploadResult.getUploadId(), partETags);
        CompleteMultipartUploadResult completeResult = cosclient.completeMultipartUpload(completeMultipartUploadRequest);
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    }
    // 关闭客户端
    cosclient.shutdown();
}
Also used : InitiateMultipartUploadResult(com.qcloud.cos.model.InitiateMultipartUploadResult) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosClientException(com.qcloud.cos.exception.CosClientException) InitiateMultipartUploadRequest(com.qcloud.cos.model.InitiateMultipartUploadRequest) UploadPartRequest(com.qcloud.cos.model.UploadPartRequest) CompleteMultipartUploadResult(com.qcloud.cos.model.CompleteMultipartUploadResult) PartETag(com.qcloud.cos.model.PartETag) LinkedList(java.util.LinkedList) SSECOSKeyManagementParams(com.qcloud.cos.model.SSECOSKeyManagementParams) COSClient(com.qcloud.cos.COSClient) UploadPartResult(com.qcloud.cos.model.UploadPartResult) CosServiceException(com.qcloud.cos.exception.CosServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) CompleteMultipartUploadRequest(com.qcloud.cos.model.CompleteMultipartUploadRequest)

Example 2 with InitiateMultipartUploadRequest

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

the class MultipartUploadDemo method InitMultipartUploadDemo.

public static String InitMultipartUploadDemo() {
    // 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-guangzhou"));
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket名需包含appid
    String bucketName = "mybucket-1251668577";
    String key = "aaa/bbb.txt";
    InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, key);
    // 设置存储类型, 默认是标准(Standard), 低频(Standard_IA), 归档(Archive)
    request.setStorageClass(StorageClass.Standard);
    try {
        InitiateMultipartUploadResult initResult = cosclient.initiateMultipartUpload(request);
        // 获取uploadid
        String uploadId = initResult.getUploadId();
        return uploadId;
    } catch (CosServiceException e) {
        throw e;
    } catch (CosClientException e) {
        throw e;
    } finally {
        cosclient.shutdown();
    }
}
Also used : COSClient(com.qcloud.cos.COSClient) InitiateMultipartUploadResult(com.qcloud.cos.model.InitiateMultipartUploadResult) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosServiceException(com.qcloud.cos.exception.CosServiceException) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosClientException(com.qcloud.cos.exception.CosClientException) InitiateMultipartUploadRequest(com.qcloud.cos.model.InitiateMultipartUploadRequest) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 3 with InitiateMultipartUploadRequest

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

the class AbstractCOSClientTest method testInitMultipart.

protected String testInitMultipart(String key) {
    InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucket, key);
    request.setStorageClass(StorageClass.Standard_IA);
    InitiateMultipartUploadResult initResult = cosclient.initiateMultipartUpload(request);
    return initResult.getUploadId();
}
Also used : InitiateMultipartUploadResult(com.qcloud.cos.model.InitiateMultipartUploadResult) InitiateMultipartUploadRequest(com.qcloud.cos.model.InitiateMultipartUploadRequest)

Example 4 with InitiateMultipartUploadRequest

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

the class CopyCallable method initiateMultipartUpload.

/**
 * Initiates a multipart upload and returns the upload id
 */
private String initiateMultipartUpload(CopyObjectRequest origReq) {
    InitiateMultipartUploadRequest req = new InitiateMultipartUploadRequest(origReq.getDestinationBucketName(), origReq.getDestinationKey()).withCannedACL(origReq.getCannedAccessControlList()).withAccessControlList(origReq.getAccessControlList()).withStorageClass(origReq.getStorageClass()).withGeneralProgressListener(origReq.getGeneralProgressListener());
    ObjectMetadata newObjectMetadata = origReq.getNewObjectMetadata();
    if (newObjectMetadata == null) {
        newObjectMetadata = new ObjectMetadata();
    }
    if (newObjectMetadata.getContentType() == null) {
        newObjectMetadata.setContentType(metadata.getContentType());
    }
    req.setObjectMetadata(newObjectMetadata);
    TransferManagerUtils.populateEndpointAddr(origReq, req);
    String uploadId = cos.initiateMultipartUpload(req).getUploadId();
    log.debug("Initiated new multipart upload: " + uploadId);
    return uploadId;
}
Also used : InitiateMultipartUploadRequest(com.qcloud.cos.model.InitiateMultipartUploadRequest) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 5 with InitiateMultipartUploadRequest

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

the class QRCodeDemo method identifyQrCodeWithMultipart.

public static void identifyQrCodeWithMultipart(COSClient cosClient) throws FileNotFoundException {
    // 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");
    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("qrcode-1.png");
    rule1.setRule("QRcode/cover/1");
    ruleList.add(rule1);
    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());
    }
}
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

InitiateMultipartUploadRequest (com.qcloud.cos.model.InitiateMultipartUploadRequest)10 InitiateMultipartUploadResult (com.qcloud.cos.model.InitiateMultipartUploadResult)7 CompleteMultipartUploadRequest (com.qcloud.cos.model.CompleteMultipartUploadRequest)5 CompleteMultipartUploadResult (com.qcloud.cos.model.CompleteMultipartUploadResult)5 UploadPartRequest (com.qcloud.cos.model.UploadPartRequest)5 CosClientException (com.qcloud.cos.exception.CosClientException)4 CosServiceException (com.qcloud.cos.exception.CosServiceException)4 PartETag (com.qcloud.cos.model.PartETag)4 UploadPartResult (com.qcloud.cos.model.UploadPartResult)4 LinkedList (java.util.LinkedList)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 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 ResponseNotCompleteException (com.qcloud.cos.exception.ResponseNotCompleteException)2