Search in sources :

Example 1 with ObjectMetadata

use of com.qcloud.cos.internal.crypto.CryptoStorageMode.ObjectMetadata 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 2 with ObjectMetadata

use of com.qcloud.cos.internal.crypto.CryptoStorageMode.ObjectMetadata in project cos-java-sdk-v5 by tencentyun.

the class COSCryptoModuleBase method initiateMultipartUploadSecurely.

@Override
public InitiateMultipartUploadResult initiateMultipartUploadSecurely(InitiateMultipartUploadRequest req) {
    // Generate a one-time use symmetric key and initialize a cipher to
    // encrypt object data
    ContentCryptoMaterial cekMaterial = createContentCryptoMaterial(req);
    if (cryptoConfig.getStorageMode() == ObjectMetadata) {
        ObjectMetadata metadata = req.getObjectMetadata();
        if (metadata == null)
            metadata = new ObjectMetadata();
        long dataSize = req.getDataSize();
        long partSize = req.getPartSize();
        if (dataSize < 0 || partSize < 0) {
            throw new CosClientException("initiate multipart upload with encryption client must set dataSize and partSize");
        }
        if (partSize % 16 != 0) {
            throw new CosClientException("initiat multipart uplaod with encryption client must set part size a mutiple of 16" + "but got " + partSize);
        }
        metadata.addUserMetadata(Headers.ENCRYPTION_DATA_SIZE, Long.toString(dataSize));
        metadata.addUserMetadata(Headers.ENCRYPTION_PART_SIZE, Long.toString(partSize));
        // Store encryption info in metadata
        req.setObjectMetadata(updateMetadataWithContentCryptoMaterial(metadata, null, cekMaterial));
    }
    InitiateMultipartUploadResult result = cos.initiateMultipartUpload(req);
    MultipartUploadCryptoContext uploadContext = newUploadContext(req, cekMaterial);
    if (req instanceof MaterialsDescriptionProvider) {
        MaterialsDescriptionProvider p = (MaterialsDescriptionProvider) req;
        uploadContext.setMaterialsDescription(p.getMaterialsDescription());
    }
    multipartUploadContexts.put(result.getUploadId(), uploadContext);
    return result;
}
Also used : MaterialsDescriptionProvider(com.qcloud.cos.model.MaterialsDescriptionProvider) InitiateMultipartUploadResult(com.qcloud.cos.model.InitiateMultipartUploadResult) CosClientException(com.qcloud.cos.exception.CosClientException) ObjectMetadata(com.qcloud.cos.internal.crypto.CryptoStorageMode.ObjectMetadata) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 3 with ObjectMetadata

use of com.qcloud.cos.internal.crypto.CryptoStorageMode.ObjectMetadata in project cos-java-sdk-v5 by tencentyun.

the class COSCryptoModuleBase method updateInstructionPutRequest.

/**
 * Updates put request to store the specified instruction object in COS.
 *
 * @param req The put-instruction-file request for the instruction file to be stored in COS.
 * @param cekMaterial The instruction object to be stored in COS.
 * @return A put request to store the specified instruction object in COS.
 */
protected final PutObjectRequest updateInstructionPutRequest(PutObjectRequest req, ContentCryptoMaterial cekMaterial) {
    byte[] bytes = cekMaterial.toJsonString().getBytes(UTF8);
    ObjectMetadata metadata = req.getMetadata();
    if (metadata == null) {
        metadata = new ObjectMetadata();
        req.setMetadata(metadata);
    }
    // Set the content-length of the upload
    metadata.setContentLength(bytes.length);
    // Set the crypto instruction file header
    metadata.addUserMetadata(Headers.CRYPTO_INSTRUCTION_FILE, "");
    // Update the instruction request
    req.setMetadata(metadata);
    req.setInputStream(new ByteArrayInputStream(bytes));
    // routine
    return req;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectMetadata(com.qcloud.cos.internal.crypto.CryptoStorageMode.ObjectMetadata) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 4 with ObjectMetadata

use of com.qcloud.cos.internal.crypto.CryptoStorageMode.ObjectMetadata in project cos-java-sdk-v5 by tencentyun.

the class COSCryptoModuleBase method wrapWithCipher.

/**
 * Returns the given <code>PutObjectRequest</code> but has the content as input stream wrapped
 * with a cipher, and configured with some meta data and user metadata.
 */
protected final <R extends AbstractPutObjectRequest> R wrapWithCipher(final R request, ContentCryptoMaterial cekMaterial) {
    // Create a new metadata object if there is no metadata already.
    ObjectMetadata metadata = request.getMetadata();
    if (metadata == null) {
        metadata = new ObjectMetadata();
    }
    // Record the original Content MD5, if present, for the unencrypted data
    if (metadata.getContentMD5() != null) {
        metadata.addUserMetadata(Headers.ENCRYPTION_UNENCRYPTED_CONTENT_MD5, metadata.getContentMD5());
    }
    // Removes the original content MD5 if present from the meta data.
    metadata.setContentMD5(null);
    // Record the original, unencrypted content-length so it can be accessed
    // later
    final long plaintextLength = plaintextLength(request, metadata);
    if (plaintextLength >= 0) {
        metadata.addUserMetadata(Headers.ENCRYPTION_UNENCRYPTED_CONTENT_LENGTH, Long.toString(plaintextLength));
        metadata.setContentLength(ciphertextLength(plaintextLength));
    }
    request.setMetadata(metadata);
    request.setInputStream(newCOSCipherLiteInputStream(request, cekMaterial, plaintextLength));
    // Treat all encryption requests as input stream upload requests, not as
    // file upload requests.
    request.setFile(null);
    return request;
}
Also used : ObjectMetadata(com.qcloud.cos.internal.crypto.CryptoStorageMode.ObjectMetadata) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Aggregations

ObjectMetadata (com.qcloud.cos.internal.crypto.CryptoStorageMode.ObjectMetadata)4 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)4 ByteArrayInputStream (java.io.ByteArrayInputStream)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 AbstractPutObjectRequest (com.qcloud.cos.model.AbstractPutObjectRequest)1 COSObjectId (com.qcloud.cos.model.COSObjectId)1 InitiateMultipartUploadResult (com.qcloud.cos.model.InitiateMultipartUploadResult)1 InstructionFileId (com.qcloud.cos.model.InstructionFileId)1 MaterialsDescriptionProvider (com.qcloud.cos.model.MaterialsDescriptionProvider)1 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)1 InputStream (java.io.InputStream)1