Search in sources :

Example 16 with COSObject

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();
}
Also used : COSObject(com.qcloud.cos.model.COSObject) CosClientException(com.qcloud.cos.exception.CosClientException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 17 with COSObject

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;
    }
}
Also used : EncryptedGetObjectRequest(com.qcloud.cos.model.EncryptedGetObjectRequest) COSObject(com.qcloud.cos.model.COSObject)

Example 18 with COSObject

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;
}
Also used : COSObject(com.qcloud.cos.model.COSObject) COSObjectInputStream(com.qcloud.cos.model.COSObjectInputStream) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 19 with COSObject

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());
}
Also used : CRC64(com.qcloud.cos.utils.CRC64) COSObject(com.qcloud.cos.model.COSObject) InputStream(java.io.InputStream) CosClientException(com.qcloud.cos.exception.CosClientException) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) ByteBuffer(java.nio.ByteBuffer)

Example 20 with COSObject

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;
}
Also used : SSLProtocolException(javax.net.ssl.SSLProtocolException) SocketException(java.net.SocketException) COSObject(com.qcloud.cos.model.COSObject) CosClientException(com.qcloud.cos.exception.CosClientException)

Aggregations

COSObject (com.qcloud.cos.model.COSObject)21 CosServiceException (com.qcloud.cos.exception.CosServiceException)11 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)11 CosClientException (com.qcloud.cos.exception.CosClientException)8 IOException (java.io.IOException)7 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)6 File (java.io.File)6 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)4 ResponseNotCompleteException (com.qcloud.cos.exception.ResponseNotCompleteException)3 SocketException (java.net.SocketException)3 SSLProtocolException (javax.net.ssl.SSLProtocolException)3 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)2 JeesuiteBaseException (com.jeesuite.common.JeesuiteBaseException)1 JeesuiteBaseException (com.mendmix.common.JeesuiteBaseException)1 FileLockException (com.qcloud.cos.exception.FileLockException)1 AbstractPutObjectRequest (com.qcloud.cos.model.AbstractPutObjectRequest)1 COSObjectId (com.qcloud.cos.model.COSObjectId)1 COSObjectInputStream (com.qcloud.cos.model.COSObjectInputStream)1 EncryptedGetObjectRequest (com.qcloud.cos.model.EncryptedGetObjectRequest)1