Search in sources :

Example 31 with ObjectMetadata

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

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

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

the class COSObjectWrapper method hasEncryptionInfo.

/**
 * Returns true if this COS object has the encryption information stored as user meta data; false
 * otherwise.
 */
final boolean hasEncryptionInfo() {
    ObjectMetadata metadata = cosobj.getObjectMetadata();
    Map<String, String> userMeta = metadata.getUserMetadata();
    return userMeta != null && (userMeta.containsKey(Headers.CRYPTO_IV) || userMeta.containsKey(Headers.ENCRYPTION_START)) && (userMeta.containsKey(Headers.CRYPTO_KEY_V2) || userMeta.containsKey(Headers.CRYPTO_KEY)) || userMeta.containsKey(Headers.ENCRYPTION_KEY);
}
Also used : ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 34 with ObjectMetadata

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

the class COSObjectWrapper method encryptionSchemeOf.

/**
 * Returns the original crypto scheme used for encryption, which may differ from the crypto
 * scheme used for decryption during, for example, a range-get operation.
 *
 * @param instructionFile the instruction file of the cos object; or null if there is none.
 */
ContentCryptoScheme encryptionSchemeOf(Map<String, String> instructionFile) {
    if (instructionFile != null) {
        String cekAlgo = instructionFile.get(Headers.CRYPTO_CEK_ALGORITHM);
        return ContentCryptoScheme.fromCEKAlgo(cekAlgo);
    }
    ObjectMetadata meta = cosobj.getObjectMetadata();
    Map<String, String> userMeta = meta.getUserMetadata();
    String cekAlgo = userMeta.get(Headers.CRYPTO_CEK_ALGORITHM);
    return ContentCryptoScheme.fromCEKAlgo(cekAlgo);
}
Also used : ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 35 with ObjectMetadata

use of com.qcloud.cos.model.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)

Aggregations

ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)63 File (java.io.File)21 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)15 COSClient (com.qcloud.cos.COSClient)14 ClientConfig (com.qcloud.cos.ClientConfig)14 Region (com.qcloud.cos.region.Region)14 CosClientException (com.qcloud.cos.exception.CosClientException)13 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)12 COSCredentials (com.qcloud.cos.auth.COSCredentials)12 PutObjectResult (com.qcloud.cos.model.PutObjectResult)12 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)11 Test (org.junit.Test)11 CosServiceException (com.qcloud.cos.exception.CosServiceException)10 ByteArrayInputStream (java.io.ByteArrayInputStream)8 IOException (java.io.IOException)8 InputStream (java.io.InputStream)7 GetObjectMetadataRequest (com.qcloud.cos.model.GetObjectMetadataRequest)6 UploadResult (com.qcloud.cos.model.UploadResult)5 TransferManager (com.qcloud.cos.transfer.TransferManager)5 Upload (com.qcloud.cos.transfer.Upload)5