Search in sources :

Example 1 with InputSubstream

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

the class COSClient method uploadPart.

@Override
public UploadPartResult uploadPart(UploadPartRequest uploadPartRequest) throws CosClientException, CosServiceException {
    rejectNull(uploadPartRequest, "The request parameter must be specified when uploading a part");
    final File fileOrig = uploadPartRequest.getFile();
    final InputStream isOrig = uploadPartRequest.getInputStream();
    final String bucketName = uploadPartRequest.getBucketName();
    final String key = uploadPartRequest.getKey();
    final String uploadId = uploadPartRequest.getUploadId();
    final int partNumber = uploadPartRequest.getPartNumber();
    final long partSize = uploadPartRequest.getPartSize();
    rejectNull(bucketName, "The bucket name parameter must be specified when uploading a part");
    rejectNull(key, "The key parameter must be specified when uploading a part");
    rejectNull(uploadId, "The upload ID parameter must be specified when uploading a part");
    rejectNull(partNumber, "The part number parameter must be specified when uploading a part");
    rejectNull(partSize, "The part size parameter must be specified when uploading a part");
    rejectNull(clientConfig.getRegion(), "region is null, region in clientConfig must be specified when uploading a part");
    CosHttpRequest<UploadPartRequest> request = createRequest(bucketName, key, uploadPartRequest, HttpMethodName.PUT);
    request.addParameter("uploadId", uploadId);
    request.addParameter("partNumber", Integer.toString(partNumber));
    final ObjectMetadata objectMetadata = uploadPartRequest.getObjectMetadata();
    if (objectMetadata != null)
        populateRequestMetadata(request, objectMetadata);
    addHeaderIfNotNull(request, Headers.CONTENT_MD5, uploadPartRequest.getMd5Digest());
    request.addHeader(Headers.CONTENT_LENGTH, Long.toString(partSize));
    // Populate the SSE-C parameters to the request header
    populateSSE_C(request, uploadPartRequest.getSSECustomerKey());
    // Populate the traffic limit parameter to the request header
    populateTrafficLimit(request, uploadPartRequest.getTrafficLimit());
    InputStream isCurr = isOrig;
    try {
        if (fileOrig == null) {
            if (isOrig == null) {
                throw new IllegalArgumentException("A File or InputStream must be specified when uploading part");
            } else {
                // When isCurr is a FileInputStream, this wrapping enables
                // unlimited mark-and-reset
                isCurr = ReleasableInputStream.wrap(isCurr);
            }
        } else {
            try {
                isCurr = new ResettableInputStream(fileOrig);
            } catch (IOException e) {
                throw new IllegalArgumentException("Failed to open file " + fileOrig, e);
            }
        }
        isCurr = new InputSubstream(isCurr, uploadPartRequest.getFileOffset(), partSize, uploadPartRequest.isLastPart());
        MD5DigestCalculatingInputStream md5DigestStream = null;
        if (uploadPartRequest.getMd5Digest() == null && !skipMd5CheckStrategy.skipClientSideValidationPerRequest(uploadPartRequest)) {
            /*
                 * If the user hasn't set the content MD5, then we don't want to buffer the whole
                 * stream in memory just to calculate it. Instead, we can calculate it on the fly
                 * and validate it with the returned ETag from the object upload.
                 */
            isCurr = md5DigestStream = new MD5DigestCalculatingInputStream(isCurr);
        }
        return doUploadPart(bucketName, key, uploadId, partNumber, partSize, request, isCurr, md5DigestStream);
    } finally {
        CosDataSource.Utils.cleanupDataSource(uploadPartRequest, fileOrig, isOrig, isCurr, log);
    }
}
Also used : DigestValidationInputStream(com.qcloud.cos.internal.DigestValidationInputStream) ReleasableInputStream(com.qcloud.cos.internal.ReleasableInputStream) MD5DigestCalculatingInputStream(com.qcloud.cos.internal.MD5DigestCalculatingInputStream) LengthCheckInputStream(com.qcloud.cos.internal.LengthCheckInputStream) ServiceClientHolderInputStream(com.qcloud.cos.internal.ServiceClientHolderInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ResettableInputStream(com.qcloud.cos.internal.ResettableInputStream) SdkFilterInputStream(com.qcloud.cos.internal.SdkFilterInputStream) InputStream(java.io.InputStream) MD5DigestCalculatingInputStream(com.qcloud.cos.internal.MD5DigestCalculatingInputStream) IOException(java.io.IOException) InputSubstream(com.qcloud.cos.internal.InputSubstream) File(java.io.File) ResettableInputStream(com.qcloud.cos.internal.ResettableInputStream)

Example 2 with InputSubstream

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

the class COSCryptoModuleBase method newMultipartCOSCipherInputStream.

protected final CipherLiteInputStream newMultipartCOSCipherInputStream(UploadPartRequest req, CipherLite cipherLite) {
    final File fileOrig = req.getFile();
    final InputStream isOrig = req.getInputStream();
    InputStream isCurr = null;
    try {
        if (fileOrig == null) {
            if (isOrig == null) {
                throw new IllegalArgumentException("A File or InputStream must be specified when uploading part");
            }
            isCurr = isOrig;
        } else {
            isCurr = new ResettableInputStream(fileOrig);
        }
        isCurr = new InputSubstream(isCurr, req.getFileOffset(), req.getPartSize(), req.isLastPart());
        return cipherLite.markSupported() ? new CipherLiteInputStream(isCurr, cipherLite, DEFAULT_BUFFER_SIZE, IS_MULTI_PART, req.isLastPart()) : new RenewableCipherLiteInputStream(isCurr, cipherLite, DEFAULT_BUFFER_SIZE, IS_MULTI_PART, req.isLastPart());
    } catch (Exception e) {
        cleanupDataSource(req, fileOrig, isOrig, isCurr, log);
        throw new CosClientException("Unable to create cipher input stream", e);
    }
}
Also used : InputSubstream(com.qcloud.cos.internal.InputSubstream) 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) CosClientException(com.qcloud.cos.exception.CosClientException) InstructionFile(com.qcloud.cos.internal.crypto.CryptoStorageMode.InstructionFile) File(java.io.File) ResettableInputStream(com.qcloud.cos.internal.ResettableInputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CosServiceException(com.qcloud.cos.exception.CosServiceException) CosClientException(com.qcloud.cos.exception.CosClientException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

InputSubstream (com.qcloud.cos.internal.InputSubstream)2 LengthCheckInputStream (com.qcloud.cos.internal.LengthCheckInputStream)2 ReleasableInputStream (com.qcloud.cos.internal.ReleasableInputStream)2 ResettableInputStream (com.qcloud.cos.internal.ResettableInputStream)2 SdkFilterInputStream (com.qcloud.cos.internal.SdkFilterInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 InputStream (java.io.InputStream)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 CosClientException (com.qcloud.cos.exception.CosClientException)1 CosServiceException (com.qcloud.cos.exception.CosServiceException)1 DigestValidationInputStream (com.qcloud.cos.internal.DigestValidationInputStream)1 MD5DigestCalculatingInputStream (com.qcloud.cos.internal.MD5DigestCalculatingInputStream)1 ServiceClientHolderInputStream (com.qcloud.cos.internal.ServiceClientHolderInputStream)1 InstructionFile (com.qcloud.cos.internal.crypto.CryptoStorageMode.InstructionFile)1 IOException (java.io.IOException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1