Search in sources :

Example 6 with InitiateMultipartUploadResult

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

the class COSCryptoModuleBase method initiateMultipartUploadSecurely.

@Override
public InitiateMultipartUploadResult initiateMultipartUploadSecurely(InitiateMultipartUploadRequest req) {
    // Generate a one-time use symmetric key and initialize a cipher to
    // encrypt object data
    ContentCryptoMaterial cekMaterial = createContentCryptoMaterial(req);
    if (cryptoConfig.getStorageMode() == ObjectMetadata) {
        ObjectMetadata metadata = req.getObjectMetadata();
        if (metadata == null)
            metadata = new ObjectMetadata();
        long dataSize = req.getDataSize();
        long partSize = req.getPartSize();
        if (dataSize < 0 || partSize < 0) {
            throw new CosClientException("initiate multipart upload with encryption client must set dataSize and partSize");
        }
        if (partSize % 16 != 0) {
            throw new CosClientException("initiat multipart uplaod with encryption client must set part size a mutiple of 16" + "but got " + partSize);
        }
        metadata.addUserMetadata(Headers.ENCRYPTION_DATA_SIZE, Long.toString(dataSize));
        metadata.addUserMetadata(Headers.ENCRYPTION_PART_SIZE, Long.toString(partSize));
        // Store encryption info in metadata
        req.setObjectMetadata(updateMetadataWithContentCryptoMaterial(metadata, null, cekMaterial));
    }
    InitiateMultipartUploadResult result = cos.initiateMultipartUpload(req);
    MultipartUploadCryptoContext uploadContext = newUploadContext(req, cekMaterial);
    if (req instanceof MaterialsDescriptionProvider) {
        MaterialsDescriptionProvider p = (MaterialsDescriptionProvider) req;
        uploadContext.setMaterialsDescription(p.getMaterialsDescription());
    }
    multipartUploadContexts.put(result.getUploadId(), uploadContext);
    return result;
}
Also used : MaterialsDescriptionProvider(com.qcloud.cos.model.MaterialsDescriptionProvider) InitiateMultipartUploadResult(com.qcloud.cos.model.InitiateMultipartUploadResult) CosClientException(com.qcloud.cos.exception.CosClientException) ObjectMetadata(com.qcloud.cos.internal.crypto.CryptoStorageMode.ObjectMetadata) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 7 with InitiateMultipartUploadResult

use of com.qcloud.cos.model.InitiateMultipartUploadResult in project hadoop-cos by tencentyun.

the class CosNativeFileSystemStore method getUploadId.

/**
 *  get cos upload Id
 * @param key cos key
 * @return uploadId
 * @throws IOException when fail to get the Multipart Upload ID.
 */
public String getUploadId(String key) throws IOException {
    if (null == key || key.length() == 0) {
        return "";
    }
    ObjectMetadata objectMetadata = new ObjectMetadata();
    if (crc32cEnabled) {
        objectMetadata.setHeader(Constants.CRC32C_REQ_HEADER, Constants.CRC32C_REQ_HEADER_VAL);
    }
    InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(bucketName, key);
    if (null != this.storageClass) {
        initiateMultipartUploadRequest.setStorageClass(this.storageClass);
    }
    initiateMultipartUploadRequest.setObjectMetadata(objectMetadata);
    this.setEncryptionMetadata(initiateMultipartUploadRequest, objectMetadata);
    try {
        InitiateMultipartUploadResult initiateMultipartUploadResult = (InitiateMultipartUploadResult) this.callCOSClientWithRetry(initiateMultipartUploadRequest);
        return initiateMultipartUploadResult.getUploadId();
    } catch (Exception e) {
        String errMsg = String.format("Get the upload id failed, cos key: %s, " + "exception: %s", key, e);
        handleException(new Exception(errMsg), key);
    }
    return null;
}
Also used : InitiateMultipartUploadResult(com.qcloud.cos.model.InitiateMultipartUploadResult) InitiateMultipartUploadRequest(com.qcloud.cos.model.InitiateMultipartUploadRequest) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) ResponseNotCompleteException(com.qcloud.cos.exception.ResponseNotCompleteException) CosServiceException(com.qcloud.cos.exception.CosServiceException) CosClientException(com.qcloud.cos.exception.CosClientException) IOException(java.io.IOException)

Example 8 with InitiateMultipartUploadResult

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

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