迁移提交

Uat_Study
hang 2023-11-24 11:25:15 +08:00
parent 6e22515603
commit dc12e57494
2 changed files with 68 additions and 13 deletions

View File

@ -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<ObjectStoreServiceOptions> options)
{
var serviceOption = options.CurrentValue;
if (Enum.TryParse<ObjectStoreUse>(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);
var configuration = new ConfigurationBuilder()
.AddJsonFile($"appsettings.{webHostEnvironment.EnvironmentName}.json")
.Build();
// 创建一个STS请求
AssumeRoleRequest request = new AssumeRoleRequest
{
RoleArn = ossOptions.RoleArn, // 角色ARN需要替换为你的角色ARN
RoleSessionName = $"session-name-{NewId.NextGuid()}", // 角色会话名称,可自定义
DurationSeconds = 900, // 令牌有效期单位这里设置为1小时
};
IClientProfile profile = DefaultProfile.GetProfile("cn-shanghai", configuration["AliyunOSS:accessKeyId"], configuration["AliyunOSS:accessKeySecret"]);
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<ObjectStoreUse>(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<AliyunOSSOptions> 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,
};

View File

@ -97,7 +97,8 @@ namespace IRaCIS.Core.API
services.AddOptions().Configure<SystemEmailSendConfig>(_configuration.GetSection("SystemEmailSendConfig"));
services.AddOptions().Configure<ServiceVerifyConfigOption>(_configuration.GetSection("BasicSystemConfig"));
services.AddOptions().Configure<AliyunOSSOptions>(_configuration.GetSection("AliyunOSS"));
services.AddOptions().Configure<ObjectStoreServiceOptions>(_configuration.GetSection("ObjectStoreService"));
//动态WebApi + UnifiedApiResultFilter 省掉控制器代码