-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathBucketReferer.cs
123 lines (112 loc) · 4.75 KB
/
BucketReferer.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
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 BucketRefererModel {
private CosXml cosXml;
BucketRefererModel() {
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 PutBucketReferer()
{
try
{
// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer
string bucket = "examplebucket-1250000000";
PutBucketRefererRequest request = new PutBucketRefererRequest(bucket);
// 设置防盗链规则
RefererConfiguration configuration = new RefererConfiguration();
configuration.Status = "Enabled"; // 是否开启防盗链,枚举值:Enabled、Disabled
configuration.RefererType = "White-List"; // 防盗链类型,枚举值:Black-List、White-List
// 生效域名列表, 支持多个域名且为前缀匹配, 支持带端口的域名和 IP, 支持通配符*,做二级域名或多级域名的通配
configuration.domainList = new DomainList();
// 单条生效域名 例如www.qq.com/example,192.168.1.2:8080, *.qq.com
configuration.domainList.AddDomain("*.domain1.com");
configuration.domainList.AddDomain("*.domain2.com");
// 是否允许空 Referer 访问,枚举值:Allow、Deny,默认值为 Deny
configuration.EmptyReferConfiguration = "Deny";
request.SetRefererConfiguration(configuration);
//执行请求
PutBucketRefererResult result = cosXml.PutBucketReferer(request);
//请求成功
Console.WriteLine(result.GetResultInfo());
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
}
/// 获取存储桶防盗链规则
public void GetBucketReferer()
{
try
{
// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer
string bucket = "examplebucket-1250000000";
GetBucketRefererRequest request = new GetBucketRefererRequest(bucket);
// 执行请求
GetBucketRefererResult result = cosXml.GetBucketReferer(request);
Console.WriteLine(result.GetResultInfo());
// Status参数
Console.WriteLine(result.refererConfiguration.Status);
// Referer名单类型
Console.WriteLine(result.refererConfiguration.RefererType);
// 名单中的域名列表
foreach (string domain in result.refererConfiguration.domainList.domains)
{
Console.WriteLine(domain);
}
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
}
static void Main(string[] args)
{
BucketRefererModel m = new BucketRefererModel();
/// 设置存储桶跨域规则
m.PutBucketReferer();
/// 获取存储桶跨域规则
m.GetBucketReferer();
}
}
}