Search in sources :

Example 21 with COSObject

use of com.qcloud.cos.model.COSObject in project alluxio by Alluxio.

the class COSInputStream method createStream.

@Override
protected InputStream createStream(long startPos, long endPos) throws IOException {
    GetObjectRequest req = new GetObjectRequest(mBucketName, mKey);
    // COS returns entire object if we read past the end
    req.setRange(startPos, endPos < mContentLength ? endPos - 1 : mContentLength - 1);
    CosServiceException lastException = null;
    String errorMessage = String.format("Failed to open key: %s bucket: %s", mKey, mBucketName);
    while (mRetryPolicy.attempt()) {
        try {
            COSObject object = mCosClient.getObject(req);
            return new BufferedInputStream(object.getObjectContent());
        } catch (CosServiceException e) {
            errorMessage = String.format("Failed to open key: %s bucket: %s attempts: %d error: %s", mKey, mBucketName, mRetryPolicy.getAttemptCount(), e.getMessage());
            if (e.getStatusCode() != HttpStatus.SC_NOT_FOUND) {
                throw new IOException(errorMessage, e);
            }
            // Key does not exist
            lastException = e;
        }
    }
    // Failed after retrying key does not exist
    throw new IOException(errorMessage, lastException);
}
Also used : CosServiceException(com.qcloud.cos.exception.CosServiceException) BufferedInputStream(java.io.BufferedInputStream) COSObject(com.qcloud.cos.model.COSObject) IOException(java.io.IOException) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest)

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