11package com .qcloud .cos .demo ;
22
3- import java .util .LinkedList ;
43import java .util .List ;
54
65import com .qcloud .cos .COSClient ;
109import com .qcloud .cos .exception .CosClientException ;
1110import com .qcloud .cos .exception .CosServiceException ;
1211import 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 ;
1612import com .qcloud .cos .model .CannedAccessControlList ;
1713import 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 ;
2214import com .qcloud .cos .region .Region ;
2315
2416/**
2517 * 展示了创建bucket, 删除bucket, 查询bucket是否存在的demo
2618 *
2719 */
2820public 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