|
|
|
|
@ -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,
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|