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);
}
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);
}
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;
}
}
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);
}
}
Aggregations