using COSXML.Model.Object;
using COSXML.Model.Tag;
using COSXML;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using COSXML.Auth;
using MiaoYu.Core.Cos.Configs;
using NPOI.SS.Formula.Functions;
using COSSTS;
using Newtonsoft.Json;
using System.Collections;
using TencentCloud.Tci.V20190318.Models;
namespace MiaoYu.Core.Cos.Services.Impl
{
///
/// 腾讯云
///
public class TencentCodeCosService : ICodeCosService//ISingletonDependency //: IScopedDependency//: ITransientDependency
{
//public TencentCodeCosService() { }
private CosXml cosXml;
private TencentCosConfig tencentCosConfig;
private TencentConfig tencentConfig;
CosXmlConfig config = null;
public TencentCodeCosService(TencentConfig tencentConfig)
{
this.tencentConfig = tencentConfig;
this.tencentCosConfig = tencentConfig.CosConfig;
if (config == null)
{
config = new CosXmlConfig.Builder()
//.SetRegion("COS_REGION") // 设置默认的区域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
.Build();
}
string secretId = tencentCosConfig.SecretId; // 云 API 密钥 SecretId, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi
string secretKey = tencentCosConfig.SecretKey; // 云 API 密钥 SecretKey, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi
long durationSecond = tencentCosConfig.DurationSecond; //每次请求签名有效时长,单位为秒
QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId,
secretKey, durationSecond);
this.cosXml = new CosXmlServer(config, qCloudCredentialProvider);
}
public (string sign, int expiredSeconds) GenerateSignURL(CosGenerateSign cosGenerateSign)
{
try
{
PreSignatureStruct preSignatureStruct = new PreSignatureStruct();
// APPID 获取参考 https://console.cloud.tencent.com/developer
preSignatureStruct.appid = tencentCosConfig.AppId;
// 存储桶所在地域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
preSignatureStruct.region = tencentCosConfig.Region;
// 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer
preSignatureStruct.bucket = tencentCosConfig.Bucket + "-" + tencentCosConfig.AppId;// "examplebucket-1250000000";
preSignatureStruct.key = "exampleobject"; //对象键
preSignatureStruct.httpMethod = "PUT"; //HTTP 请求方法
preSignatureStruct.isHttps = true; //生成 HTTPS 请求 URL
preSignatureStruct.signDurationSecond = tencentCosConfig.DurationSecond; //请求签名时间为 600s
preSignatureStruct.headers = null;//签名中需要校验的 header
preSignatureStruct.queryParameters = null; //签名中需要校验的 URL 中请求参数
//上传预签名 URL (使用永久密钥方式计算的签名 URL)
string requestSignURL = cosXml.GenerateSignURL(preSignatureStruct);
return new(requestSignURL, (int)tencentCosConfig.DurationSecond);
}
catch (COSXML.CosException.CosClientException clientEx)
{
//请求失败
Console.WriteLine("CosClientException: " + clientEx);
}
catch (COSXML.CosException.CosServerException serverEx)
{
//请求失败
Console.WriteLine("CosServerException: " + serverEx.GetInfo());
}
throw new NotImplementedException();
}
///
///
///
///
///
public CodeCosGenerateTemporaryKeyEntity GenerateTemporaryKey(CosGenerateSign cosGenerateSign)
{
#region 验证区域
if (string.IsNullOrEmpty(cosGenerateSign.Bucket))
{
cosGenerateSign.Bucket = tencentCosConfig.Bucket;
}
if (string.IsNullOrEmpty(cosGenerateSign.AppId))
{
cosGenerateSign.AppId = tencentCosConfig.AppId;
}
if (string.IsNullOrEmpty(cosGenerateSign.Region))
{
cosGenerateSign.Region = tencentCosConfig.Region;
}
#endregion
string bucket = cosGenerateSign.Bucket + "-" + cosGenerateSign.AppId; // 您的 bucket
string region = cosGenerateSign.Region;// bucket 所在区域
// 改成允许的路径前缀,根据自己网站的用户判断允许上传的路径,例子:a.jpg 或者 a/* 或者 * (通配符*存在重大安全风险, 谨慎评估使用)
string allowPrefix = "*";
/*
* 密钥的权限列表。必须在这里指定本次临时密钥所需要的权限。权限列表请参见 https://cloud.tencent.com/document/product/436/31923
* 规则为 {project}:{interfaceName}
* project : 产品缩写 cos相关授权为值为cos,数据万象(数据处理)相关授权值为ci
* 授权所有接口用*表示,例如 cos:*,ci:*
*/
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"
};
//设置参数
Dictionary values = new Dictionary();
values.Add("bucket", bucket);
values.Add("region", region);
values.Add("allowPrefix", allowPrefix);
// 也可以通过 allowPrefixes 指定路径前缀的集合
values.Add("allowPrefixes", new string[] {
string.IsNullOrEmpty(cosGenerateSign.Prefixes)?"miaoyu/*":cosGenerateSign.Prefixes,
});
values.Add("allowActions", allowActions);
values.Add("durationSeconds", 600);//指定临时证书的有效期, 参考 https://cloud.tencent.com/document/product/1312/48195
values.Add("secretId", tencentCosConfig.SecretId);
values.Add("secretKey", tencentCosConfig.SecretKey);
Dictionary credential = STSClient.genCredential(values); //返回值说明见README.md
var json = JsonConvert.SerializeObject(credential);
var person = JsonConvert.DeserializeObject(json);
return person;
}
}
}