Skip to content

Commit b41199a

Browse files
author
markjrzhang
committed
modification conflict
2 parents 5cd6fec + 463153f commit b41199a

35 files changed

+960
-643
lines changed

src/main/java/com/qcloud/cos/ClientConfig.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ public class ClientConfig {
113113

114114
private boolean isCheckRequestPath = true;
115115

116+
private int timeout_client_thread_size = 0;
117+
118+
private int error_log_status_code_thresh = 500;
119+
116120
// 不传入region 用于后续调用List Buckets(获取所有的bucket信息)
117121
public ClientConfig() {
118122
super();
@@ -389,4 +393,20 @@ public void setCheckRequestPath(boolean isCheck) {
389393
public boolean isCheckRequestPath() {
390394
return isCheckRequestPath;
391395
}
396+
397+
public int getTimeoutClientThreadSize() {
398+
return timeout_client_thread_size;
399+
}
400+
401+
public void setTimeoutClientThreadSize(int pool_size) {
402+
timeout_client_thread_size = pool_size;
403+
}
404+
405+
public void setErrorLogStatusCodeThresh(int status_code) {
406+
error_log_status_code_thresh = status_code;
407+
}
408+
409+
public int getErrorLogStatusCodeThresh() {
410+
return error_log_status_code_thresh;
411+
}
392412
}

src/main/java/com/qcloud/cos/demo/AsymmetricKeyEncryptionClientDemo.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ public class AsymmetricKeyEncryptionClientDemo {
4747

4848
private static COSClient cosClient = createCosClient();
4949

50+
public static void main(String[] args) throws Exception {
51+
putObjectDemo();
52+
getObjectDemo();
53+
// 关闭
54+
cosClient.shutdown();
55+
}
56+
5057
private static COSClient createCosClient() {
5158
return createCosClient("ap-guangzhou");
5259
}
@@ -76,12 +83,6 @@ private static COSClient createCosClient(String region) {
7683
CryptoConfiguration cryptoConf = new CryptoConfiguration(CryptoMode.AesCtrEncryption)
7784
.withStorageMode(CryptoStorageMode.ObjectMetadata);
7885

79-
//// 如果 kms 服务的 region 与 cos 的 region 不一致,则在加密信息里指定 kms 服务的 region
80-
//cryptoConf.setKmsRegion(kmsRegion);
81-
82-
//// 如果需要可以为 KMS 服务的 cmk 设置对应的描述信息。
83-
//encryptionMaterials.addDescription("kms-region", "guangzhou");
84-
8586
// 生成加密客户端EncryptionClient, COSEncryptionClient是COSClient的子类, 所有COSClient支持的接口他都支持。
8687
// EncryptionClient覆盖了COSClient上传下载逻辑,操作内部会执行加密操作,其他操作执行逻辑和COSClient一致
8788
COSEncryptionClient cosEncryptionClient =
@@ -157,11 +158,4 @@ private static void deleteObjectDemo() {
157158
// 删除文件
158159
cosClient.deleteObject(bucketName, key);
159160
}
160-
161-
public static void main(String[] args) throws Exception {
162-
putObjectDemo();
163-
getObjectDemo();
164-
// 关闭
165-
cosClient.shutdown();
166-
}
167161
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.qcloud.cos.demo;
2+
3+
import com.qcloud.cos.COSClient;
4+
import com.qcloud.cos.ClientConfig;
5+
import com.qcloud.cos.auth.BasicCOSCredentials;
6+
import com.qcloud.cos.auth.COSCredentials;
7+
import com.qcloud.cos.model.BucketCrossOriginConfiguration;
8+
import com.qcloud.cos.model.CORSRule;
9+
import com.qcloud.cos.region.Region;
10+
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
14+
public class BucketCorsDemo {
15+
private static String bucketName = "examplebucket-1250000000";
16+
17+
private static COSClient cosClient = createClient();
18+
public static void main(String[] args) {
19+
try {
20+
setBucketCorsDemo();
21+
getBucketCorsDemo();
22+
deleteBucketCorsDemo();
23+
} catch (Exception e) {
24+
e.printStackTrace();
25+
} finally {
26+
cosClient.shutdown();
27+
}
28+
}
29+
30+
private static COSClient createClient() {
31+
// 1 初始化用户身份信息(appid, secretId, secretKey)
32+
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
33+
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
34+
ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
35+
// 3 生成cos客户端
36+
COSClient cosclient = new COSClient(cred, clientConfig);
37+
38+
return cosclient;
39+
}
40+
41+
private static void setBucketCorsDemo() {
42+
BucketCrossOriginConfiguration bucketCORS = new BucketCrossOriginConfiguration();
43+
List<CORSRule> corsRules = new ArrayList<CORSRule>();
44+
CORSRule corsRule = new CORSRule();
45+
// 规则名称
46+
corsRule.setId("set-bucket-cors-test");
47+
// 允许的 HTTP 方法
48+
corsRule.setAllowedMethods(CORSRule.AllowedMethods.PUT, CORSRule.AllowedMethods.GET, CORSRule.AllowedMethods.HEAD);
49+
corsRule.setAllowedHeaders("x-cos-grant-full-control");
50+
corsRule.setAllowedOrigins("http://mail.qq.com", "http://www.qq.com", "http://video.qq.com");
51+
corsRule.setExposedHeaders("x-cos-request-id");
52+
corsRule.setMaxAgeSeconds(60);
53+
corsRules.add(corsRule);
54+
bucketCORS.setRules(corsRules);
55+
cosClient.setBucketCrossOriginConfiguration(bucketName, bucketCORS);
56+
System.out.println("finish set bucket cors");
57+
}
58+
59+
private static void getBucketCorsDemo() {
60+
BucketCrossOriginConfiguration bucketCrossOriginConfiguration = cosClient.getBucketCrossOriginConfiguration(bucketName);
61+
List<CORSRule> corsRules = bucketCrossOriginConfiguration.getRules();
62+
for (CORSRule rule : corsRules) {
63+
List<CORSRule.AllowedMethods> allowedMethods = rule.getAllowedMethods();
64+
List<String> allowedHeaders = rule.getAllowedHeaders();
65+
List<String> allowedOrigins = rule.getAllowedOrigins();
66+
List<String> exposedHeaders = rule.getExposedHeaders();
67+
int maxAgeSeconds = rule.getMaxAgeSeconds();
68+
System.out.println("allow methods:" + allowedMethods);
69+
System.out.println("allow headers:" + allowedHeaders);
70+
System.out.println("allow origins:" + allowedOrigins);
71+
System.out.println("exposed headers:" + exposedHeaders);
72+
System.out.println("max age seconds:" + maxAgeSeconds);
73+
}
74+
}
75+
76+
private static void deleteBucketCorsDemo() {
77+
cosClient.deleteBucketCrossOriginConfiguration(bucketName);
78+
System.out.println("finish delete bucket cors");
79+
}
80+
}
Lines changed: 39 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.qcloud.cos.demo;
22

3-
import java.util.LinkedList;
43
import java.util.List;
54

65
import com.qcloud.cos.COSClient;
@@ -10,155 +9,74 @@
109
import com.qcloud.cos.exception.CosClientException;
1110
import com.qcloud.cos.exception.CosServiceException;
1211
import com.qcloud.cos.model.Bucket;
13-
import com.qcloud.cos.model.BucketLoggingConfiguration;
14-
import com.qcloud.cos.model.BucketTaggingConfiguration;
15-
import com.qcloud.cos.model.BucketVersioningConfiguration;
1612
import com.qcloud.cos.model.CannedAccessControlList;
1713
import com.qcloud.cos.model.CreateBucketRequest;
18-
import com.qcloud.cos.model.SetBucketLoggingConfigurationRequest;
19-
import com.qcloud.cos.model.SetBucketTaggingConfigurationRequest;
20-
import com.qcloud.cos.model.SetBucketVersioningConfigurationRequest;
21-
import com.qcloud.cos.model.TagSet;
2214
import com.qcloud.cos.region.Region;
2315

2416
/**
2517
* 展示了创建bucket, 删除bucket, 查询bucket是否存在的demo
2618
*
2719
*/
2820
public class BucketDemo {
29-
public static void main(String[] args) {
30-
listBuckets();
31-
}
32-
// 创建bucket
33-
private static void createBucketDemo() {
34-
// 1 初始化用户身份信息(appid, secretId, secretKey)
35-
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
36-
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
37-
ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1"));
38-
// 3 生成cos客户端
39-
COSClient cosclient = new COSClient(cred, clientConfig);
40-
// bucket名称, 需包含appid
41-
String bucketName = "publicreadbucket-12500000000";
42-
43-
CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
44-
// 设置bucket的权限为PublicRead(公有读私有写), 其他可选有私有读写, 公有读私有写
45-
createBucketRequest.setCannedAcl(CannedAccessControlList.PublicRead);
46-
Bucket bucket = cosclient.createBucket(createBucketRequest);
47-
48-
// 关闭客户端
49-
cosclient.shutdown();
50-
}
51-
52-
// 开启 bucket 版本控制
53-
private static void setBucketVersioning() {
54-
// 1 初始化用户身份信息(appid, secretId, secretKey)
55-
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
56-
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
57-
ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1"));
58-
// 3 生成cos客户端
59-
COSClient cosclient = new COSClient(cred, clientConfig);
60-
// bucket名称, 需包含appid
61-
String bucketName = "examplebucket-12500000000";
21+
private static String secretId = "AKIDXXXXXXXX";
22+
private static String secretKey = "1A2Z3YYYYYYYYYY";
23+
private static String cosRegion = "ap-guangzhou";
24+
private static String bucketName = "examplebucket-12500000000";
25+
private static COSClient cosClient = createCli();
6226

63-
// 开启版本控制
64-
BucketVersioningConfiguration bucketVersioningConfiguration = new BucketVersioningConfiguration(BucketVersioningConfiguration.ENABLED);
65-
// 关闭版本控制
66-
//BucketVersioningConfiguration bucketVersioningConfiguration = new BucketVersioningConfiguration(BucketVersioningConfiguration.SUSPENDED);
67-
SetBucketVersioningConfigurationRequest setBucketVersioningConfigurationRequest = new SetBucketVersioningConfigurationRequest(bucketName, bucketVersioningConfiguration);
68-
cosclient.setBucketVersioningConfiguration(setBucketVersioningConfigurationRequest);
69-
70-
cosclient.shutdown();
27+
public static void main(String[] args) {
28+
try {
29+
createBucketDemo();
30+
judgeBucketExistDemo();
31+
listBuckets();
32+
deleteBucketDemo();
33+
} catch (Exception e) {
34+
e.printStackTrace();
35+
} finally {
36+
cosClient.shutdown();
37+
}
7138
}
7239

73-
// 开启日志存储
74-
private static void setBucketLogging() {
40+
private static COSClient createCli() {
7541
// 1 初始化用户身份信息(appid, secretId, secretKey)
76-
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
42+
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
7743
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
78-
ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1"));
44+
ClientConfig clientConfig = new ClientConfig(new Region(cosRegion));
7945
// 3 生成cos客户端
8046
COSClient cosclient = new COSClient(cred, clientConfig);
81-
// bucket名称, 需包含appid
82-
String bucketName = "examplebucket-12500000000";
8347

84-
BucketLoggingConfiguration bucketLoggingConfiguration = new BucketLoggingConfiguration();
85-
// 设置日志存储的 bucket
86-
bucketLoggingConfiguration.setDestinationBucketName(bucketName);
87-
// 设置日志存储的前缀
88-
bucketLoggingConfiguration.setLogFilePrefix("logs/");
89-
SetBucketLoggingConfigurationRequest setBucketLoggingConfigurationRequest =
90-
new SetBucketLoggingConfigurationRequest(bucketName, bucketLoggingConfiguration);
91-
cosclient.setBucketLoggingConfiguration(setBucketLoggingConfigurationRequest);
48+
return cosclient;
9249
}
9350

94-
// 使用 bucket tag
95-
private static void setGetDeleteBucketTagging() {
96-
// 1 初始化用户身份信息(secretId, secretKey)
97-
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
98-
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
99-
ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
100-
// 3 生成cos客户端
101-
COSClient cosclient = new COSClient(cred, clientConfig);
102-
// bucket名需包含appid
103-
String bucketName = "mybucket-12500000000";
104-
List<TagSet> tagSetList = new LinkedList<TagSet>();
105-
TagSet tagSet = new TagSet();
106-
tagSet.setTag("age", "18");
107-
tagSet.setTag("name", "xiaoming");
108-
tagSetList.add(tagSet);
109-
BucketTaggingConfiguration bucketTaggingConfiguration = new BucketTaggingConfiguration();
110-
bucketTaggingConfiguration.setTagSets(tagSetList);
111-
SetBucketTaggingConfigurationRequest setBucketTaggingConfigurationRequest =
112-
new SetBucketTaggingConfigurationRequest(bucketName, bucketTaggingConfiguration);
113-
cosclient.setBucketTaggingConfiguration(setBucketTaggingConfigurationRequest);
114-
115-
cosclient.getBucketTaggingConfiguration(bucketName);
116-
cosclient.deleteBucketTaggingConfiguration(bucketName);
51+
// 创建bucket
52+
private static void createBucketDemo() {
53+
CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
54+
// 设置bucket的权限为PublicRead(公有读私有写), 其他可选有私有读写, 公有读私有写
55+
createBucketRequest.setCannedAcl(CannedAccessControlList.PublicRead);
56+
Bucket bucket = cosClient.createBucket(createBucketRequest);
57+
System.out.println("create bucket, bucketName is " + bucket.getName());
11758
}
11859

11960
// 删除bucket, 只用于空bucket, 含有数据的bucket需要在删除前清空删除。
12061
private static void deleteBucketDemo() {
121-
// 1 初始化用户身份信息(appid, secretId, secretKey)
122-
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
123-
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
124-
ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1"));
125-
// 3 生成cos客户端
126-
COSClient cosclient = new COSClient(cred, clientConfig);
127-
// bucket名称, 需包含appid
128-
String bucketName = "publicreadbucket-12500000000";
12962
// 删除bucket
130-
cosclient.deleteBucket(bucketName);
131-
132-
// 关闭客户端
133-
cosclient.shutdown();
63+
cosClient.deleteBucket(bucketName);
64+
System.out.println("delete bucket, bucketName is " + bucketName);
13465
}
13566

13667
// 查询bucket是否存在
13768
private static void judgeBucketExistDemo() {
138-
// 1 初始化用户身份信息(appid, secretId, secretKey)
139-
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
140-
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
141-
ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1"));
142-
// 3 生成cos客户端
143-
COSClient cosclient = new COSClient(cred, clientConfig);
144-
145-
String bucketName = "publicreadbucket-12500000000";
14669
// 判断bucket是否存在
147-
cosclient.doesBucketExist(bucketName);
148-
149-
// 关闭客户端
150-
cosclient.shutdown();
70+
boolean isExist = cosClient.doesBucketExist(bucketName);
71+
if (isExist) {
72+
System.out.println(bucketName + " is exist");
73+
} else {
74+
System.out.println(bucketName + " is not exist");
75+
}
15176
}
15277

15378
private static void listBuckets() {
154-
// 1 初始化用户身份信息(appid, secretId, secretKey)
155-
COSCredentials cred = new BasicCOSCredentials("AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "****************************");
156-
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
157-
ClientConfig clientConfig = new ClientConfig(new Region("ap-shanghai"));
158-
// 3 生成cos客户端
159-
COSClient cosclient = new COSClient(cred, clientConfig);
160-
161-
List<Bucket> buckets = cosclient.listBuckets();
79+
List<Bucket> buckets = cosClient.listBuckets();
16280

16381
for (Bucket bucket : buckets) {
16482
System.out.println(bucket.getName());
@@ -171,24 +89,15 @@ private static void listBuckets() {
17189

17290
//创多AZ桶
17391
private static void createMAZBucketDemo() {
174-
// 1 初始化用户身份信息(appid, secretId, secretKey)
175-
COSCredentials cred = new BasicCOSCredentials("AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "****************************");
176-
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
177-
ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
178-
// 3 生成cos客户端
179-
COSClient cosclient = new COSClient(cred, clientConfig);
180-
181-
String bucketname = "publicreadbucket-12500000000";
182-
CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketname);
92+
CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
18393

18494
try {
185-
cosclient.createMAZBucket(createBucketRequest);
95+
Bucket bucket = cosClient.createMAZBucket(createBucketRequest);
96+
System.out.println("create MAZ bucket, bucketName is " + bucket.getName());
18697
} catch (CosServiceException cse) {
18798
cse.printStackTrace();
18899
} catch (CosClientException cce) {
189100
cce.printStackTrace();
190-
} finally {
191-
cosclient.shutdown();
192101
}
193102
}
194103
}

0 commit comments

Comments
 (0)