Search in sources :

Example 21 with PutObjectRequest

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

the class QRCodeDemo method identifyQrCodeWithTransferManager.

public static void identifyQrCodeWithTransferManager(TransferManager transferManager) throws InterruptedException {
    // bucket名需包含appid
    // api 请参考 https://cloud.tencent.com/document/product/436/54070
    String bucketName = "examplebucket-1250000000";
    String key = "qrcode.png";
    File localFile = new File("E://qrcode.png");
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
    PicOperations picOperations = new PicOperations();
    picOperations.setIsPicInfo(1);
    List<PicOperations.Rule> ruleList = new LinkedList<>();
    PicOperations.Rule rule1 = new PicOperations.Rule();
    rule1.setBucket(bucketName);
    rule1.setFileId("qrcode-1.png");
    rule1.setRule("QRcode/cover/1");
    ruleList.add(rule1);
    picOperations.setRules(ruleList);
    putObjectRequest.setPicOperations(picOperations);
    Upload upload = transferManager.upload(putObjectRequest);
    UploadResult uploadResult = upload.waitForUploadResult();
    CIUploadResult ciUploadResult = uploadResult.getCiUploadResult();
    System.out.println(uploadResult.getRequestId());
    System.out.println(ciUploadResult.getOriginalInfo().getEtag());
    for (CIObject ciObject : ciUploadResult.getProcessResults().getObjectList()) {
        System.out.println(ciObject.getLocation());
    }
}
Also used : PicOperations(com.qcloud.cos.model.ciModel.persistence.PicOperations) Upload(com.qcloud.cos.transfer.Upload) CIUploadResult(com.qcloud.cos.model.ciModel.persistence.CIUploadResult) CompleteMultipartUploadResult(com.qcloud.cos.model.CompleteMultipartUploadResult) UploadResult(com.qcloud.cos.model.UploadResult) CIUploadResult(com.qcloud.cos.model.ciModel.persistence.CIUploadResult) InitiateMultipartUploadResult(com.qcloud.cos.model.InitiateMultipartUploadResult) File(java.io.File) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) LinkedList(java.util.LinkedList) CIObject(com.qcloud.cos.model.ciModel.persistence.CIObject)

Example 22 with PutObjectRequest

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

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

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

the class COSCryptoModuleBase method putObjectUsingMetadata.

private PutObjectResult putObjectUsingMetadata(PutObjectRequest req) {
    ContentCryptoMaterial cekMaterial = createContentCryptoMaterial(req);
    // Wraps the object data with a cipher input stream
    final File fileOrig = req.getFile();
    final InputStream isOrig = req.getInputStream();
    PutObjectRequest wrappedReq = wrapWithCipher(req, cekMaterial);
    // Update the metadata
    req.setMetadata(updateMetadataWithContentCryptoMaterial(req.getMetadata(), req.getFile(), cekMaterial));
    // Put the encrypted object into COS
    try {
        return cos.putObject(wrappedReq);
    } finally {
        cleanupDataSource(req, fileOrig, isOrig, wrappedReq.getInputStream(), log);
    }
}
Also used : 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) InstructionFile(com.qcloud.cos.internal.crypto.CryptoStorageMode.InstructionFile) File(java.io.File) AbstractPutObjectRequest(com.qcloud.cos.model.AbstractPutObjectRequest) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 25 with PutObjectRequest

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

the class COSCryptoModuleBase method putObjectUsingInstructionFile.

/**
 * Puts an encrypted object into COS, and puts an instruction file into COS. Encryption info is
 * stored in the instruction file.
 *
 * @param putObjectRequest The request object containing all the parameters to upload a new
 *        object to COS.
 * @return A {@link PutObjectResult} object containing the information returned by COS for
 *         the new, created object.
 */
private PutObjectResult putObjectUsingInstructionFile(PutObjectRequest putObjectRequest) {
    final File fileOrig = putObjectRequest.getFile();
    final InputStream isOrig = putObjectRequest.getInputStream();
    final PutObjectRequest putInstFileRequest = putObjectRequest.clone().withFile(null).withInputStream(null);
    putInstFileRequest.setKey(putInstFileRequest.getKey() + DOT + DEFAULT_INSTRUCTION_FILE_SUFFIX);
    // Create instruction
    ContentCryptoMaterial cekMaterial = createContentCryptoMaterial(putObjectRequest);
    // Wraps the object data with a cipher input stream; note the metadata
    // is mutated as a side effect.
    PutObjectRequest req = wrapWithCipher(putObjectRequest, cekMaterial);
    // Put the encrypted object into COS
    final PutObjectResult result;
    try {
        result = cos.putObject(req);
    } finally {
        cleanupDataSource(putObjectRequest, fileOrig, isOrig, req.getInputStream(), log);
    }
    // Put the instruction file into COS
    cos.putObject(updateInstructionPutRequest(putInstFileRequest, cekMaterial));
    // Return the result of the encrypted object PUT.
    return result;
}
Also used : PutObjectResult(com.qcloud.cos.model.PutObjectResult) 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) InstructionFile(com.qcloud.cos.internal.crypto.CryptoStorageMode.InstructionFile) File(java.io.File) AbstractPutObjectRequest(com.qcloud.cos.model.AbstractPutObjectRequest) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Aggregations

PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)45 File (java.io.File)30 PutObjectResult (com.qcloud.cos.model.PutObjectResult)25 CosServiceException (com.qcloud.cos.exception.CosServiceException)17 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)17 CosClientException (com.qcloud.cos.exception.CosClientException)15 COSClient (com.qcloud.cos.COSClient)13 ClientConfig (com.qcloud.cos.ClientConfig)13 Region (com.qcloud.cos.region.Region)13 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)12 COSCredentials (com.qcloud.cos.auth.COSCredentials)12 Upload (com.qcloud.cos.transfer.Upload)12 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)10 UploadResult (com.qcloud.cos.model.UploadResult)10 ByteArrayInputStream (java.io.ByteArrayInputStream)8 LinkedList (java.util.LinkedList)8 Test (org.junit.Test)8 CIObject (com.qcloud.cos.model.ciModel.persistence.CIObject)7 CIUploadResult (com.qcloud.cos.model.ciModel.persistence.CIUploadResult)7 PicOperations (com.qcloud.cos.model.ciModel.persistence.PicOperations)7