Search in sources :

Example 11 with COSObject

use of com.qcloud.cos.model.COSObject in project hadoop-cos by tencentyun.

the class CosNativeFileSystemStore method retrieve.

/**
 * retrieve cos key
 *
 * @param key The key is the object name that is being retrieved from the
 *            cos bucket.
 * @return This method returns null if the key is not found.
 * @throws IOException Retrieve the specified cos key failed.
 */
@Override
public InputStream retrieve(String key) throws IOException {
    LOG.debug("Retrieve the key: {}.", key);
    GetObjectRequest getObjectRequest = new GetObjectRequest(this.bucketName, key);
    if (this.trafficLimit >= 0) {
        getObjectRequest.setTrafficLimit(this.trafficLimit);
    }
    this.setEncryptionMetadata(getObjectRequest, new ObjectMetadata());
    try {
        COSObject cosObject = (COSObject) callCOSClientWithRetry(getObjectRequest);
        return cosObject.getObjectContent();
    } catch (Exception e) {
        String errMsg = String.format("Retrieving the cos key [%s] occurs an exception: %s", key, e);
        handleException(new Exception(errMsg), key);
    }
    // never will get here
    return null;
}
Also used : COSObject(com.qcloud.cos.model.COSObject) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) ResponseNotCompleteException(com.qcloud.cos.exception.ResponseNotCompleteException) CosServiceException(com.qcloud.cos.exception.CosServiceException) CosClientException(com.qcloud.cos.exception.CosClientException) IOException(java.io.IOException)

Example 12 with COSObject

use of com.qcloud.cos.model.COSObject in project jeesuite-libs by vakinge.

the class QcloudProvider method getObjectInputStream.

@Override
public InputStream getObjectInputStream(String bucketName, String fileKey) {
    try {
        String _bucketName = buildBucketName(bucketName);
        String _fileKey = resolveFileKey(bucketName, fileKey);
        COSObject cosObject = cosclient.getObject(_bucketName, _fileKey);
        return cosObject.getObjectContent();
    } catch (Exception e) {
        throw new JeesuiteBaseException(500, buildMessage(bucketName, e));
    }
}
Also used : JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) COSObject(com.qcloud.cos.model.COSObject) CosServiceException(com.qcloud.cos.exception.CosServiceException) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) IOException(java.io.IOException)

Example 13 with COSObject

use of com.qcloud.cos.model.COSObject in project cos-java-sdk-v5 by tencentyun.

the class PutGetDelTest method testGetObjectIfNoneMatchRightEtag.

@Test
public void testGetObjectIfNoneMatchRightEtag() throws IOException {
    if (!judgeUserInfoValid()) {
        return;
    }
    File localFile = buildTestFile(1024);
    String key = "ut/" + localFile.getName();
    cosclient.putObject(bucket, key, localFile);
    try {
        String fileEtag = Md5Utils.md5Hex(localFile);
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
        List<String> eTagList = new ArrayList<>();
        eTagList.add("\"" + fileEtag + "\"");
        getObjectRequest.setNonmatchingETagConstraints(eTagList);
        COSObject cosObject = cosclient.getObject(getObjectRequest);
        assertNull(cosObject);
    } catch (CosServiceException cse) {
        fail(cse.toString());
    } finally {
        cosclient.deleteObject(bucket, key);
        assertTrue(localFile.delete());
    }
}
Also used : CosServiceException(com.qcloud.cos.exception.CosServiceException) COSObject(com.qcloud.cos.model.COSObject) ArrayList(java.util.ArrayList) File(java.io.File) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) Test(org.junit.Test)

Example 14 with COSObject

use of com.qcloud.cos.model.COSObject in project cos-java-sdk-v5 by tencentyun.

the class PutGetDelTest method testGetObjectIfMatchContainRightEtag.

@Ignore
public void testGetObjectIfMatchContainRightEtag() throws IOException {
    if (!judgeUserInfoValid()) {
        return;
    }
    File localFile = buildTestFile(1024);
    String key = "ut/" + localFile.getName();
    cosclient.putObject(bucket, key, localFile);
    try {
        String fileEtag = Md5Utils.md5Hex(localFile);
        String wrongEtag = fileEtag.substring(5) + fileEtag.substring(0, 5);
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
        List<String> eTagList = new ArrayList<>();
        eTagList.add("\"" + wrongEtag + "\"");
        eTagList.add("\"" + fileEtag + "\"");
        getObjectRequest.setMatchingETagConstraints(eTagList);
        COSObject cosObject = cosclient.getObject(getObjectRequest);
        assertNotNull(cosObject);
    } catch (CosServiceException cse) {
        fail(cse.toString());
    } finally {
        cosclient.deleteObject(bucket, key);
        assertTrue(localFile.delete());
    }
}
Also used : CosServiceException(com.qcloud.cos.exception.CosServiceException) COSObject(com.qcloud.cos.model.COSObject) ArrayList(java.util.ArrayList) File(java.io.File) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) Ignore(org.junit.Ignore)

Example 15 with COSObject

use of com.qcloud.cos.model.COSObject in project cos-java-sdk-v5 by tencentyun.

the class DownloadCallable method retryableDownloadCOSObjectToFile.

private COSObject retryableDownloadCOSObjectToFile(File file, RetryableCOSDownloadTask retryableCOSDownloadTask, boolean appendData) {
    boolean hasRetried = false;
    COSObject cosObject;
    for (; ; ) {
        if (resumeExistingDownload && hasRetried) {
            // Need to adjust the get range or else we risk corrupting the downloaded file
            adjustRequest(req);
        }
        cosObject = retryableCOSDownloadTask.getCOSObjectStream();
        if (cosObject == null)
            return null;
        try {
            if (testing && resumeExistingDownload && !hasRetried) {
                throw new CosClientException("testing");
            }
            ServiceUtils.downloadToFile(cosObject, file, retryableCOSDownloadTask.needIntegrityCheck(), appendData, expectedFileLength);
            return cosObject;
        } catch (CosClientException ace) {
            if (!ace.isRetryable())
                throw ace;
            // The current code will retry the download only when case 2) or 3) happens.
            if (ace.getCause() instanceof SocketException || ace.getCause() instanceof SSLProtocolException) {
                throw ace;
            } else {
                if (hasRetried)
                    throw ace;
                else {
                    log.info("Retry the download of object " + cosObject.getKey() + " (bucket " + cosObject.getBucketName() + ")", ace);
                    hasRetried = true;
                }
            }
        } finally {
            cosObject.getObjectContent().abort();
        }
    }
}
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