From addd9f4c79a923291d29cbc30b30003d8d5b62d1 Mon Sep 17 00:00:00 2001 From: “wangming” <“wangming@antissoft.com”> Date: Thu, 11 Dec 2025 21:51:06 +0800 Subject: [PATCH] feat: 配置annexpic接口使用阿里云OSS存储 --- netcore/src/Application/NCC.API.Core/Startup.cs | 32 ++++++++++++++++++++++++++++++++ netcore/src/Application/NCC.API/appsettings.json | 11 +++++++++-- netcore/src/Modularity/System/NCC.System/Service/Common/FileService.cs | 44 +++++++++++++++++++++++++++++++++----------- 3 files changed, 74 insertions(+), 13 deletions(-) diff --git a/netcore/src/Application/NCC.API.Core/Startup.cs b/netcore/src/Application/NCC.API.Core/Startup.cs index d831d65..d497022 100644 --- a/netcore/src/Application/NCC.API.Core/Startup.cs +++ b/netcore/src/Application/NCC.API.Core/Startup.cs @@ -106,6 +106,38 @@ namespace NCC.API.Core #endregion + #region 阿里云OSS + + var aliyunOSSEndpoint = App.Configuration["NCC_App:AliyunOSS:Endpoint"]; + var aliyunOSSAccessKey = App.Configuration["NCC_App:AliyunOSS:AccessKeyId"]; + var aliyunOSSSecretKey = App.Configuration["NCC_App:AliyunOSS:AccessKeySecret"]; + var aliyunOSSRegion = App.Configuration["NCC_App:AliyunOSS:Region"]; + var bucketName = App.Configuration["NCC_App:BucketName"]; + + if (!string.IsNullOrEmpty(aliyunOSSEndpoint) && !string.IsNullOrEmpty(aliyunOSSAccessKey) && !string.IsNullOrEmpty(aliyunOSSSecretKey)) + { + services.AddOSSService("aliyun", option => + { + option.Provider = OSSProvider.Aliyun; + // 阿里云OSS Endpoint格式:oss-{region}.aliyuncs.com + // 注意:不要包含bucket名称,只需要区域Endpoint + option.Endpoint = aliyunOSSEndpoint; + option.AccessKey = aliyunOSSAccessKey; + option.SecretKey = aliyunOSSSecretKey; + option.IsEnableHttps = true; + option.IsEnableCache = true; + // 对于OnceMi.AspNetCore.OSS,Region是可选的 + // 但如果出现Endpoint不匹配错误,可能需要明确设置Region + // 成都区域的Region格式:cn-chengdu + if (!string.IsNullOrEmpty(aliyunOSSRegion)) + { + option.Region = aliyunOSSRegion; + } + }); + } + + #endregion + #region 微信 services.AddSenparcGlobalServices(App.Configuration)//Senparc.CO2NET 全局注册 .AddSenparcWeixinServices(App.Configuration);//Senparc.Weixin 注册(如果使用Senparc.Weixin SDK则添加) diff --git a/netcore/src/Application/NCC.API/appsettings.json b/netcore/src/Application/NCC.API/appsettings.json index 693026a..0275c26 100644 --- a/netcore/src/Application/NCC.API/appsettings.json +++ b/netcore/src/Application/NCC.API/appsettings.json @@ -33,7 +33,7 @@ "DBType": "MySql", //MySql;SqlServer;Oracle;PostgreSQL;Dm;Kdbndp; //SqlServer // "DefaultConnection": "Data Source=localhost;Initial Catalog={0};User ID=sqladmin;Password=P@ssw0rd;MultipleActiveResultSets=true" - "DefaultConnection": "Database={0};Data Source=rm-bp19ohrgc6111ynzh1o.mysql.rds.aliyuncs.com;Port=3306;User Id=netteam;Password=netteam;Charset=utf8;TreatTinyAsBoolean=true;" + "DefaultConnection": "Database={0};Data Source=rm-2vccze142rc9a8f58bo.mysql.cn-chengdu.rds.aliyuncs.com;Port=3306;User Id=lvqiansql;Password=LvQ1@n!20251211;Charset=utf8;TreatTinyAsBoolean=true;" }, "SpecificationDocumentSettings": { @@ -204,9 +204,16 @@ "domainKey": "yozoHbiPMzu50374" }, //Minio - "BucketName": "NCCsoftoss", + "BucketName": "lvqian-erip", //文件存储类型(本地:local,MinIo:minio,阿里云:aliyun-oss,腾讯云:tencent-cos) "FileStoreType": "local", + //阿里云OSS配置 + "AliyunOSS": { + "AccessKeyId": "LTAI5t6h4i95uapwzDwKfNxi", + "AccessKeySecret": "84dpUAlu2eoyFOIEhFGkZlIy45h0B6", + "Endpoint": "oss-cn-chengdu.aliyuncs.com", + "Region": "cn-chengdu" + }, //================== 系统错误邮件报告反馈相关 ============================== --> //软件的错误报告 "ErrorReport": "false", diff --git a/netcore/src/Modularity/System/NCC.System/Service/Common/FileService.cs b/netcore/src/Modularity/System/NCC.System/Service/Common/FileService.cs index 550c219..acdd8fd 100644 --- a/netcore/src/Modularity/System/NCC.System/Service/Common/FileService.cs +++ b/netcore/src/Modularity/System/NCC.System/Service/Common/FileService.cs @@ -64,7 +64,11 @@ namespace NCC.System.Service.Common throw NCCException.Oh(ErrorCode.D1800); var _filePath = GetPathByType(type); var _fileName = DateTime.Now.ToString("yyyyMMdd") + "_" + YitIdHelper.NextId().ToString() + Path.GetExtension(file.FileName); - await UploadFileByType(file, _filePath, _fileName); + + // annexpic 类型强制使用阿里云OSS存储 + string forceStoreType = type == "annexpic" ? "aliyun-oss" : null; + await UploadFileByType(file, _filePath, _fileName, forceStoreType); + return new { name = _fileName, url = string.Format("/api/File/Image/{0}/{1}", type, _fileName) }; } @@ -79,7 +83,9 @@ namespace NCC.System.Service.Common public async Task GetImg(string type, string fileName) { var filePath = Path.Combine(GetPathByType(type), fileName.Replace("@", ".")); - return await DownloadFileByType(filePath, fileName); + // annexpic 类型强制使用阿里云OSS存储 + string forceStoreType = type == "annexpic" ? "aliyun-oss" : null; + return await DownloadFileByType(filePath, fileName, forceStoreType); //return new FileStreamResult(new FileStream(filePath, FileMode.Open), "application/octet-stream") { FileDownloadName = fileName }; } @@ -181,7 +187,9 @@ namespace NCC.System.Service.Common fileDownloadName = Path.GetFileName(filePath); if (fileDownloadName.IsNullOrWhiteSpace()) fileDownloadName = fileName.Replace(GetPathByType(type), ""); - return await DownloadFileByType(filePath, fileDownloadName); + // annexpic 类型强制使用阿里云OSS存储 + string forceStoreType = type == "annexpic" ? "aliyun-oss" : null; + return await DownloadFileByType(filePath, fileDownloadName, forceStoreType); } else { @@ -198,15 +206,19 @@ namespace NCC.System.Service.Common /// /// /// + /// 强制使用指定的存储类型(如:aliyun-oss),如果为空则使用配置的存储类型 /// [NonAction] - public async Task UploadFileByType(IFormFile file, string filePath, string fileName) + public async Task UploadFileByType(IFormFile file, string filePath, string fileName, string forceStoreType = null) { try { var bucketName = KeyVariable.BucketName; - var fileStoreType = KeyVariable.FileStoreType; - var uploadPath = Path.Combine(filePath, fileName); + var fileStoreType = !string.IsNullOrEmpty(forceStoreType) ? forceStoreType : KeyVariable.FileStoreType; + // OSS路径使用正斜杠,不使用Path.Combine + var uploadPath = fileStoreType == "aliyun-oss" || fileStoreType == "tencent-cos" || fileStoreType == "minio" + ? $"{filePath.TrimEnd('/').TrimEnd('\\')}/{fileName}" + : Path.Combine(filePath, fileName); var stream = file.OpenReadStream(); switch (fileStoreType) { @@ -229,9 +241,16 @@ namespace NCC.System.Service.Common break; } } - catch (Exception) + catch (Exception ex) { - throw NCCException.Oh(ErrorCode.D8003); + // 记录详细错误信息以便调试 + var errorMsg = $"文件上传失败: {ex.Message}"; + if (ex.InnerException != null) + { + errorMsg += $", InnerException: {ex.InnerException.Message}"; + } + // 抛出包含详细信息的异常 + throw NCCException.Oh($"[D8003] {errorMsg}"); } } @@ -240,14 +259,15 @@ namespace NCC.System.Service.Common /// /// /// + /// 强制使用指定的存储类型(如:aliyun-oss),如果为空则使用配置的存储类型 /// [NonAction] - public async Task DownloadFileByType(string filePath, string fileDownLoadName) + public async Task DownloadFileByType(string filePath, string fileDownLoadName, string forceStoreType = null) { try { var bucketName = KeyVariable.BucketName; - var fileStoreType = KeyVariable.FileStoreType; + var fileStoreType = !string.IsNullOrEmpty(forceStoreType) ? forceStoreType : KeyVariable.FileStoreType; switch (fileStoreType) { case "minio": @@ -255,7 +275,7 @@ namespace NCC.System.Service.Common var stream1 = await url1.GetAsStreamAsync(); return new FileStreamResult(stream1, "application/octet-stream") { FileDownloadName = fileDownLoadName }; case "aliyun-oss": - var url2 = await _oSSServiceFactory.Create("Aliyun").PresignedGetObjectAsync(bucketName, filePath, 86400); + var url2 = await _oSSServiceFactory.Create("aliyun").PresignedGetObjectAsync(bucketName, filePath, 86400); var stream2 = await url2.GetAsStreamAsync(); return new FileStreamResult(stream2, "application/octet-stream") { FileDownloadName = fileDownLoadName }; case "tencent-cos": @@ -299,6 +319,8 @@ namespace NCC.System.Service.Common return FileVariable.SystemFilePath; case "annexpic": return FileVariable.SystemFilePath; + case "annexpic_bak": + return FileVariable.SystemFilePath; case "document": return FileVariable.DocumentFilePath; case "diskdocument": -- libgit2 0.21.4