Search in sources :

Example 1 with COSObjectId

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

the class COSEncryptionClient method deleteObject.

@Override
public void deleteObject(DeleteObjectRequest req) {
    // Delete the object
    super.deleteObject(req);
    // If it exists, delete the instruction file.
    InstructionFileId ifid = new COSObjectId(req.getBucketName(), req.getKey()).instructionFileId();
    DeleteObjectRequest instructionDeleteRequest = (DeleteObjectRequest) req.clone();
    instructionDeleteRequest.withBucketName(ifid.getBucket()).withKey(ifid.getKey());
    super.deleteObject(instructionDeleteRequest);
}
Also used : DeleteObjectRequest(com.qcloud.cos.model.DeleteObjectRequest) COSObjectId(com.qcloud.cos.model.COSObjectId) InstructionFileId(com.qcloud.cos.model.InstructionFileId)

Example 2 with COSObjectId

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

the class COSCryptoModuleBase method createInstructionPutRequest.

protected final PutObjectRequest createInstructionPutRequest(String bucketName, String key, ContentCryptoMaterial cekMaterial) {
    byte[] bytes = cekMaterial.toJsonString().getBytes(UTF8);
    InputStream is = new ByteArrayInputStream(bytes);
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(bytes.length);
    metadata.addUserMetadata(Headers.CRYPTO_INSTRUCTION_FILE, "");
    InstructionFileId ifileId = new COSObjectId(bucketName, key).instructionFileId();
    return new PutObjectRequest(ifileId.getBucket(), ifileId.getKey(), is, metadata);
}
Also used : COSObjectId(com.qcloud.cos.model.COSObjectId) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ResettableInputStream(com.qcloud.cos.internal.ResettableInputStream) ReleasableInputStream(com.qcloud.cos.internal.ReleasableInputStream) SdkFilterInputStream(com.qcloud.cos.internal.SdkFilterInputStream) LengthCheckInputStream(com.qcloud.cos.internal.LengthCheckInputStream) InputStream(java.io.InputStream) InstructionFileId(com.qcloud.cos.model.InstructionFileId) ObjectMetadata(com.qcloud.cos.internal.crypto.CryptoStorageMode.ObjectMetadata) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) AbstractPutObjectRequest(com.qcloud.cos.model.AbstractPutObjectRequest) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 3 with COSObjectId

use of com.qcloud.cos.model.COSObjectId 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 4 with COSObjectId

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

the class COSCryptoModuleAE method decipherWithInstFileSuffix.

/**
 * Same as {@link #decipher(GetObjectRequest, long[], long[], COSObject)} but makes use of an
 * instruction file with the specified suffix.
 *
 * @param instFileSuffix never null or empty (which is assumed to have been sanitized upstream.)
 */
private COSObject decipherWithInstFileSuffix(GetObjectRequest req, long[] desiredRange, long[] cryptoRange, COSObject retrieved, String instFileSuffix) {
    final COSObjectId id = req.getCOSObjectId();
    // Check if encrypted info is in an instruction file
    final COSObjectWrapper ifile = fetchInstructionFile(id, instFileSuffix);
    if (ifile == null) {
        throw new CosClientException("Instruction file with suffix " + instFileSuffix + " is not found for " + retrieved);
    }
    try {
        return decipherWithInstructionFile(req, desiredRange, cryptoRange, new COSObjectWrapper(retrieved, id), ifile);
    } finally {
        IOUtils.closeQuietly(ifile, log);
    }
}
Also used : COSObjectId(com.qcloud.cos.model.COSObjectId) CosClientException(com.qcloud.cos.exception.CosClientException)

Aggregations

COSObjectId (com.qcloud.cos.model.COSObjectId)4 AbstractPutObjectRequest (com.qcloud.cos.model.AbstractPutObjectRequest)2 InstructionFileId (com.qcloud.cos.model.InstructionFileId)2 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)2 CosClientException (com.qcloud.cos.exception.CosClientException)1 LengthCheckInputStream (com.qcloud.cos.internal.LengthCheckInputStream)1 ReleasableInputStream (com.qcloud.cos.internal.ReleasableInputStream)1 ResettableInputStream (com.qcloud.cos.internal.ResettableInputStream)1 SdkFilterInputStream (com.qcloud.cos.internal.SdkFilterInputStream)1 ObjectMetadata (com.qcloud.cos.internal.crypto.CryptoStorageMode.ObjectMetadata)1 COSObject (com.qcloud.cos.model.COSObject)1 DeleteObjectRequest (com.qcloud.cos.model.DeleteObjectRequest)1 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)1 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1