Search in sources :

Example 6 with UploadPartResult

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

the class COSCryptoModuleBase method uploadPartSecurely.

/**
 * {@inheritDoc}
 *
 * <p>
 * <b>NOTE:</b> Because the encryption process requires context from previous blocks, parts
 * uploaded with the COSEncryptionClient (as opposed to the normal COSClient) must be
 * uploaded serially, and in order. Otherwise, the previous encryption context isn't available
 * to use when encrypting the current part.
 */
@Override
public UploadPartResult uploadPartSecurely(UploadPartRequest req) {
    final int blockSize = contentCryptoScheme.getBlockSizeInBytes();
    final boolean isLastPart = req.isLastPart();
    final String uploadId = req.getUploadId();
    final long partSize = req.getPartSize();
    final boolean partSizeMultipleOfCipherBlockSize = 0 == (partSize % blockSize);
    if (!isLastPart && !partSizeMultipleOfCipherBlockSize) {
        throw new CosClientException("Invalid part size: part sizes for encrypted multipart uploads must be multiples " + "of the cipher block size (" + blockSize + ") with the exception of the last part.");
    }
    final MultipartUploadCryptoContext uploadContext = multipartUploadContexts.get(uploadId);
    if (uploadContext == null) {
        throw new CosClientException("No client-side information available on upload ID " + uploadId);
    }
    final UploadPartResult result;
    // Checks the parts are uploaded in series
    uploadContext.beginPartUpload(req.getPartNumber());
    CipherLite cipherLite = cipherLiteForNextPart(uploadContext);
    final File fileOrig = req.getFile();
    final InputStream isOrig = req.getInputStream();
    SdkFilterInputStream isCurr = null;
    try {
        CipherLiteInputStream clis = newMultipartCOSCipherInputStream(req, cipherLite);
        // so the clis will be closed (in the finally block below) upon
        isCurr = clis;
        // unexpected failure should we opened a file undereath
        req.setInputStream(isCurr);
        // Treat all encryption requests as input stream upload requests,
        // not as file upload requests.
        req.setFile(null);
        req.setFileOffset(0);
        // 16-byte mac
        if (isLastPart) {
            // We only change the size of the last part
            long lastPartSize = computeLastPartSize(req);
            if (lastPartSize > -1)
                req.setPartSize(lastPartSize);
            if (uploadContext.hasFinalPartBeenSeen()) {
                throw new CosClientException("This part was specified as the last part in a multipart upload, but a previous part was already marked as the last part.  " + "Only the last part of the upload should be marked as the last part.");
            }
        }
        result = cos.uploadPart(req);
    } finally {
        cleanupDataSource(req, fileOrig, isOrig, isCurr, log);
        uploadContext.endPartUpload();
    }
    if (isLastPart)
        uploadContext.setHasFinalPartBeenSeen(true);
    return result;
}
Also used : UploadPartResult(com.qcloud.cos.model.UploadPartResult) CosClientException(com.qcloud.cos.exception.CosClientException) ByteArrayInputStream(java.io.ByteArrayInputStream) ResettableInputStream(com.qcloud.cos.internal.ResettableInputStream) ReleasableInputStream(com.qcloud.cos.internal.ReleasableInputStream) SdkFilterInputStream(com.qcloud.cos.internal.SdkFilterInputStream) LengthCheckInputStream(com.qcloud.cos.internal.LengthCheckInputStream) InputStream(java.io.InputStream) SdkFilterInputStream(com.qcloud.cos.internal.SdkFilterInputStream) InstructionFile(com.qcloud.cos.internal.crypto.CryptoStorageMode.InstructionFile) File(java.io.File)

Example 7 with UploadPartResult

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

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