API | 操作名 | 操作描述 |
设置存储桶 Referer | 设置存储桶 Referer 白名单或者黑名单 | |
查询存储桶 Referer | 查询存储桶 Referer 白名单或者黑名单 |
using COSXML.Model.Tag;using COSXML.Model.Bucket;using COSXML.Auth;using System;using COSXML;namespace COSSnippet{public class BucketRefererModel {private CosXml cosXml;BucketRefererModel() {CosXmlConfig config = new CosXmlConfig.Builder().SetRegion("COS_REGION") // 设置默认的地域, COS 地域的简称请参照 https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.Build();string secretId = "SECRET_ID"; // 云 API 密钥 SecretId, 获取 API 密钥请参照 https://console.tencentcloud.com/cam/capistring secretKey = "SECRET_KEY"; // 云 API 密钥 SecretKey, 获取 API 密钥请参照 https://console.tencentcloud.com/cam/capilong durationSecond = 600; //每次请求签名有效时长,单位为秒QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,secretKey, durationSecond);this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);}// 设置存储桶防盗链public void PutBucketReferer(){//.cssg-snippet-body-start:[put-bucket-cors]try{// 存储桶名称,此处填入格式必须为 BucketName-APPID, 其中 APPID 获取参考 https://console.tencentcloud.com/developerstring bucket = "examplebucket-1250000000";PutBucketRefererRequest request = new PutBucketRefererRequest(bucket);// 设置防盗链规则RefererConfiguration configuration = new RefererConfiguration();// 是否开启防盗链,枚举值:Enabled、Disabledconfiguration.Status = "Enabled";// 防盗链类型,枚举值:Black-List、White-Listconfiguration.RefererType = "White-List";// 生效域名列表,支持多个域名且为前缀匹配,支持带端口的域名和 IP, 支持通配符*,做二级域名或多级域名的通配configuration.domainList = new DomainList();// 单条生效域名 例如www.qq.com/example,192.168.1.2:8080, *.qq.comconfiguration.domainList.AddDomain("*.domain1.com");configuration.domainList.AddDomain("*.domain2.com");// 是否允许空 Referer 访问,枚举值:Allow、Deny,默认值为 Denyconfiguration.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());}//.cssg-snippet-body-end}// 获取存储桶防盗链规则public void GetBucketReferer(){//.cssg-snippet-body-start:[get-bucket-cors]try{// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.tencentcloud.com/developerstring 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());}//.cssg-snippet-body-end}// .cssg-methods-pragmastatic void Main(string[] args){BucketRefererModel m = new BucketRefererModel();// 设置存储桶跨域规则m.PutBucketReferer();// 获取存储桶跨域规则m.GetBucketReferer();// .cssg-methods-pragma}}}
本页内容是否解决了您的问题?