|
9 | 9 | import com.qcloud.cos.model.ListBucketInventoryConfigurationsResult;
|
10 | 10 | import com.qcloud.cos.model.SetBucketInventoryConfigurationRequest;
|
11 | 11 | import com.qcloud.cos.model.DeleteBucketInventoryConfigurationRequest;
|
| 12 | +import com.qcloud.cos.model.PostBucketInventoryConfigurationResult; |
12 | 13 | import com.qcloud.cos.model.inventory.InventoryConfiguration;
|
13 | 14 | import com.qcloud.cos.model.inventory.InventoryCosBucketDestination;
|
14 | 15 | import com.qcloud.cos.model.inventory.InventoryFrequency;
|
|
26 | 27 | import java.util.List;
|
27 | 28 |
|
28 | 29 | public class BucketInventoryDemo {
|
| 30 | + private static String secretId = System.getenv("SECRETID"); |
| 31 | + private static String secretKey = System.getenv("SECRETKEY"); |
| 32 | + private static String bucketName = System.getenv("BUCKET_NAME"); |
| 33 | + private static String region = System.getenv("REGION"); |
| 34 | + private static COSClient cosClient = createCli(); |
| 35 | + |
29 | 36 | public static void main(String[] args) {
|
30 |
| - setGetDeleteBucketInventoryDemo(); |
31 |
| - setBucketInventoryDemo(); |
| 37 | + try { |
| 38 | + setGetDeleteBucketInventoryDemo(); |
| 39 | + setBucketInventoryDemo(); |
| 40 | +// postBucketInventoryDemo(); |
| 41 | + } catch (Exception e) { |
| 42 | + e.printStackTrace(); |
| 43 | + } finally { |
| 44 | + cosClient.shutdown(); |
| 45 | + } |
32 | 46 | }
|
33 | 47 |
|
34 |
| - private static void setGetDeleteBucketInventoryDemo() { |
35 |
| - // 1 初始化用户身份信息(secretId, secretKey) |
36 |
| - COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY"); |
37 |
| - // 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224 |
38 |
| - ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou")); |
39 |
| - // 3 生成cos客户端 |
40 |
| - COSClient cosclient = new COSClient(cred, clientConfig); |
41 |
| - // bucket名需包含appid |
42 |
| - String bucketName = "mybucket-12500000000"; |
| 48 | + private static COSClient createCli() { |
| 49 | + // 初始化用户身份信息(secretId, secretKey) |
| 50 | + COSCredentials cred = new BasicCOSCredentials(secretId,secretKey); |
| 51 | + // 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224 |
| 52 | + ClientConfig clientConfig = new ClientConfig(new Region(region)); |
| 53 | + // 生成cos客户端 |
| 54 | + return new COSClient(cred, clientConfig); |
| 55 | + } |
43 | 56 |
|
| 57 | + private static void setGetDeleteBucketInventoryDemo() { |
44 | 58 | InventoryConfiguration inventoryConfiguration = new InventoryConfiguration();
|
45 | 59 | InventoryCosBucketDestination inventoryCosBucketDestination = new InventoryCosBucketDestination();
|
46 | 60 | // 设置清单的输出目标存储桶的格式和前缀等
|
@@ -70,25 +84,25 @@ private static void setGetDeleteBucketInventoryDemo() {
|
70 | 84 | SetBucketInventoryConfigurationRequest setBucketInventoryConfigurationRequest = new SetBucketInventoryConfigurationRequest();
|
71 | 85 | setBucketInventoryConfigurationRequest.setBucketName(bucketName);
|
72 | 86 | setBucketInventoryConfigurationRequest.setInventoryConfiguration(inventoryConfiguration);
|
73 |
| - cosclient.setBucketInventoryConfiguration(setBucketInventoryConfigurationRequest); |
| 87 | + cosClient.setBucketInventoryConfiguration(setBucketInventoryConfigurationRequest); |
74 | 88 |
|
75 | 89 | inventoryConfiguration.setId("2");
|
76 | 90 | inventorySchedule.setFrequency(InventoryFrequency.Weekly);
|
77 |
| - cosclient.setBucketInventoryConfiguration(setBucketInventoryConfigurationRequest); |
| 91 | + cosClient.setBucketInventoryConfiguration(setBucketInventoryConfigurationRequest); |
78 | 92 |
|
79 | 93 | // 获取指定id的清单配置
|
80 |
| - GetBucketInventoryConfigurationResult getBucketInventoryConfigurationResult = cosclient.getBucketInventoryConfiguration(bucketName, "1"); |
| 94 | + GetBucketInventoryConfigurationResult getBucketInventoryConfigurationResult = cosClient.getBucketInventoryConfiguration(bucketName, "1"); |
81 | 95 |
|
82 | 96 | // 批量获取清单
|
83 | 97 | ListBucketInventoryConfigurationsRequest listBucketInventoryConfigurationsRequest = new ListBucketInventoryConfigurationsRequest();
|
84 | 98 | listBucketInventoryConfigurationsRequest.setBucketName(bucketName);
|
85 |
| - ListBucketInventoryConfigurationsResult listBucketInventoryConfigurationsResult = cosclient.listBucketInventoryConfigurations(listBucketInventoryConfigurationsRequest); |
| 99 | + ListBucketInventoryConfigurationsResult listBucketInventoryConfigurationsResult = cosClient.listBucketInventoryConfigurations(listBucketInventoryConfigurationsRequest); |
86 | 100 |
|
87 | 101 | // 删除指定清单
|
88 | 102 | DeleteBucketInventoryConfigurationRequest deleteBucketInventoryConfigurationRequest = new DeleteBucketInventoryConfigurationRequest();
|
89 | 103 | deleteBucketInventoryConfigurationRequest.setBucketName(bucketName);
|
90 | 104 | deleteBucketInventoryConfigurationRequest.setId("1");
|
91 |
| - cosclient.deleteBucketInventoryConfiguration(deleteBucketInventoryConfigurationRequest); |
| 105 | + cosClient.deleteBucketInventoryConfiguration(deleteBucketInventoryConfigurationRequest); |
92 | 106 | }
|
93 | 107 |
|
94 | 108 | private static void setBucketInventoryDemo() {
|
@@ -144,4 +158,34 @@ private static void setBucketInventoryDemo() {
|
144 | 158 | cosclient.setBucketInventoryConfiguration(request);
|
145 | 159 | cosclient.shutdown();
|
146 | 160 | }
|
| 161 | + |
| 162 | + private static void postBucketInventoryDemo() { |
| 163 | + InventoryConfiguration inventoryConfiguration = new InventoryConfiguration(); |
| 164 | + InventoryCosBucketDestination inventoryCosBucketDestination = new InventoryCosBucketDestination(); |
| 165 | + // 设置清单的输出目标存储桶的格式和前缀等 |
| 166 | + inventoryCosBucketDestination.setAccountId("100000000001"); |
| 167 | + inventoryCosBucketDestination.setBucketArn("qcs::cos:ap-guangzhou::mybucket-12500000000"); |
| 168 | + inventoryCosBucketDestination.setEncryption(new ServerSideEncryptionCOS()); |
| 169 | + inventoryCosBucketDestination.setFormat(InventoryFormat.CSV); |
| 170 | + inventoryCosBucketDestination.setPrefix("inventory-output"); |
| 171 | + InventoryDestination inventoryDestination = new InventoryDestination(); |
| 172 | + inventoryDestination.setCosBucketDestination(inventoryCosBucketDestination); |
| 173 | + inventoryConfiguration.setDestination(inventoryDestination); |
| 174 | + |
| 175 | + // 设置清单的调度周期,扫描前缀和id等 |
| 176 | + inventoryConfiguration.setId("4"); |
| 177 | + InventoryPrefixPredicate inventoryFilter = new InventoryPrefixPredicate("test/"); |
| 178 | + inventoryConfiguration.setInventoryFilter(new InventoryFilter(inventoryFilter)); |
| 179 | + inventoryConfiguration.setIncludedObjectVersions(InventoryIncludedObjectVersions.All); |
| 180 | + // 设置可选的输出字段 |
| 181 | + List<String> optionalFields = new LinkedList<String>(); |
| 182 | + optionalFields.add(InventoryOptionalField.Size.toString()); |
| 183 | + optionalFields.add(InventoryOptionalField.LastModifiedDate.toString()); |
| 184 | + inventoryConfiguration.setOptionalFields(optionalFields); |
| 185 | + SetBucketInventoryConfigurationRequest setBucketInventoryConfigurationRequest = new SetBucketInventoryConfigurationRequest(); |
| 186 | + setBucketInventoryConfigurationRequest.setBucketName(bucketName); |
| 187 | + setBucketInventoryConfigurationRequest.setInventoryConfiguration(inventoryConfiguration); |
| 188 | + PostBucketInventoryConfigurationResult result = cosClient.postBucketInventoryConfiguration(setBucketInventoryConfigurationRequest); |
| 189 | + System.out.println("finish post bucket inventory, jobId: " + result.getJobId() + ", reqId: " + result.getRequestId()); |
| 190 | + } |
147 | 191 | }
|
0 commit comments