use of com.qcloud.cos.model.COSObject in project cos-java-sdk-v5 by tencentyun.
the class COSCryptoModuleAE method getObjectSecurely.
@Override
public ObjectMetadata getObjectSecurely(GetObjectRequest getObjectRequest, File destinationFile) {
assertParameterNotNull(destinationFile, "The destination file parameter must be specified when downloading an object directly to a file");
COSObject cosObject = getObjectSecurely(getObjectRequest);
// getObject can return null if constraints were specified but not met
if (cosObject == null)
return null;
OutputStream outputStream = null;
try {
outputStream = new BufferedOutputStream(new FileOutputStream(destinationFile));
byte[] buffer = new byte[1024 * 10];
int bytesRead;
while ((bytesRead = cosObject.getObjectContent().read(buffer)) > -1) {
outputStream.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
throw new CosClientException("Unable to store object contents to disk: " + e.getMessage(), e);
} finally {
IOUtils.closeQuietly(outputStream, log);
IOUtils.closeQuietly(cosObject.getObjectContent(), log);
}
return cosObject.getObjectMetadata();
}
use of com.qcloud.cos.model.COSObject in project cos-java-sdk-v5 by tencentyun.
the class COSCryptoModuleAE method getObjectSecurely.
@Override
public COSObject getObjectSecurely(GetObjectRequest req) {
// Adjust the crypto range to retrieve all of the cipher blocks needed to contain the user's
// desired
// range of bytes.
long[] desiredRange = req.getRange();
if (isStrict() && (desiredRange != null))
throw new SecurityException("Range get and getting a part are not allowed in strict crypto mode");
long[] adjustedCryptoRange = getAdjustedCryptoRange(desiredRange);
if (adjustedCryptoRange != null)
req.setRange(adjustedCryptoRange[0], adjustedCryptoRange[1]);
// Get the object from COS
COSObject retrieved = cos.getObject(req);
// would return null, so we simply return null as well.
if (retrieved == null)
return null;
String suffix = null;
if (req instanceof EncryptedGetObjectRequest) {
EncryptedGetObjectRequest ereq = (EncryptedGetObjectRequest) req;
suffix = ereq.getInstructionFileSuffix();
}
try {
return suffix == null || suffix.trim().isEmpty() ? decipher(req, desiredRange, adjustedCryptoRange, retrieved) : decipherWithInstFileSuffix(req, desiredRange, adjustedCryptoRange, retrieved, suffix);
} catch (RuntimeException ex) {
// If we're unable to set up the decryption, make sure we close the
// HTTP connection
IOUtils.closeQuietly(retrieved, log);
throw ex;
} catch (Error error) {
IOUtils.closeQuietly(retrieved, log);
throw error;
}
}
use of com.qcloud.cos.model.COSObject in project cos-java-sdk-v5 by tencentyun.
the class COSObjectResponseHandler method handle.
@Override
public CosServiceResponse<COSObject> handle(CosHttpResponse response) throws Exception {
COSObject object = new COSObject();
CosServiceResponse<COSObject> cosResponse = parseResponseMetadata(response);
ObjectMetadata metadata = object.getObjectMetadata();
populateObjectMetadata(response, metadata);
object.setObjectContent(new COSObjectInputStream(response.getContent(), response.getHttpRequest()));
cosResponse.setResult(object);
return cosResponse;
}
use of com.qcloud.cos.model.COSObject in project cos-java-sdk-v5 by tencentyun.
the class RangeDownloadCallable method call.
public DownloadPart call() throws Exception {
COSObject object = cos.getObject(request);
InputStream input = object.getObjectContent();
ObjectMetadata meta = object.getObjectMetadata();
long[] range = request.getRange();
long start = range[0];
long end = range[1];
long position = start;
ByteBuffer tmpBuf = ByteBuffer.allocateDirect(1024 * 1024);
byte[] buffer = new byte[1024 * 10];
int bytesRead;
CRC64 crc64 = new CRC64();
while ((bytesRead = input.read(buffer)) > -1) {
start += bytesRead;
crc64.update(buffer, bytesRead);
if (tmpBuf.remaining() < bytesRead) {
tmpBuf.flip();
position += destFileChannel.write(tmpBuf, position);
tmpBuf.clear();
}
tmpBuf.put(buffer, 0, bytesRead);
}
tmpBuf.flip();
destFileChannel.write(tmpBuf, position);
if (start != end + 1) {
destFileChannel.close();
destFile.delete();
String msg = String.format("get object want %d bytes, but got %d bytes, reqeust_id: %s", end + 1, start, meta.getRequestId());
throw new CosClientException(msg);
}
String block = String.format("%d-%d", range[0], range[1]);
downloadRecord.putDownloadedBlocks(block);
downloadRecord.dump();
return new DownloadPart(range[0], range[1], crc64.getValue());
}
use of com.qcloud.cos.model.COSObject in project cos-java-sdk-v5 by tencentyun.
the class ServiceUtils method retryableDownloadCOSObjectToFile.
/**
* Gets an object stored in COS and downloads it into the specified file.
* This method includes the one-time retry mechanism after integrity check failure
* on the downloaded file. It will also return immediately after getting null valued
* COSObject (when getObject request does not meet the specified constraints).
*
* @param file
* The file to store the object's data in.
* @param retryableCOSDownloadTask
* The implementation of SafeCOSDownloadTask interface which allows user to
* get access to all the visible variables at the calling site of this method.
*/
public static COSObject retryableDownloadCOSObjectToFile(File file, RetryableCOSDownloadTask retryableCOSDownloadTask, boolean appendData) {
boolean hasRetried = false;
boolean needRetry;
COSObject cosObject;
do {
needRetry = false;
cosObject = retryableCOSDownloadTask.getCOSObjectStream();
if (cosObject == null)
return null;
try {
ServiceUtils.downloadObjectToFile(cosObject, file, retryableCOSDownloadTask.needIntegrityCheck(), appendData);
} catch (CosClientException cse) {
if (!cse.isRetryable()) {
cosObject.getObjectContent().abort();
throw cse;
}
// The current code will retry the download only when case 2) or 3) happens.
if (cse.getCause() instanceof SocketException || cse.getCause() instanceof SSLProtocolException) {
throw cse;
} else {
needRetry = true;
if (hasRetried) {
cosObject.getObjectContent().abort();
throw cse;
} else {
log.info("Retry the download of object " + cosObject.getKey() + " (bucket " + cosObject.getBucketName() + ")", cse);
hasRetried = true;
}
}
}
} while (needRetry);
return cosObject;
}
Aggregations