@ -3,23 +3,22 @@ using Aliyun.Acs.Core.Auth.Sts;
using Aliyun.Acs.Core.Http ;
using Aliyun.Acs.Core.Profile ;
using Aliyun.OSS ;
using Microsoft.Extensions.Caching.Memory ;
using Microsoft.Extensions.Caching.Distributed ;
using System ;
using System.Collections.Generic ;
using Volo.Abp.Caching ;
using Volo.Abp.DependencyInjection ;
namespace Volo.Abp.BlobStoring.Aliyun
{
/// <summary>
/// Sub-account access to OSS or STS temporary authorization to access OSS
/// 子账号/STS临时授权访问OSS
/// STS:https://help.aliyun.com/document_detail/28756.html
/// </summary>
public class DefaultOssClientFactory : IOssClientFactory , ITransientDependency
{
private readonly IMemoryCache _ cache ;
private readonly IDistributedCache < AssumeRoleCredentialsCacheItem > _ cache ;
public DefaultOssClientFactory (
IMemoryCache cache )
IDistributedCache < AssumeRoleCredentialsCacheItem > cache )
{
_ cache = cache ;
}
@ -29,45 +28,40 @@ namespace Volo.Abp.BlobStoring.Aliyun
//Sub-account
if ( aliyunConfig . DurationSeconds < = 0 )
{
var key = aliyunConfig . ToOssKeyString ( ) ;
var iOssClient = _ cache . Get < IOss > ( key ) ;
if ( iOssClient ! = null )
{
return iOssClient ;
}
iOssClient = new OssClient ( aliyunConfig . Endpoint , aliyunConfig . AccessKeyId , aliyunConfig . AccessKeySecret ) ;
_ cache . Set ( key , iOssClient ) ;
return iOssClient ;
return new OssClient ( aliyunConfig . Endpoint , aliyunConfig . AccessKeyId , aliyunConfig . AccessKeySecret ) ;
}
else
{
//STS temporary authorization to access OSS
var key = aliyunConfig . ToOssWithSts KeyString ( ) ;
var iOssClient = _ cache . Get < IOss > ( key ) ;
if ( iOssClient ! = null )
var key = aliyunConfig . ToKeyString ( ) ;
var cacheItem = _ cache . Get ( key ) ;
if ( cacheItem = = null )
{
return iOssClient ;
}
IClientProfile profile = DefaultProfile . GetProfile (
IClientProfile profile = DefaultProfile . GetProfile (
aliyunConfig . RegionId ,
aliyunConfig . AccessKeyId ,
aliyunConfig . AccessKeySecret ) ;
DefaultAcsClient client = new DefaultAcsClient ( profile ) ;
AssumeRoleRequest request = new AssumeRoleRequest
{
AcceptFormat = FormatType . JSON ,
//eg:acs:ram::$accountID:role/$roleName
RoleArn = aliyunConfig . RoleArn ,
RoleSessionName = aliyunConfig . RoleSessionName ,
//Set the validity period of the temporary access credential, the unit is s, the minimum is 900, and the maximum is 3600. default 3600
DurationSeconds = aliyunConfig . DurationSeconds ,
//Set additional permission policy of Token; when acquiring Token, further reduce the permission of Token by setting an additional permission policy
Policy = aliyunConfig . Policy . IsNullOrEmpty ( ) ? null : aliyunConfig . Policy ,
} ;
var response = client . GetAcsResponse ( request ) ;
iOssClient = new OssClient ( aliyunConfig . Endpoint , response . Credentials . AccessKeyId , response . Credentials . AccessKeySecret , response . Credentials . SecurityToken ) ;
_ cache . Set ( key , iOssClient , TimeSpan . FromSeconds ( aliyunConfig . DurationSeconds ) ) ;
return iOssClient ;
DefaultAcsClient client = new DefaultAcsClient ( profile ) ;
AssumeRoleRequest request = new AssumeRoleRequest
{
AcceptFormat = FormatType . JSON ,
//eg:acs:ram::$accountID:role/$roleName
RoleArn = aliyunConfig . RoleArn ,
RoleSessionName = aliyunConfig . RoleSessionName ,
//Set the validity period of the temporary access credential, the unit is s, the minimum is 900, and the maximum is 3600. default 3600
DurationSeconds = aliyunConfig . DurationSeconds ,
//Set additional permission policy of Token; when acquiring Token, further reduce the permission of Token by setting an additional permission policy
Policy = aliyunConfig . Policy . IsNullOrEmpty ( ) ? null : aliyunConfig . Policy ,
} ;
var response = client . GetAcsResponse ( request ) ;
cacheItem = new AssumeRoleCredentialsCacheItem ( response . Credentials . AccessKeyId , response . Credentials . AccessKeySecret , response . Credentials . SecurityToken ) ;
_ cache . Set ( key , cacheItem , new DistributedCacheEntryOptions ( )
{
//Subtract 10 seconds of network request time.
AbsoluteExpirationRelativeToNow = TimeSpan . FromSeconds ( aliyunConfig . DurationSeconds - 1 0 )
} ) ;
}
return new OssClient ( aliyunConfig . Endpoint , cacheItem . AccessKeyId , cacheItem . AccessKeySecret , cacheItem . SecurityToken ) ;
}
}
}