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);
}
}
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);
}
}
Aggregations