Search in sources :

Example 6 with PutObjectResult

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

the class AsymmetricKeyEncryptionClientDemo method putObjectDemo.

static void putObjectDemo() {
    // 上传文件
    // 这里给出putObject的示例, 对于高级API上传,只用在生成TransferManager时传入COSEncryptionClient对象即可
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
    PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
    System.out.println(putObjectResult.getRequestId());
}
Also used : PutObjectResult(com.qcloud.cos.model.PutObjectResult) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 7 with PutObjectResult

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

the class KMSEncryptionClientDemo method putObjectDemo.

static void putObjectDemo() {
    // 上传文件
    // 这里给出putObject的示例, 对于高级API上传,只用在生成TransferManager时传入COSEncryptionClient对象即可
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
    PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
    System.out.println(putObjectResult.getRequestId());
}
Also used : PutObjectResult(com.qcloud.cos.model.PutObjectResult) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 8 with PutObjectResult

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

the class KmsUploadDemo method SimpleUploadWithKmsMeta.

public static void SimpleUploadWithKmsMeta() {
    COSCredentials cred = new BasicCOSCredentials("SECRET_ID", "SECRET_KEY");
    // 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
    // 设置使用https请求
    clientConfig.setHttpProtocol(HttpProtocol.https);
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket名需包含appid
    String bucketName = "mybucket-1251668577";
    String key = "aaa/bbb.txt";
    File localFile = new File("/test.log");
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
    String kmsKeyId = "your-kms-key-id";
    String encryptionContext = Base64.encodeAsString("{\"Ssekmstest\":\"Ssekmstest\"}".getBytes());
    SSECOSKeyManagementParams ssecosKeyManagementParams = new SSECOSKeyManagementParams(kmsKeyId, encryptionContext);
    putObjectRequest.setSSECOSKeyManagementParams(ssecosKeyManagementParams);
    // 服务端加密场景下,返回的etag不再代表文件的md5,所以需要去掉客户端的md5校验
    // 如有需要,可获取crc64,自行校验
    System.setProperty(SkipMd5CheckStrategy.DISABLE_PUT_OBJECT_MD5_VALIDATION_PROPERTY, "true");
    try {
        PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
        // putobjectResult会返回文件的etag
        String etag = putObjectResult.getETag();
        String crc64 = putObjectResult.getCrc64Ecma();
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    }
    // 关闭客户端
    cosclient.shutdown();
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosServiceException(com.qcloud.cos.exception.CosServiceException) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) PutObjectResult(com.qcloud.cos.model.PutObjectResult) CosClientException(com.qcloud.cos.exception.CosClientException) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) File(java.io.File) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) SSECOSKeyManagementParams(com.qcloud.cos.model.SSECOSKeyManagementParams)

Example 9 with PutObjectResult

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

the class PicOperationDemo method PicPersistentProcessing.

public static void PicPersistentProcessing() {
    // 1 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("SECRET_ID", "SECRET_KEY");
    // 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // 4. bucket名需包含appid
    String bucketName = "examplebucket-1250000000";
    String key = "example.jpg";
    File localFile = new File("/data/example.jpg");
    // 5.对图像进行持久化处理
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setHeader("Pic-Operations", "{\"is_pic_info\":1,\"rules\":[{\"fileid\":\"example.png\",\"rule\":\"imageView2/format/png\"}]}");
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
    putObjectRequest.setMetadata(objectMetadata);
    try {
        PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
        // putobjectResult会返回文件的etag
        String etag = putObjectResult.getETag();
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    }
    // 关闭客户端
    cosclient.shutdown();
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosServiceException(com.qcloud.cos.exception.CosServiceException) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) PutObjectResult(com.qcloud.cos.model.PutObjectResult) CosClientException(com.qcloud.cos.exception.CosClientException) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) File(java.io.File) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 10 with PutObjectResult

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

the class AbstractCOSClientTest method putObjectFromLocalFileByInputStream.

protected static void putObjectFromLocalFileByInputStream(File localFile, long uploadSize, String uploadEtag, String key, ObjectMetadata objectMetadata) {
    if (!judgeUserInfoValid()) {
        return;
    }
    FileInputStream input = null;
    try {
        input = new FileInputStream(localFile);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, input, objectMetadata);
        putObjectRequest.setStorageClass(StorageClass.Standard_IA);
        PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
        String etag = putObjectResult.getETag();
        if (useClientEncryption) {
            assertEquals(false, uploadEtag.equals(etag));
        } else {
            assertEquals(true, uploadEtag.equals(etag));
        }
    } catch (IOException e) {
        fail(e.toString());
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : PutObjectResult(com.qcloud.cos.model.PutObjectResult) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Aggregations

PutObjectResult (com.qcloud.cos.model.PutObjectResult)34 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)22 File (java.io.File)21 COSClient (com.qcloud.cos.COSClient)14 ClientConfig (com.qcloud.cos.ClientConfig)14 Region (com.qcloud.cos.region.Region)14 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)11 COSCredentials (com.qcloud.cos.auth.COSCredentials)11 CosServiceException (com.qcloud.cos.exception.CosServiceException)11 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)11 CosClientException (com.qcloud.cos.exception.CosClientException)10 CIObject (com.qcloud.cos.model.ciModel.persistence.CIObject)4 CIUploadResult (com.qcloud.cos.model.ciModel.persistence.CIUploadResult)4 PicOperations (com.qcloud.cos.model.ciModel.persistence.PicOperations)4 IOException (java.io.IOException)4 LinkedList (java.util.LinkedList)4 Test (org.junit.Test)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 FileOperationException (run.halo.app.exception.FileOperationException)3