HuanMengAdmin/admin-server/MiaoYu.Core.Cos/Services/Impl/TencentCodeCosService.cs
2024-07-22 16:03:12 +08:00

82 lines
3.9 KiB
C#

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;
namespace MiaoYu.Core.Cos.Services.Impl
{
/// <summary>
/// 腾讯云 TencentCosXmlConfig
/// </summary>
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();
}
}
}