diff --git a/IRaCIS.Core.API/Controllers/ExtraController.cs b/IRaCIS.Core.API/Controllers/ExtraController.cs index 1234a2e45..99a32155d 100644 --- a/IRaCIS.Core.API/Controllers/ExtraController.cs +++ b/IRaCIS.Core.API/Controllers/ExtraController.cs @@ -26,6 +26,8 @@ using Aliyun.Acs.Core.Profile; using Aliyun.Acs.Sts.Model.V20150401; using Microsoft.AspNetCore.Hosting; using MassTransit; +using IRaCIS.Core.Application.Helper; +using Microsoft.Extensions.Options; namespace IRaCIS.Api.Controllers { @@ -134,25 +136,77 @@ namespace IRaCIS.Api.Controllers return returnModel; } - [Authorize] - [HttpGet("user/GenerateSTS")] - public IResponseOutput GenerateSTS( [FromServices] IWebHostEnvironment webHostEnvironment) + [HttpGet("user/GetObjectStoreToken")] + public IResponseOutput GetObjectStoreToken([FromServices] IOptionsMonitor options) { - + var serviceOption = options.CurrentValue; - var configuration = new ConfigurationBuilder() - .AddJsonFile($"appsettings.{webHostEnvironment.EnvironmentName}.json") - .Build(); + if (Enum.TryParse(serviceOption.ObjectStoreUse, out var parsedEnum) && parsedEnum == ObjectStoreUse.AliyunOSS) + { + var ossOptions = serviceOption.AliyunOSS; + + IClientProfile profile = DefaultProfile.GetProfile(ossOptions.RegionId, ossOptions.AccessKeyId, ossOptions.AccessKeySecret); + DefaultAcsClient client = new DefaultAcsClient(profile); - IClientProfile profile = DefaultProfile.GetProfile("cn-shanghai", configuration["AliyunOSS:accessKeyId"], configuration["AliyunOSS:accessKeySecret"]); + // 创建一个STS请求 + AssumeRoleRequest request = new AssumeRoleRequest + { + RoleArn = ossOptions.RoleArn, // 角色ARN,需要替换为你的角色ARN + RoleSessionName = $"session-name-{NewId.NextGuid()}", // 角色会话名称,可自定义 + DurationSeconds = 900, // 令牌有效期(单位:秒),这里设置为1小时 + }; + + + AssumeRoleResponse response = client.GetAcsResponse(request); + + // 返回STS令牌信息给前端 + var stsToken = new ObjectStoreDTO() + { + ObjectStoreUse = serviceOption.ObjectStoreUse, + AliyunOSS = new AliyunOSSTempToken() + { + AccessKeyId = response.Credentials.AccessKeyId, + AccessKeySecret = response.Credentials.AccessKeySecret, + SecurityToken = response.Credentials.SecurityToken, + Expiration = response.Credentials.Expiration, + + Region = ossOptions.Region, + BucketName = ossOptions.BucketName, + ViewEndpoint = ossOptions.ViewEndpoint, + + }, + MinIO = serviceOption.MinIO + } + ; + + return ResponseOutput.Ok(stsToken); + } + else if (Enum.TryParse(serviceOption.ObjectStoreUse, out var parsedValue) && parsedValue == ObjectStoreUse.MinIO) + { + return ResponseOutput.Ok(new ObjectStoreDTO() { ObjectStoreUse = serviceOption.ObjectStoreUse, MinIO = serviceOption.MinIO }); + } + else + { + return ResponseOutput.Ok(new ObjectStoreDTO() { ObjectStoreUse = serviceOption.ObjectStoreUse, MinIO = serviceOption.MinIO }); + } + + } + + [HttpGet("user/GenerateSTS")] + public IResponseOutput GenerateSTS([FromServices] IOptionsMonitor options) + { + var ossOptions = options.CurrentValue; + + + IClientProfile profile = DefaultProfile.GetProfile(ossOptions.RegionId, ossOptions.AccessKeyId, ossOptions.AccessKeySecret); DefaultAcsClient client = new DefaultAcsClient(profile); // 创建一个STS请求 AssumeRoleRequest request = new AssumeRoleRequest { - RoleArn = "acs:ram::1899121822495495:role/oss-upload", // 角色ARN,需要替换为你的角色ARN + RoleArn = ossOptions.RoleArn, // 角色ARN,需要替换为你的角色ARN RoleSessionName = $"session-name-{NewId.NextGuid()}", // 角色会话名称,可自定义 DurationSeconds = 900, // 令牌有效期(单位:秒),这里设置为1小时 }; @@ -168,9 +222,9 @@ namespace IRaCIS.Api.Controllers SecurityToken = response.Credentials.SecurityToken, Expiration = response.Credentials.Expiration, - Region= configuration["AliyunOSS:region"], - BucketName = configuration["AliyunOSS:bucketName"], - ViewEndpoint = configuration["AliyunOSS:viewEndpoint"], + Region = ossOptions.Region, + BucketName = ossOptions.BucketName, + ViewEndpoint = ossOptions.ViewEndpoint, }; diff --git a/IRaCIS.Core.API/Startup.cs b/IRaCIS.Core.API/Startup.cs index 498b34c37..4727ed475 100644 --- a/IRaCIS.Core.API/Startup.cs +++ b/IRaCIS.Core.API/Startup.cs @@ -97,7 +97,8 @@ namespace IRaCIS.Core.API services.AddOptions().Configure(_configuration.GetSection("SystemEmailSendConfig")); services.AddOptions().Configure(_configuration.GetSection("BasicSystemConfig")); - + services.AddOptions().Configure(_configuration.GetSection("AliyunOSS")); + services.AddOptions().Configure(_configuration.GetSection("ObjectStoreService")); //̬WebApi + UnifiedApiResultFilter ʡ