Search in sources :

Example 1 with SdkFilterInputStream

use of com.qcloud.cos.internal.SdkFilterInputStream in project cos-java-sdk-v5 by tencentyun.

the class COSClient method selectObjectContent.

@Override
public SelectObjectContentResult selectObjectContent(SelectObjectContentRequest selectRequest) throws CosClientException, CosServiceException {
    rejectNull(selectRequest, "The request parameter must be specified");
    rejectNull(selectRequest.getBucketName(), "The bucket name parameter must be specified when selecting object content.");
    rejectNull(selectRequest.getKey(), "The key parameter must be specified when selecting object content.");
    CosHttpRequest<SelectObjectContentRequest> request = createRequest(selectRequest.getBucketName(), selectRequest.getKey(), selectRequest, HttpMethodName.POST);
    request.addParameter("select", null);
    request.addParameter("select-type", "2");
    populateSSE_C(request, selectRequest.getSSECustomerKey());
    setContent(request, RequestXmlFactory.convertToXmlByteArray(selectRequest), ContentType.APPLICATION_XML.toString(), true);
    COSObject result = invoke(request, new COSObjectResponseHandler());
    // Hold a reference to this client while the InputStream is still
    // around - otherwise a finalizer in the HttpClient may reset the
    // underlying TCP connection out from under us.
    SdkFilterInputStream resultStream = new ServiceClientHolderInputStream(result.getObjectContent(), this);
    return new SelectObjectContentResult().withPayload(new SelectObjectContentEventStream(resultStream));
}
Also used : COSObjectResponseHandler(com.qcloud.cos.internal.COSObjectResponseHandler) SdkFilterInputStream(com.qcloud.cos.internal.SdkFilterInputStream) ServiceClientHolderInputStream(com.qcloud.cos.internal.ServiceClientHolderInputStream)

Example 2 with SdkFilterInputStream

use of com.qcloud.cos.internal.SdkFilterInputStream 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)

Aggregations

SdkFilterInputStream (com.qcloud.cos.internal.SdkFilterInputStream)2 CosClientException (com.qcloud.cos.exception.CosClientException)1 COSObjectResponseHandler (com.qcloud.cos.internal.COSObjectResponseHandler)1 LengthCheckInputStream (com.qcloud.cos.internal.LengthCheckInputStream)1 ReleasableInputStream (com.qcloud.cos.internal.ReleasableInputStream)1 ResettableInputStream (com.qcloud.cos.internal.ResettableInputStream)1 ServiceClientHolderInputStream (com.qcloud.cos.internal.ServiceClientHolderInputStream)1 InstructionFile (com.qcloud.cos.internal.crypto.CryptoStorageMode.InstructionFile)1 UploadPartResult (com.qcloud.cos.model.UploadPartResult)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1