-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathBucketEncryptionDemo.java
74 lines (67 loc) · 2.74 KB
/
BucketEncryptionDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.qcloud.cos.demo;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.model.BucketEncryptionConfiguration;
import com.qcloud.cos.region.Region;
public class BucketEncryptionDemo {
private static String secretId = System.getenv("SECRETID");
private static String secretKey = System.getenv("SECRETKEY");
private static String bucketName = System.getenv("BUCKET_NAME");
private static String region = System.getenv("REGION");
private static COSClient cosClient = createCli();
public static void main(String[] args) {
try {
putBucketEncryption();
getBucketEncryption();
deleteBucketEncrytion();
} catch (Exception e) {
e.printStackTrace();
} finally {
cosClient.shutdown();
}
}
private static COSClient createCli() {
// 初始化用户身份信息(secretId, secretKey)
COSCredentials cred = new BasicCOSCredentials(secretId,secretKey);
// 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
ClientConfig clientConfig = new ClientConfig(new Region(region));
// 生成cos客户端
return new COSClient(cred, clientConfig);
}
private static void putBucketEncryption() {
BucketEncryptionConfiguration configuration = new BucketEncryptionConfiguration();
configuration.setSseAlgorithm("AES256");
try {
cosClient.putBucketEncryptionConfiguration(bucketName, configuration);
System.out.println("finish put bucket Encryption");
} catch (CosServiceException cse) {
cse.printStackTrace();
} catch (CosClientException cce) {
cce.printStackTrace();
}
}
private static void getBucketEncryption() {
try {
BucketEncryptionConfiguration configuration = cosClient.getBucketEncryptionConfiguration(bucketName);
System.out.println(configuration.getSseAlgorithm());
} catch (CosServiceException cse) {
cse.printStackTrace();
} catch (CosClientException cce) {
cce.printStackTrace();
}
}
private static void deleteBucketEncrytion() {
try {
cosClient.deleteBucketEncryptionConfiguration(bucketName);
System.out.println("finish delete bucket encrytion");
} catch (CosServiceException cse) {
cse.printStackTrace();
} catch (CosClientException cce) {
cce.printStackTrace();
}
}
}