Search in sources :

Example 6 with COSObject

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

the class DownloadTaskImpl method getCOSObjectStream.

@Override
public COSObject getCOSObjectStream() {
    COSObject cosObject = cos.getObject(getObjectRequest);
    download.setCosObject(cosObject);
    return cosObject;
}
Also used : COSObject(com.qcloud.cos.model.COSObject)

Example 7 with COSObject

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

the class COSCryptoModuleBase method putInstructionFileSecurely.

@Override
public final PutObjectResult putInstructionFileSecurely(PutInstructionFileRequest req) {
    final COSObjectId id = req.getCOSObjectId();
    final GetObjectRequest getreq = new GetObjectRequest(id);
    // Get the object from cos
    final COSObject retrieved = cos.getObject(getreq);
    // We only need the meta-data already retrieved, not the data stream.
    // So close it immediately to prevent resource leakage.
    IOUtils.closeQuietly(retrieved, log);
    if (retrieved == null) {
        throw new IllegalArgumentException("The specified COS object (" + id + ") doesn't exist.");
    }
    COSObjectWrapper wrapped = new COSObjectWrapper(retrieved, id);
    try {
        final ContentCryptoMaterial origCCM = contentCryptoMaterialOf(wrapped);
        securityCheck(origCCM, wrapped);
        // Re-ecnrypt the CEK in a new content crypto material
        final EncryptionMaterials newKEK = req.getEncryptionMaterials();
        final ContentCryptoMaterial newCCM;
        if (newKEK == null) {
            newCCM = origCCM.recreate(req.getMaterialsDescription(), this.kekMaterialsProvider, cryptoScheme, cryptoConfig.getCryptoProvider(), kms, req);
        } else {
            newCCM = origCCM.recreate(newKEK, this.kekMaterialsProvider, cryptoScheme, cryptoConfig.getCryptoProvider(), kms, req);
        }
        PutObjectRequest putInstFileRequest = req.createPutObjectRequest(retrieved);
        // Put the new instruction file into COS
        return cos.putObject(updateInstructionPutRequest(putInstFileRequest, newCCM));
    } 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 : COSObjectId(com.qcloud.cos.model.COSObjectId) COSObject(com.qcloud.cos.model.COSObject) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) AbstractPutObjectRequest(com.qcloud.cos.model.AbstractPutObjectRequest) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 8 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.jeesuite.common.JeesuiteBaseException) COSObject(com.qcloud.cos.model.COSObject) CosServiceException(com.qcloud.cos.exception.CosServiceException) IOException(java.io.IOException) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException)

Example 9 with COSObject

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

the class CosNativeFileSystemStore method retrieve.

/**
 * @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 with a specified byte range start failed.
 */
@Override
public InputStream retrieve(String key, long byteRangeStart) throws IOException {
    try {
        LOG.debug("Retrieve the cos key: {}, byte range start: {}.", key, byteRangeStart);
        long fileSize = getFileLength(key);
        long byteRangeEnd = fileSize - 1;
        GetObjectRequest getObjectRequest = new GetObjectRequest(this.bucketName, key);
        if (this.trafficLimit >= 0) {
            getObjectRequest.setTrafficLimit(this.trafficLimit);
        }
        this.setEncryptionMetadata(getObjectRequest, new ObjectMetadata());
        if (byteRangeEnd >= byteRangeStart) {
            getObjectRequest.setRange(byteRangeStart, fileSize - 1);
        }
        COSObject cosObject = (COSObject) callCOSClientWithRetry(getObjectRequest);
        return cosObject.getObjectContent();
    } catch (Exception e) {
        String errMsg = String.format("Retrieving key [%s] with byteRangeStart [%d] " + "occurs an exception: %s.", key, byteRangeStart, 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 10 with COSObject

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

the class CosNativeFileSystemStore method retrieveBlock.

@Override
public InputStream retrieveBlock(String key, long byteRangeStart, long byteRangeEnd) throws IOException {
    LOG.debug("Retrieve the cos key: {}, byte range start: {}, byte range end: {}.", key, byteRangeStart, byteRangeEnd);
    try {
        GetObjectRequest request = new GetObjectRequest(this.bucketName, key);
        request.setRange(byteRangeStart, byteRangeEnd);
        if (this.trafficLimit >= 0) {
            request.setTrafficLimit(this.trafficLimit);
        }
        this.setEncryptionMetadata(request, new ObjectMetadata());
        COSObject cosObject = (COSObject) this.callCOSClientWithRetry(request);
        return cosObject.getObjectContent();
    } catch (CosServiceException e) {
        String errMsg = String.format("Retrieving the key %s with the byteRangeStart [%d] " + "occurs an exception: %s.", key, byteRangeStart, e);
        handleException(new Exception(errMsg), key);
    } catch (CosClientException e) {
        String errMsg = String.format("Retrieving key %s with the byteRangeStart [%d] and the byteRangeEnd [%d] " + "occurs an exception: %s.", key, byteRangeStart, byteRangeEnd, e);
        handleException(new Exception(errMsg), key);
    }
    return null;
}
Also used : CosServiceException(com.qcloud.cos.exception.CosServiceException) COSObject(com.qcloud.cos.model.COSObject) CosClientException(com.qcloud.cos.exception.CosClientException) 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)

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