oss 权限修改

Uat_Study
hang 2023-09-05 10:45:01 +08:00
parent a47f2fd496
commit 1ac336220e
3 changed files with 56 additions and 11 deletions

View File

@ -21,6 +21,10 @@ using IRaCIS.Core.Domain.Models;
using IRaCIS.Core.Infrastructure;
using System.Linq;
using Microsoft.Extensions.Logging;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Sts.Model.V20150401;
using Microsoft.AspNetCore.Hosting;
namespace IRaCIS.Api.Controllers
{
@ -136,6 +140,46 @@ namespace IRaCIS.Api.Controllers
}
[HttpGet("user/GenerateSTS")]
public IResponseOutput GenerateSTS( [FromServices] IWebHostEnvironment webHostEnvironment)
{
var configuration = new ConfigurationBuilder()
.AddJsonFile($"appsettings.{webHostEnvironment.EnvironmentName}.json")
.Build();
//configuration["AliyunOSS:region"]
IClientProfile profile = DefaultProfile.GetProfile("cn-shanghai", configuration["AliyunOSS:accessKeyId"], configuration["AliyunOSS:accessKeySecret"]);
DefaultAcsClient client = new DefaultAcsClient(profile);
// 创建一个STS请求
AssumeRoleRequest request = new AssumeRoleRequest
{
RoleArn = "acs:ram::1899121822495495:role/oss-upload", // 角色ARN需要替换为你的角色ARN
RoleSessionName = "session-name", // 角色会话名称,可自定义
DurationSeconds = 3600, // 令牌有效期单位这里设置为1小时
};
AssumeRoleResponse response = client.GetAcsResponse(request);
// 返回STS令牌信息给前端
var stsToken = new
{
AccessKeyId = response.Credentials.AccessKeyId,
AccessKeySecret = response.Credentials.AccessKeySecret,
SecurityToken = response.Credentials.SecurityToken,
Expiration = response.Credentials.Expiration
};
return ResponseOutput.Ok(stsToken);
}
[HttpGet, Route("imageShare/ShareImage")]
[AllowAnonymous]

View File

@ -66,6 +66,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="aliyun-net-sdk-sts" Version="3.1.1" />
<PackageReference Include="AspNetCoreRateLimit" Version="4.0.2" />
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />

View File

@ -9,10 +9,10 @@
"AliyunOSS": {
"endpoint": "https://oss-cn-shanghai.aliyuncs.com",
"region": "oss-cn-shanghai",
"accessKeyId": "mpXG7Nu6zTpsDrI1",
"accessKeySecret": "yNINcEb099SkNfF6vYKaoP8TZNI3xZ",
"accessKeyId": "LTAI5tKvzs7ed3UfSpNk3xwQ",
"accessKeySecret": "zTIceGEShlZDGnLrCFfIGFE7TXVRio",
"bucketName": "zyypacs",
"viewEndpoint": "https://zy-irc-cache.oss-cn-shanghai.aliyuncs.com"
"viewEndpoint": "https://zyypacs.oss-cn-shanghai.aliyuncs.com"
},
"ConnectionStrings": {
"RemoteNew": "Server=123.56.94.154,1433\\MSSQLSERVER;Database=Test.Study;User ID=sa;Password=dev123456DEV;TrustServerCertificate=true",