添加cos

This commit is contained in:
zpc 2024-07-22 16:03:12 +08:00
parent 4b63f22993
commit b0884a56d7
21 changed files with 438 additions and 18 deletions

View File

@ -43,7 +43,15 @@ export default class TImageConfigService {
static findForm(id?: string | undefined) {
return Http.get(`${this.urlPrefix}/findForm${(id ? '/' + id : '')}`)
}
/**
* key
*
* @param
* @returns
*/
static getCosSign() {
return Http.get(`${this.urlPrefix}/GetCosSign`)
}
/**
*
*

View File

@ -3,6 +3,10 @@ import { reactive, ref } from "vue";
import { FormInstance } from "ant-design-vue";
import Tools from "@/core/utils/Tools";
import TImageConfigService from "@/services/apps/T_Image_Configs/TImageConfigService";
import { UploadOutlined } from '@ant-design/icons-vue';
import { message } from 'ant-design-vue';
import type { UploadProps } from 'ant-design-vue';
//
const props = defineProps<{ onSuccess: () => void }>();
@ -59,6 +63,24 @@ function save() {
}
});
}
const fileList = ref<UploadProps['fileList']>([]);
const uploading = ref<boolean>(false);
const handleRemove: UploadProps['onRemove'] = file => {
const index = fileList.value.indexOf(file);
const newFileList = fileList.value.slice();
newFileList.splice(index, 1);
fileList.value = newFileList;
};
const beforeUpload: UploadProps['beforeUpload'] = file => {
fileList.value = [...(fileList.value || []), file];
console.log(fileList.value);
return false;
};
</script>
<template>
@ -90,6 +112,21 @@ function save() {
<a-input v-model:value="state.vm.form.url" placeholder="请输入" />
</a-form-item>
</a-col>
<a-col>
<a-upload-dragger
:file-list="fileList" :before-upload="beforeUpload" @remove="handleRemove"
name="file"
:multiple="false"
>
<p class="ant-upload-drag-icon">
<inbox-outlined></inbox-outlined>
</p>
<p class="ant-upload-text">点击上传文件</p>
</a-upload-dragger>
</a-col>
</a-row>
</a-form>
</a-spin>

View File

@ -52,6 +52,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.editorconfig = .editorconfig
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiaoYu.Core.Cos", "MiaoYu.Core.Cos\MiaoYu.Core.Cos.csproj", "{3FBBDE5E-2D2C-428B-A2BF-298499ABA5A7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -126,6 +128,10 @@ Global
{39C765DB-41E7-4BC6-B75E-2A90CFF3A8EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{39C765DB-41E7-4BC6-B75E-2A90CFF3A8EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{39C765DB-41E7-4BC6-B75E-2A90CFF3A8EF}.Release|Any CPU.Build.0 = Release|Any CPU
{3FBBDE5E-2D2C-428B-A2BF-298499ABA5A7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3FBBDE5E-2D2C-428B-A2BF-298499ABA5A7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3FBBDE5E-2D2C-428B-A2BF-298499ABA5A7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3FBBDE5E-2D2C-428B-A2BF-298499ABA5A7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -148,6 +154,7 @@ Global
{75959B17-A901-49E1-A5E7-667349DC4203} = {DC7A7E4A-B4DC-4958-BAA5-2BBB1A153B5F}
{925EF035-4A41-42E4-A3A4-B8E9AE52F6B7} = {DC7A7E4A-B4DC-4958-BAA5-2BBB1A153B5F}
{39C765DB-41E7-4BC6-B75E-2A90CFF3A8EF} = {451BE0BB-26ED-47ED-ABC7-23001D21410C}
{3FBBDE5E-2D2C-428B-A2BF-298499ABA5A7} = {DB46F54A-9F53-44EC-80F8-9E53F0B871CF}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E3C61955-46C1-4D06-994F-C86A72B2B0E2}

View File

@ -1,3 +1,5 @@
using MiaoYu.Core.Cos;
namespace MiaoYu.Api.Admin;
/// <summary>
@ -11,7 +13,8 @@ namespace MiaoYu.Api.Admin;
AdminRepositoryStartup,
ChatAdminRepositoryStartup,
CoreSwaggerJwtStartup,
SharedAdminStartup>]
SharedAdminStartup,
CoreCosStartup>]
public class ApiAdminStartup : AppStartupBase<ApiAdminStartup>
{
/// <summary>

View File

@ -1,6 +1,9 @@
using IdGen;
using MiaoYu.Core.Cos.Services;
using MiaoYu.Repository.ChatAI.Admin.Entities;
using NPOI.SS.Formula.Functions;
namespace MiaoYu.Api.Admin.ApplicationServices.Apps;
/// <summary>
@ -8,10 +11,12 @@ namespace MiaoYu.Api.Admin.ApplicationServices.Apps;
/// </summary>
public class TImageConfigService : ApplicationService<IRepository<T_Image_Config>>
{
public TImageConfigService(IRepository<T_Image_Config> defaultRepository)
private ICodeCosService codeCosService;
public TImageConfigService(IRepository<T_Image_Config> defaultRepository,
ICodeCosService codeCosService)
: base(defaultRepository)
{
this.codeCosService = codeCosService;
}
/// <summary>
@ -121,6 +126,14 @@ public class TImageConfigService : ApplicationService<IRepository<T_Image_Config
return ExcelUtil.ExportExcelByPagingView(tableViewModel, null, "Id");
}
/// <summary>
/// 获取签名
/// </summary>
/// <returns></returns>
public string GetCosSign() {
var (sign,ex) = codeCosService.GenerateSignURL(new MiaoYu.Core.Cos.Models.CosGenerateSign());
return sign;
}
}

View File

@ -94,7 +94,17 @@ public class TImageConfigController : AdminControllerBase<TImageConfigService>
base.HttpContext.DownLoadFile(data, Tools.GetFileContentType[".xls"].ToStr(), name);
}
/// <summary>
///
/// </summary>
/// <returns></returns>
[HttpGet]
[ActionDescriptor(DisplayName = "获取cos加密")]
public string GetCosSign()
{
return this._defaultService.GetCosSign();
}
}

View File

@ -11,6 +11,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\MiaoYu.Core.Cos\MiaoYu.Core.Cos.csproj" />
<ProjectReference Include="..\MiaoYu.Core.Identity\MiaoYu.Core.Identity.csproj" />
<ProjectReference Include="..\MiaoYu.Core.Quartz\MiaoYu.Core.Quartz.csproj" />
<ProjectReference Include="..\MiaoYu.Core.Razor\MiaoYu.Core.Razor.csproj" />

View File

@ -209,6 +209,12 @@
<param name="pagingSearchInput"></param>
<returns></returns>
</member>
<member name="M:MiaoYu.Api.Admin.ApplicationServices.Apps.TImageConfigService.GetCosSign">
<summary>
获取签名
</summary>
<returns></returns>
</member>
<member name="T:MiaoYu.Api.Admin.ApplicationServices.Apps.T_CharacterService">
<summary>
人物表 服务 T_CharacterService
@ -1519,6 +1525,12 @@
<param name="pagingSearchInput"></param>
<returns></returns>
</member>
<member name="M:MiaoYu.Api.Admin.Controllers.Apps.TImageConfigController.GetCosSign">
<summary>
</summary>
<returns></returns>
</member>
<member name="T:MiaoYu.Api.Admin.Controllers.Apps.T_CharacterController">
<summary>
人物表 控制器

View File

@ -7,25 +7,25 @@
// pi
"AdminRepositoryOptions": {
// SqlServer MySql PostgreSqlOracle
"DefaultDatabaseType": "MySql",
"DefaultDatabaseType": "SqlServer",
//EFCore
"IsMonitorEFCore": false,
"IsMonitorEFCore": true,
// - mysql
"ConnectionString": "Server=localhost; port=3306; Database=hzy_admin_mysql_20230227; uid=root; pwd=123456; Convert Zero Datetime=False"
//"ConnectionString": "Server=localhost; port=3306; Database=hzy_admin_mysql_20230227; uid=root; pwd=123456; Convert Zero Datetime=False"
//// - sqlserver
//"ConnectionString": "Server=.;Database=hzy_microservices_sqlserver_20230227;User ID=sa;Password=123456;MultipleActiveResultSets=true;Encrypt=True;TrustServerCertificate=True;",
//"ConnectionString": "Server=192.168.195.2;Database=MiaoYu;User ID=zpc;Password=zpc;MultipleActiveResultSets=true;Encrypt=True;TrustServerCertificate=True;",
"ConnectionString": "Server=192.168.195.2;Database=HZY;User ID=zpc;Password=zpc;MultipleActiveResultSets=true;Encrypt=True;TrustServerCertificate=True;"
//// - postgresql
//"ConnectionString": "User ID=postgres;Password=123456;Host=localhost;Port=5432;Database=hzy_microservices_pgsql_20230227;Pooling=true;TimeZone=Asia/Shanghai",
//// - oracle
//"ConnectionString": "user id=hzy_admin_oracle_20221213;password=123456; data source=//127.0.0.1:1521/orcl;Pooling=true;Min Pool Size=1"
},
"ChatAdminRepositoryOptions": {
// SqlServer MySql PostgreSqlOracle
"DefaultDatabaseType": "SqlServer",
//EFCore
"IsMonitorEFCore": true,
"ConnectionString": "Server=192.168.195.2;Database=MiaoYu;User ID=zpc;Password=zpc;MultipleActiveResultSets=true;Encrypt=True;TrustServerCertificate=True;"
}
}

View File

@ -41,5 +41,31 @@
"MaxFileSizeLimit": "1gb",
// |
"AllowExtensions": ".png|.jpg"
},
//
"TencentCloud": {
"SecretId": "AKIDLbhdP0Vs57yd7QZWu8A2jFbno8JKBUp6",
"SecretKey": "MlP5tcUG6mdj7TwOpDWnZNFGIrJY8eH4",
"AppId": "1308826010",
"SMSCode": {
//
"ReqMethod": "POST",
//
"Timeout": 30,
//ID:
"SmsSdkAppId": "1400923253",
//
"SignName": "上海寰梦科技发展",
//
"TemplateId": "2209122"
},
"CosConfig": {
"AppId": "1308826010",
"SecretId": "AKIDLbhdP0Vs57yd7QZWu8A2jFbno8JKBUp6",
"SecretKey": "MlP5tcUG6mdj7TwOpDWnZNFGIrJY8eH4",
"DurationSecond": "300",
"Region": "ap-shanghai",
"Bucket": "miaoyu"
}
}
}

View File

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MiaoYu.Core.Cos.Configs
{
public class TencentBaseConfig
{
/// <summary>
/// 腾讯云id
/// </summary>
public string SecretId { get; set; }
/// <summary>
/// 密钥
/// </summary>
public string SecretKey { get; set; }
/// <summary>
/// 应用Id
/// </summary>
public string AppId { get; set; }
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MiaoYu.Core.Cos.Configs
{
/// <summary>
///
/// </summary>
public class TencentConfig:TencentBaseConfig
{
/// <summary>
/// cos
/// </summary>
public TencentCosConfig CosConfig { get; set; }
}
}

View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MiaoYu.Core.Cos.Configs
{
/// <summary>
/// 对象存储
/// </summary>
public class TencentCosConfig:TencentBaseConfig
{
/// <summary>
/// 最大超时时间
/// </summary>
public long DurationSecond { get; set; }
/// <summary>
/// 区域
/// </summary>
public string Region { get; set; }
/// <summary>
///
/// </summary>
public string Bucket { get; set; }
}
}

View File

@ -0,0 +1,59 @@
using HZY.Framework.Core.AspNetCore;
using MiaoYu.Core.Cos.Configs;
using MiaoYu.Core.Cos.Services;
using MiaoYu.Core.Cos.Services.Impl;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MiaoYu.Core.Cos
{
/// <summary>
/// cos
/// </summary>
public class CoreCosStartup : StartupModule<CoreCosStartup>
{
/// <summary>
/// 服务配置
/// </summary>
/// <param name="webApplicationBuilder"></param>
public override void ConfigureServices(WebApplicationBuilder webApplicationBuilder)
{
var tencentConfig = webApplicationBuilder.Configuration.GetSection("TencentCloud").Get<TencentConfig>();
if (tencentConfig == null)
{
tencentConfig = new TencentConfig();
}
if (tencentConfig.CosConfig == null)
{
tencentConfig.CosConfig = new TencentCosConfig() { };
}
if (string.IsNullOrEmpty(tencentConfig.CosConfig.SecretId))
{
tencentConfig.CosConfig.SecretId = tencentConfig.SecretId;
}
if (string.IsNullOrEmpty(tencentConfig.CosConfig.SecretKey))
{
tencentConfig.CosConfig.SecretKey = tencentConfig.SecretKey;
}
//注册一个验证码的服务
webApplicationBuilder.Services.AddSingleton<TencentConfig>(tencentConfig);
//webApplicationBuilder.Services.AddScoped<ICodeCosService, TencentCodeCosService>();
}
/// <summary>
/// 使用服务
/// </summary>
/// <param name="webApplication"></param>
public override void Configure(WebApplication webApplication)
{
}
}
}

View File

@ -0,0 +1,3 @@
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<Rougamo />
</Weavers>

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- This file was generated by Fody. Manual changes to this file will be lost when your project is rebuilt. -->
<xs:element name="Weavers">
<xs:complexType>
<xs:all>
<xs:element name="Rougamo" minOccurs="0" maxOccurs="1" type="xs:anyType" />
</xs:all>
<xs:attribute name="VerifyAssembly" type="xs:boolean">
<xs:annotation>
<xs:documentation>'true' to run assembly verification (PEVerify) on the target assembly after all weavers have been executed.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="VerifyIgnoreCodes" type="xs:string">
<xs:annotation>
<xs:documentation>A comma-separated list of error codes that can be safely ignored in assembly verification.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="GenerateXsd" type="xs:boolean">
<xs:annotation>
<xs:documentation>'false' to turn off automatic generation of the XML Schema file.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>

View File

@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Tencent.QCloud.Cos.Sdk" Version="5.4.38" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MiaoYu.Core.EntityFramework\MiaoYu.Core.EntityFramework.csproj" />
<ProjectReference Include="..\MiaoYu.Core\MiaoYu.Core.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,18 @@
using MiaoYu.Core.Cos.Configs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MiaoYu.Core.Cos.Models
{
/// <summary>
/// 生成签名
/// </summary>
public class CosGenerateSign: TencentCosConfig
{
}
}

View File

@ -0,0 +1,16 @@
namespace MiaoYu.Core.Cos.Services;
/// <summary>
///
/// </summary>
public interface ICodeCosService:ISingletonDependency //:ITransientDependency: IScopedDependency
{
/// <summary>
/// 返回签名地址
/// </summary>
/// <param name="cosGenerateSign"></param>
/// <returns>sign签名值,过期时间</returns>
(string sign, int expiredSeconds) GenerateSignURL(CosGenerateSign cosGenerateSign);
}

View File

@ -0,0 +1,81 @@
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();
}
}
}

View File

@ -0,0 +1,7 @@
global using HZY.Framework.Core.AspNetCore;
global using HZY.Framework.DependencyInjection.Attributes;
global using Microsoft.AspNetCore.Builder;
global using Microsoft.Extensions.DependencyInjection;
global using HZY.Framework.DependencyInjection;
global using MiaoYu.Core.Cos.Models;