-
Notifications
You must be signed in to change notification settings - Fork 186
/
GetKeyAndCredentials.cs
150 lines (128 loc) · 5.24 KB
/
GetKeyAndCredentials.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Net.Mail;
using COSSTS;
using Newtonsoft.Json;
using Formatting = System.Xml.Formatting;
namespace COSSnippet
{
public class GetKeyAndCredentials
{
//永久密钥
string secretId = "";
string secretKey = "";
string bucket = "bucket-125000000";
string appId = "125000000";
string region = "ap-guangzhou";
string filename = "test.jpg";
int time = 1800;
// 限制
Boolean limitExt = false; // 限制上传文件后缀
List<string> extWhiteList = new List<String> { "jpg", "jpeg", "png", "gif", "bmp" }; // 限制的上传后缀
Boolean limitContentType = false; // 限制上传 contentType
Boolean limitContentLength = false; // 限制上传文件大小
public string generateCosKey(string ext)
{
DateTime date = DateTime.Now;
int m = date.Month;
string ymd = $"{date.Year}{(m < 10 ? $"0{m}" : m.ToString())}{date.Day}";
Random random = new Random();
string r = random.Next(0, 1000000).ToString("D6"); // 生成6位随机数,前面补零
string cosKey = $"file/{ymd}/{ymd}_{r}.{(string.IsNullOrEmpty(ext) ? "" : ext)}";
return cosKey;
}
public Dictionary<string, object> getConfig()
{
Dictionary<string, object> config = new Dictionary<string, object>();
string[] allowActions = new string[] { // 允许的操作范围,这里以上传操作为例
"name/cos:PutObject",
"name/cos:PostObject",
"name/cos:InitiateMultipartUpload",
"name/cos:ListMultipartUploads",
"name/cos:ListParts",
"name/cos:UploadPart",
"name/cos:CompleteMultipartUpload",
};
string[] segments = filename.Split(".");
string ext = segments.Length > 0 ? segments[segments.Length - 1] : string.Empty;
string resource = $"qcs::cos:{region}:uid/{appId}:{bucket}/{generateCosKey(ext)}";
var condition = new Dictionary<string, object>();
// 1. 限制上传文件后缀
if (limitExt)
{
var extInvalid = string.IsNullOrEmpty(ext) || !extWhiteList.Contains(ext);
if (extInvalid)
{
Console.WriteLine("非法文件,禁止上传");
return null;
}
}
// 2. 限制上传文件 content-type
if (limitContentType)
{
condition["string_like"] = new Dictionary<string, string>
{
{ "cos:content-type", "image/*" } // 只允许上传 content-type 为图片类型
};
}
// 3. 限制上传文件大小
if (limitContentLength)
{
condition["numeric_less_than_equal"] = new Dictionary<string, long>
{
{ "cos:content-length", 5 * 1024 * 1024 } // 上传大小限制不能超过 5MB
};
}
var policy = new Dictionary<string, object>
{
{ "version", "2.0" },
{ "statement", new List<Dictionary<string, object>>
{
new Dictionary<string, object>
{
{ "action", allowActions },
{ "effect", "allow" },
{ "resource", new List<string>
{
resource,
}
},
{ "condition", condition }
}
}
}
};
// 序列化为 JSON 并输出
string jsonPolicy = JsonConvert.SerializeObject(policy);
config.Add("bucket", bucket);
config.Add("region", region);
config.Add("durationSeconds", time);
config.Add("secretId", secretId);
config.Add("secretKey", secretKey);
config.Add("policy", jsonPolicy);
return config;
}
// 获取联合身份临时访问凭证 https://cloud.tencent.com/document/product/1312/48195
public Dictionary<string, object> GetCredential()
{
var config = getConfig();
//获取临时密钥
Dictionary<string, object> credential = STSClient.genCredential(config);
return credential;
}
static void Main(string[] args)
{
GetKeyAndCredentials m = new GetKeyAndCredentials();
Dictionary<string, object> result = m.GetCredential();
Console.WriteLine("Credentials:" + result["Credentials"]);
Console.WriteLine("ExpiredTime:" + result["ExpiredTime"]);
Console.WriteLine("StartTime:" + result["StartTime"]);
}
}
}