-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathBucketInventory.cs
161 lines (142 loc) · 5.79 KB
/
BucketInventory.cs
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using COSXML.Common;
using COSXML.CosException;
using COSXML.Model;
using COSXML.Model.Object;
using COSXML.Model.Tag;
using COSXML.Model.Bucket;
using COSXML.Model.Service;
using COSXML.Utils;
using COSXML.Auth;
using COSXML.Transfer;
using System;
using COSXML;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace COSSnippet
{
public class BucketInventoryModel {
private CosXml cosXml;
BucketInventoryModel() {
CosXmlConfig config = new CosXmlConfig.Builder()
.SetRegion("COS_REGION") // 设置默认的区域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
.Build();
string secretId = "SECRET_ID"; // 云 API 密钥 SecretId, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi
string secretKey = "SECRET_KEY"; // 云 API 密钥 SecretKey, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi
long durationSecond = 600; //每次请求签名有效时长,单位为秒
QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,
secretKey, durationSecond);
this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);
}
/// 设置存储桶清单任务
public void PutBucketInventory()
{
//.cssg-snippet-body-start:[put-bucket-inventory]
try
{
string inventoryId = "aInventoryId";
// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer
string bucket = "examplebucket-1250000000";
PutBucketInventoryRequest putRequest = new PutBucketInventoryRequest(bucket, inventoryId);
putRequest.SetDestination("CSV", "100000000001", "examplebucket-1250000000", "ap-guangzhou","list1");
putRequest.IsEnable(true);
// 清单任务周期,枚举值:Daily、Weekly
putRequest.SetScheduleFrequency("Daily");
// 是否在清单中包含对象版本,枚举值:All、Current
putRequest.SetIncludedObjectVersions("All");
//执行请求
PutBucketInventoryResult putResult = cosXml.PutBucketInventory(putRequest);
//请求成功
Console.WriteLine(putResult.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
//.cssg-snippet-body-end
}
/// 获取存储桶清单任务
public void GetBucketInventory()
{
//.cssg-snippet-body-start:[get-bucket-inventory]
try
{
string inventoryId = "aInventoryId";
// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer
string bucket = "examplebucket-1250000000";
GetBucketInventoryRequest getRequest = new GetBucketInventoryRequest(bucket);
getRequest.SetInventoryId(inventoryId);
GetBucketInventoryResult getResult = cosXml.GetBucketInventory(getRequest);
InventoryConfiguration configuration = getResult.inventoryConfiguration;
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
//.cssg-snippet-body-end
}
/// 删除存储桶清单任务
public void DeleteBucketInventory()
{
//.cssg-snippet-body-start:[delete-bucket-inventory]
try
{
string inventoryId = "aInventoryId";
// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer
string bucket = "examplebucket-1250000000";
DeleteBucketInventoryRequest deleteRequest = new DeleteBucketInventoryRequest(bucket);
deleteRequest.SetInventoryId(inventoryId);
DeleteBucketInventoryResult deleteResult = cosXml.DeleteBucketInventory(deleteRequest);
//请求成功
Console.WriteLine(deleteResult.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
//.cssg-snippet-body-end
}
/// 列出所有存储桶清单任务
public void ListBucketInventory()
{
//.cssg-snippet-body-start:[list-bucket-inventory]
//.cssg-snippet-body-end
}
// .cssg-methods-pragma
static void Main(string[] args)
{
BucketInventoryModel m = new BucketInventoryModel();
/// 设置存储桶清单任务
m.PutBucketInventory();
/// 获取存储桶清单任务
m.GetBucketInventory();
/// 删除存储桶清单任务
m.DeleteBucketInventory();
/// 列出所有存储桶清单任务
m.ListBucketInventory();
// .cssg-methods-pragma
}
}
}