using Aliyun.Acs.Core;
|
using Aliyun.Acs.Core.Auth.Sts;
|
using Aliyun.Acs.Core.Http;
|
using Aliyun.Acs.Core.Profile;
|
using LifePayment.Domain.Shared;
|
|
namespace LifePayment.Application
|
{
|
public class OssSTSHelper
|
{
|
private const int TokenExpireTime = 3600;
|
private const string RoleSessionName = "SessionTest";
|
private const string PolicyFile = @"{
|
""Version"": ""1"",
|
""Statement"": [
|
{
|
""Effect"": ""Allow"",
|
""Action"": ""oss:PutObject"",
|
""Resource"": [
|
""acs:oss:*:*:waterdroptest2/*"",
|
""acs:oss:*:*:parkmanagement/*"",
|
""acs:oss:*:*:jurenlian/*""
|
]
|
}
|
]
|
}";
|
|
private readonly OssSettings ossSettings;
|
|
public OssSTSHelper(OssSettings ossSettings)
|
{
|
this.ossSettings = ossSettings;
|
}
|
|
public OssSTSReponse GetOssSTS()
|
{
|
string regionId = "cn-hangzhou";
|
|
IClientProfile profile = DefaultProfile.GetProfile(regionId, ossSettings.OssAccessKeyId, ossSettings.OssAccessSecret);
|
DefaultAcsClient client = new DefaultAcsClient(profile);
|
AssumeRoleRequest request = new AssumeRoleRequest();
|
|
request.Method = MethodType.POST;
|
|
request.RoleArn = ossSettings.OssRoleRan;
|
request.RoleSessionName = RoleSessionName;
|
request.Policy = PolicyFile;
|
request.DurationSeconds = TokenExpireTime;
|
AssumeRoleResponse response = client.GetAcsResponse(request);
|
OssSTSReponse result = new OssSTSReponse()
|
{
|
Expiration = response.Credentials.Expiration,
|
OssAccessKeyId = response.Credentials.AccessKeyId,
|
OssAccessSecret = response.Credentials.AccessKeySecret,
|
SecurityToken = response.Credentials.SecurityToken,
|
RequestId = response.RequestId
|
};
|
return result;
|
}
|
|
}
|
}
|