Skip to content

Commit 53e1214

Browse files
authored
Merge pull request #190 from tencentyun/dev/update_demo
update Demo
2 parents a46c0c9 + 26972e5 commit 53e1214

40 files changed

+275
-247
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
import com.qcloud.cos.auth.COSCredentials;
77
import com.qcloud.cos.exception.CosClientException;
88
import com.qcloud.cos.exception.CosServiceException;
9-
import com.qcloud.cos.model.*;
9+
import com.qcloud.cos.model.AppendObjectRequest;
10+
import com.qcloud.cos.model.AppendObjectResult;
1011
import com.qcloud.cos.region.Region;
1112

12-
import java.io.ByteArrayInputStream;
1313
import java.io.File;
14-
import java.io.InputStream;
1514

1615
public class AppendObjectDemo {
17-
public static void appendObjectFromLocal() {
16+
private static void appendObjectFromLocal() {
1817
// 1 初始化用户身份信息(secretId, secretKey)
1918
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
2019
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ public class AsymmetricKeyEncryptionClientDemo {
4141
private static final String priKeyPath = "pri.key";
4242
private static final SecureRandom srand = new SecureRandom();
4343

44-
static String bucketName = "mybucket-12500000000";
45-
static String key = "testKMS/asy.txt";
46-
static File localFile = new File("len1m.txt");
44+
private static String bucketName = "mybucket-12500000000";
45+
private static String key = "testKMS/asy.txt";
46+
private static File localFile = new File("len1m.txt");
4747

48-
static COSClient cosClient = createCosClient();
48+
private static COSClient cosClient = createCosClient();
4949

50-
static COSClient createCosClient() {
50+
private static COSClient createCosClient() {
5151
return createCosClient("ap-guangzhou");
5252
}
5353

54-
static COSClient createCosClient(String region) {
54+
private static COSClient createCosClient(String region) {
5555
// 初始化用户身份信息(secretId, secretKey)
5656
COSCredentials cred = new BasicCOSCredentials("AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxx",
5757
"yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
@@ -137,23 +137,23 @@ private static KeyPair loadAsymKeyPair()
137137
return new KeyPair(publicKey, privateKey);
138138
}
139139

140-
static void getObjectDemo() {
140+
private static void getObjectDemo() {
141141
// 下载文件
142142
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
143143
File downloadFile = new File("downAsy.txt");
144144
ObjectMetadata objectMetadata = cosClient.getObject(getObjectRequest, downloadFile);
145145
System.out.println(objectMetadata.getRequestId());
146146
}
147147

148-
static void putObjectDemo() {
148+
private static void putObjectDemo() {
149149
// 上传文件
150150
// 这里给出putObject的示例, 对于高级API上传,只用在生成TransferManager时传入COSEncryptionClient对象即可
151151
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
152152
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
153153
System.out.println(putObjectResult.getRequestId());
154154
}
155155

156-
static void deleteObjectDemo() {
156+
private static void deleteObjectDemo() {
157157
// 删除文件
158158
cosClient.deleteObject(bucketName, key);
159159
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
import java.util.List;
2121

2222
public class BucketCertificateDemo {
23+
public static void main(String[] args) {
24+
putGetDeleteBucketCertificate();
25+
}
2326

24-
public static void putGetDeleteBucketCertificate() {
27+
private static void putGetDeleteBucketCertificate() {
2528
// 1 初始化用户身份信息(secretId, secretKey)
2629
COSCredentials cred = new BasicCOSCredentials("xxxxxxxxxxxxxxxxxxxxxxxxxxxx", "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
2730
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
@@ -79,8 +82,4 @@ private static String getStreamContent(String filePath) throws IOException{
7982

8083
return stringBuilder.toString();
8184
}
82-
83-
public static void main(String[] args) {
84-
putGetDeleteBucketCertificate();
85-
}
8685
}

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@
2626
*
2727
*/
2828
public class BucketDemo {
29+
public static void main(String[] args) {
30+
listBuckets();
31+
}
2932
// 创建bucket
30-
public static void createBucketDemo() {
33+
private static void createBucketDemo() {
3134
// 1 初始化用户身份信息(appid, secretId, secretKey)
3235
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
3336
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
@@ -47,7 +50,7 @@ public static void createBucketDemo() {
4750
}
4851

4952
// 开启 bucket 版本控制
50-
public static void setBucketVersioning() {
53+
private static void setBucketVersioning() {
5154
// 1 初始化用户身份信息(appid, secretId, secretKey)
5255
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
5356
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
@@ -68,7 +71,7 @@ public static void setBucketVersioning() {
6871
}
6972

7073
// 开启日志存储
71-
public static void setBucketLogging() {
74+
private static void setBucketLogging() {
7275
// 1 初始化用户身份信息(appid, secretId, secretKey)
7376
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
7477
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
@@ -89,7 +92,7 @@ public static void setBucketLogging() {
8992
}
9093

9194
// 使用 bucket tag
92-
public static void setGetDeleteBucketTagging() {
95+
private static void setGetDeleteBucketTagging() {
9396
// 1 初始化用户身份信息(secretId, secretKey)
9497
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
9598
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
@@ -114,7 +117,7 @@ public static void setGetDeleteBucketTagging() {
114117
}
115118

116119
// 删除bucket, 只用于空bucket, 含有数据的bucket需要在删除前清空删除。
117-
public static void deleteBucketDemo() {
120+
private static void deleteBucketDemo() {
118121
// 1 初始化用户身份信息(appid, secretId, secretKey)
119122
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
120123
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
@@ -131,7 +134,7 @@ public static void deleteBucketDemo() {
131134
}
132135

133136
// 查询bucket是否存在
134-
public static void judgeBucketExistDemo() {
137+
private static void judgeBucketExistDemo() {
135138
// 1 初始化用户身份信息(appid, secretId, secretKey)
136139
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
137140
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
@@ -145,9 +148,9 @@ public static void judgeBucketExistDemo() {
145148

146149
// 关闭客户端
147150
cosclient.shutdown();
148-
}
151+
}
149152

150-
public static void listBuckets() {
153+
private static void listBuckets() {
151154
// 1 初始化用户身份信息(appid, secretId, secretKey)
152155
COSCredentials cred = new BasicCOSCredentials("AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "****************************");
153156
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
@@ -167,7 +170,7 @@ public static void listBuckets() {
167170
}
168171

169172
//创多AZ桶
170-
public static void createMAZBucketDemo() {
173+
private static void createMAZBucketDemo() {
171174
// 1 初始化用户身份信息(appid, secretId, secretKey)
172175
COSCredentials cred = new BasicCOSCredentials("AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "****************************");
173176
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
@@ -188,7 +191,4 @@ public static void createMAZBucketDemo() {
188191
cosclient.shutdown();
189192
}
190193
}
191-
public static void main(String[] args) {
192-
listBuckets();
193-
}
194194
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import com.qcloud.cos.region.Region;
1010

1111
public class BucketDomainDemo {
12-
public static void setGetDeleteBucketDomainDemo() {
12+
private static void setGetDeleteBucketDomainDemo() {
1313
// 1 初始化用户身份信息(secretId, secretKey)
1414
COSCredentials cred = new BasicCOSCredentials("AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
1515
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,29 @@
44
import com.qcloud.cos.ClientConfig;
55
import com.qcloud.cos.auth.BasicCOSCredentials;
66
import com.qcloud.cos.auth.COSCredentials;
7-
import com.qcloud.cos.model.*;
8-
import com.qcloud.cos.model.inventory.*;
7+
import com.qcloud.cos.model.GetBucketInventoryConfigurationResult;
8+
import com.qcloud.cos.model.ListBucketInventoryConfigurationsRequest;
9+
import com.qcloud.cos.model.ListBucketInventoryConfigurationsResult;
10+
import com.qcloud.cos.model.SetBucketInventoryConfigurationRequest;
11+
import com.qcloud.cos.model.DeleteBucketInventoryConfigurationRequest;
12+
import com.qcloud.cos.model.inventory.InventoryConfiguration;
13+
import com.qcloud.cos.model.inventory.InventoryCosBucketDestination;
914
import com.qcloud.cos.model.inventory.InventoryFrequency;
15+
import com.qcloud.cos.model.inventory.ServerSideEncryptionCOS;
16+
import com.qcloud.cos.model.inventory.InventoryFormat;
17+
import com.qcloud.cos.model.inventory.InventoryDestination;
18+
import com.qcloud.cos.model.inventory.InventorySchedule;
19+
import com.qcloud.cos.model.inventory.InventoryPrefixPredicate;
20+
import com.qcloud.cos.model.inventory.InventoryFilter;
21+
import com.qcloud.cos.model.inventory.InventoryIncludedObjectVersions;
22+
import com.qcloud.cos.model.inventory.InventoryOptionalField;
1023
import com.qcloud.cos.region.Region;
1124

1225
import java.util.LinkedList;
1326
import java.util.List;
1427

1528
public class BucketInventoryDemo {
16-
public static void setGetDeleteBucketInventoryDemo() {
29+
private static void setGetDeleteBucketInventoryDemo() {
1730
// 1 初始化用户身份信息(secretId, secretKey)
1831
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
1932
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
@@ -73,7 +86,7 @@ public static void setGetDeleteBucketInventoryDemo() {
7386
cosclient.deleteBucketInventoryConfiguration(deleteBucketInventoryConfigurationRequest);
7487
}
7588

76-
public static void setBucketInventoryDemo() {
89+
private static void setBucketInventoryDemo() {
7790
// 1 初始化用户身份信息(secretId, secretKey)
7891
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
7992
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818

1919
public class BucketLifecycleDemo {
20-
public String bucketName = "example-12500000000";
21-
public COSClient cosClient = COSBuilder();
20+
private String bucketName = "example-12500000000";
21+
private COSClient cosClient = COSBuilder();
2222

23-
public COSClient COSBuilder() {
23+
private COSClient COSBuilder() {
2424
// 初始化用户身份信息(secretId, secretKey)
2525
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
2626
// 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
@@ -29,7 +29,7 @@ public COSClient COSBuilder() {
2929
return new COSClient(cred, clientConfig);
3030
}
3131

32-
public void addLifeCycle(String id,String path){
32+
private void addLifeCycle(String id,String path) {
3333
List<BucketLifecycleConfiguration.Rule> rules = new ArrayList<BucketLifecycleConfiguration.Rule>();
3434
// 规则1 30天后删除路径以 hongkong_movie/ 为开始的文件
3535
BucketLifecycleConfiguration.Rule deletePrefixRule = new BucketLifecycleConfiguration.Rule();
@@ -64,7 +64,7 @@ public void addLifeCycle(String id,String path){
6464
// 设置生命周期
6565
cosClient.setBucketLifecycleConfiguration(setBucketLifecycleConfigurationRequest);
6666
}
67-
public void queryPath(){
67+
private void queryPath() {
6868
// 存储桶的命名格式为 BucketName-APPID ,此处填写的存储桶名称必须为此格式
6969
BucketLifecycleConfiguration queryLifeCycleRet =
7070
cosClient.getBucketLifecycleConfiguration(bucketName);
@@ -79,7 +79,7 @@ public void queryPath(){
7979
}
8080
}
8181

82-
public Boolean deletedAllLifeCycle(){
82+
private Boolean deletedAllLifeCycle() {
8383
COSClient cosClient = COSBuilder();
8484
cosClient.deleteBucketLifecycleConfiguration(bucketName);
8585
return true;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
import com.qcloud.cos.ClientConfig;
55
import com.qcloud.cos.auth.BasicCOSCredentials;
66
import com.qcloud.cos.auth.COSCredentials;
7-
import com.qcloud.cos.model.*;
7+
import com.qcloud.cos.model.BucketLoggingConfiguration;
8+
import com.qcloud.cos.model.SetBucketLoggingConfigurationRequest;
89
import com.qcloud.cos.region.Region;
910

1011
public class BucketLoggingDemo {
11-
public static void setGetBucketLoggingDemo() {
12+
private static void setGetBucketLoggingDemo() {
1213
// 1 初始化用户身份信息(secretId, secretKey)
1314
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
1415
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99

1010

1111
public class BucketPolicyDemo {
12-
public static void setGetBucketPolicy() {
12+
public static void main(String[] args) {
13+
setGetBucketPolicy();
14+
}
15+
16+
private static void setGetBucketPolicy() {
1317
// 1 初始化用户身份信息(secretId, secretKey)
1418
COSCredentials cred = new BasicCOSCredentials("SECRET_ID", "SECRET_KEY");
1519
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
@@ -41,8 +45,5 @@ public static void setGetBucketPolicy() {
4145
BucketPolicy bucketPolicy = cosclient.getBucketPolicy(bucketName);
4246
System.out.println(bucketPolicy.getPolicyText());
4347
}
44-
public static void main(String[] args) {
45-
setGetBucketPolicy();
46-
}
4748
}
4849

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,20 @@
88
import com.qcloud.cos.region.Region;
99

1010
public class BucketRefererDemo {
11-
public static COSClient cosClient = null;
11+
private static COSClient cosClient = null;
1212
// bucket名需包含appid
13-
public static String bucketName = "examplebucket-12500000000";
13+
private static String bucketName = "examplebucket-12500000000";
1414

15-
public static void createCosClient() {
15+
public static void main(String[] args) {
16+
createCosClient();
17+
18+
setBucketReferer();
19+
getBucketReferer();
20+
21+
shutdown();
22+
}
23+
24+
private static void createCosClient() {
1625
// 1 初始化用户身份信息(secretId, secretKey)
1726
COSCredentials cred = new BasicCOSCredentials("AKID********************************", "********************************");
1827
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
@@ -21,14 +30,14 @@ public static void createCosClient() {
2130
cosClient = new COSClient(cred, clientConfig);
2231
}
2332

24-
public static void shutdown() {
33+
private static void shutdown() {
2534
if (cosClient != null) {
2635
cosClient.shutdown();
2736
cosClient = null;
2837
}
2938
}
3039

31-
public static void setBucketReferer() {
40+
private static void setBucketReferer() {
3241
BucketRefererConfiguration configuration = new BucketRefererConfiguration();
3342

3443
// 启用防盗链
@@ -49,7 +58,7 @@ public static void setBucketReferer() {
4958
cosClient.setBucketRefererConfiguration(bucketName, configuration);
5059
}
5160

52-
public static void getBucketReferer() {
61+
private static void getBucketReferer() {
5362
BucketRefererConfiguration configuration = cosClient.getBucketRefererConfiguration(bucketName);
5463

5564
if (configuration == null) {
@@ -65,13 +74,4 @@ public static void getBucketReferer() {
6574
System.out.printf("domain: %s\n", domain);
6675
}
6776
}
68-
69-
public static void main(String[] args) {
70-
createCosClient();
71-
72-
setBucketReferer();
73-
getBucketReferer();
74-
75-
shutdown();
76-
}
7777
}

0 commit comments

Comments
 (0)