Search in sources :

Example 26 with ClientConfig

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

the class BucketReplicationDemo method createCOSClient.

public static void createCOSClient() {
    COSCredentials cred = new BasicCOSCredentials("AKID********************************", "********************************");
    // 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-shanghai"));
    // 3 生成cos客户端
    cosClient = new COSClient(cred, clientConfig);
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 27 with ClientConfig

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

the class BucketTaggingDemo method SetGetDeleteBucketTagging.

public static void SetGetDeleteBucketTagging() {
    // 1 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
    // 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);
    // bucket名需包含appid
    String bucketName = "mybucket-1251668577";
    List<TagSet> tagSetList = new LinkedList<TagSet>();
    TagSet tagSet = new TagSet();
    tagSet.setTag("age", "18");
    tagSet.setTag("name", "xiaoming");
    tagSetList.add(tagSet);
    BucketTaggingConfiguration bucketTaggingConfiguration = new BucketTaggingConfiguration();
    bucketTaggingConfiguration.setTagSets(tagSetList);
    SetBucketTaggingConfigurationRequest setBucketTaggingConfigurationRequest = new SetBucketTaggingConfigurationRequest(bucketName, bucketTaggingConfiguration);
    cosclient.setBucketTaggingConfiguration(setBucketTaggingConfigurationRequest);
    BucketTaggingConfiguration bucketTaggingConfiguration1 = cosclient.getBucketTaggingConfiguration(bucketName);
    cosclient.deleteBucketTaggingConfiguration(bucketName);
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BucketTaggingConfiguration(com.qcloud.cos.model.BucketTaggingConfiguration) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) TagSet(com.qcloud.cos.model.TagSet) Region(com.qcloud.cos.region.Region) SetBucketTaggingConfigurationRequest(com.qcloud.cos.model.SetBucketTaggingConfigurationRequest) ClientConfig(com.qcloud.cos.ClientConfig) LinkedList(java.util.LinkedList)

Example 28 with ClientConfig

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

the class BucketTaggingDemo method SetTagWhilePutObject.

public static void SetTagWhilePutObject() {
    // 1 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
    // 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-shanghai"));
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket名需包含appid
    String bucketName = "mybucket-1251668577";
    String key = "testTag";
    InputStream is = new ByteArrayInputStream(new byte[] { 'd', 'a', 't', 'a' });
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setHeader("x-cos-tagging", "level=info");
    cosclient.putObject(bucketName, key, is, objectMetadata);
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 29 with ClientConfig

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

the class BucketWebsiteDemo method SetGetDeleteWebisteDemo.

public static void SetGetDeleteWebisteDemo() {
    // 1 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
    // 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);
    // bucket名需包含appid
    String bucketName = "mybucket-1251668577";
    // 设置bucket website
    BucketWebsiteConfiguration bucketWebsiteConfiguration = new BucketWebsiteConfiguration();
    // 索引文件
    bucketWebsiteConfiguration.setIndexDocumentSuffix("index.html");
    // 路由规则
    List<RoutingRule> routingRuleList = new ArrayList<RoutingRule>();
    RoutingRule routingRule = new RoutingRule();
    RoutingRuleCondition routingRuleCondition = new RoutingRuleCondition();
    routingRuleCondition.setHttpErrorCodeReturnedEquals("404");
    routingRule.setCondition(routingRuleCondition);
    RedirectRule redirectRule = new RedirectRule();
    redirectRule.setProtocol("https");
    redirectRule.setReplaceKeyPrefixWith("404.html");
    routingRule.setRedirect(redirectRule);
    routingRuleList.add(routingRule);
    bucketWebsiteConfiguration.setRoutingRules(routingRuleList);
    cosclient.setBucketWebsiteConfiguration(bucketName, bucketWebsiteConfiguration);
    // 获取bucket website
    BucketWebsiteConfiguration bucketWebsiteConfiguration1 = cosclient.getBucketWebsiteConfiguration(bucketName);
    // 删除bucket website
    cosclient.deleteBucketWebsiteConfiguration(bucketName);
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) ArrayList(java.util.ArrayList) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 30 with ClientConfig

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

the class KmsUploadDemo method CopyObjectWithKmsMeta.

public static void CopyObjectWithKmsMeta() {
    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 kmsKeyId = "your-kms-key-id";
    String encryptionContext = Base64.encodeAsString("{\"Ssekmstest\":\"Ssekmstest\"}".getBytes());
    // 要拷贝的bucket region, 支持跨园区拷贝
    Region srcBucketRegion = new Region("ap-guangzhou");
    // 源bucket, bucket名需包含appid
    String srcBucketName = "mybucket-1251668577";
    // 要拷贝的源文件
    String srcKey = "aaa/bbb.txt";
    // 目的bucket, bucket名需包含appid
    String destBucketName = "mybucket-1251668577";
    // 要拷贝的目的文件
    String destKey = "ccc/ddd.txt";
    CopyObjectRequest copyObjectRequest = new CopyObjectRequest(srcBucketRegion, srcBucketName, srcKey, destBucketName, destKey);
    copyObjectRequest.setSSECOSKeyManagementParams(new SSECOSKeyManagementParams(kmsKeyId, encryptionContext));
    try {
        CopyObjectResult copyObjectResult = cosclient.copyObject(copyObjectRequest);
        String crc64 = copyObjectResult.getCrc64Ecma();
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    }
    // 关闭客户端
    cosclient.shutdown();
}
Also used : COSClient(com.qcloud.cos.COSClient) CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosServiceException(com.qcloud.cos.exception.CosServiceException) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CopyObjectResult(com.qcloud.cos.model.CopyObjectResult) CosClientException(com.qcloud.cos.exception.CosClientException) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) SSECOSKeyManagementParams(com.qcloud.cos.model.SSECOSKeyManagementParams)

Aggregations

ClientConfig (com.qcloud.cos.ClientConfig)107 COSClient (com.qcloud.cos.COSClient)103 Region (com.qcloud.cos.region.Region)103 COSCredentials (com.qcloud.cos.auth.COSCredentials)99 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)96 CosClientException (com.qcloud.cos.exception.CosClientException)40 CosServiceException (com.qcloud.cos.exception.CosServiceException)38 File (java.io.File)22 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)14 PutObjectResult (com.qcloud.cos.model.PutObjectResult)14 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)13 TransferManager (com.qcloud.cos.transfer.TransferManager)13 ExecutorService (java.util.concurrent.ExecutorService)13 AnonymousCOSCredentials (com.qcloud.cos.auth.AnonymousCOSCredentials)7 CopyObjectRequest (com.qcloud.cos.model.CopyObjectRequest)7 LinkedList (java.util.LinkedList)7 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)6 FileOperationException (run.halo.app.exception.FileOperationException)6 IOException (java.io.IOException)5 CopyResult (com.qcloud.cos.model.CopyResult)4