From 30f36aca8f2820d4dd7d81b951077829be94084c Mon Sep 17 00:00:00 2001 From: shaoxiaoxu Date: Wed, 15 Jul 2020 18:42:41 +0800 Subject: [PATCH] chore: resolve conversation https://github.com/abpframework/abp/pull/4735 --- .../FodyWeavers.xml | 2 +- .../Aliyun/AbpBlobStoringAliyunModule.cs | 3 - .../Aliyun/AliyunBlobNamingNormalizer.cs | 5 -- .../BlobStoring/Aliyun/AliyunBlobProvider.cs | 58 +++++++++++----- .../Aliyun/AliyunBlobProviderConfiguration.cs | 39 ++++++----- .../AliyunBlobProviderConfigurationNames.cs | 1 + .../Aliyun/AssumeRoleCredentialsCacheItem.cs | 30 +++++++++ .../Aliyun/DefaultOssClientFactory.cs | 66 +++++++++---------- .../Properties/serviceDependencies.json | 7 -- .../Properties/serviceDependencies.local.json | 7 -- .../Volo.Abp.BlobStoring.Aliyun.Tests.csproj | 4 ++ .../Aliyun/AbpBlobStoringAliyunTestModule.cs | 18 ++++- .../Aliyun/AliyunBlobContainer_Tests.cs | 35 +++------- ...liyunBlobNamingNormalizerProvider_Tests.cs | 8 --- .../Abp/BlobStoring/Aliyun/MyTestContainer.cs | 7 -- 15 files changed, 149 insertions(+), 141 deletions(-) create mode 100644 framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AssumeRoleCredentialsCacheItem.cs delete mode 100644 framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Properties/serviceDependencies.json delete mode 100644 framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Properties/serviceDependencies.local.json delete mode 100644 framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Volo/Abp/BlobStoring/Aliyun/MyTestContainer.cs diff --git a/framework/src/Volo.Abp.BlobStoring.Aliyun/FodyWeavers.xml b/framework/src/Volo.Abp.BlobStoring.Aliyun/FodyWeavers.xml index 00e1d9a1c1..be0de3a908 100644 --- a/framework/src/Volo.Abp.BlobStoring.Aliyun/FodyWeavers.xml +++ b/framework/src/Volo.Abp.BlobStoring.Aliyun/FodyWeavers.xml @@ -1,3 +1,3 @@  - + \ No newline at end of file diff --git a/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AbpBlobStoringAliyunModule.cs b/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AbpBlobStoringAliyunModule.cs index 6472413ebb..c14dd193fc 100644 --- a/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AbpBlobStoringAliyunModule.cs +++ b/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AbpBlobStoringAliyunModule.cs @@ -3,9 +3,6 @@ using Volo.Abp.Modularity; namespace Volo.Abp.BlobStoring.Aliyun { - /// - /// https://help.aliyun.com/document_detail/31817.html - /// [DependsOn( typeof(AbpBlobStoringModule), typeof(AbpCachingModule) diff --git a/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AliyunBlobNamingNormalizer.cs b/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AliyunBlobNamingNormalizer.cs index e504e30d77..b456d7abb6 100644 --- a/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AliyunBlobNamingNormalizer.cs +++ b/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AliyunBlobNamingNormalizer.cs @@ -9,8 +9,6 @@ namespace Volo.Abp.BlobStoring.Aliyun /// Container names can contain only letters, numbers, and the dash (-) character /// they can't start or end with the dash (-) character /// Container names must be from 3 through 63 characters long - /// 只允许小写字母、数字、短横线(-),且不能以短横线开头或结尾 - /// 3~63 个字符 /// public virtual string NormalizeContainerName(string containerName) { @@ -42,9 +40,6 @@ namespace Volo.Abp.BlobStoring.Aliyun containerName = containerName.Substring(0, 63); } - // can't start or end with the dash (-) character - containerName = containerName.Trim('-'); - return containerName; } diff --git a/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AliyunBlobProvider.cs b/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AliyunBlobProvider.cs index ce558bef0f..a562d93ee3 100644 --- a/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AliyunBlobProvider.cs +++ b/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AliyunBlobProvider.cs @@ -1,4 +1,5 @@ using Aliyun.OSS; +using System; using System.IO; using System.Threading.Tasks; using Volo.Abp.DependencyInjection; @@ -29,30 +30,34 @@ namespace Volo.Abp.BlobStoring.Aliyun return OssClientFactory.Create(aliyunConfig); } - public override async Task SaveAsync(BlobProviderSaveArgs args) + + public override Task SaveAsync(BlobProviderSaveArgs args) { + var containerName = GetContainerName(args); var blobName = AliyunBlobNameCalculator.Calculate(args); - if (!args.OverrideExisting && await ExistsAsync(new BlobProviderExistsArgs(args.ContainerName, args.Configuration, args.BlobName))) + var aliyunConfig = args.Configuration.GetAliyunConfiguration(); + var ossClient = GetOssClient(aliyunConfig); + if (!args.OverrideExisting && BlobExistsAsync(ossClient, containerName, blobName)) { - throw new BlobAlreadyExistsException($"Saving BLOB '{args.BlobName}' does already exists in the container '{args.ContainerName}'! Set {nameof(args.OverrideExisting)} if it should be overwritten."); + throw new BlobAlreadyExistsException($"Saving BLOB '{args.BlobName}' does already exists in the container '{containerName}'! Set {nameof(args.OverrideExisting)} if it should be overwritten."); } - var aliyunConfig = args.Configuration.GetAliyunConfiguration(); - var OssClient = GetOssClient(aliyunConfig); if (aliyunConfig.CreateContainerIfNotExists) { - if (!OssClient.DoesBucketExist(args.ContainerName)) + if (!ossClient.DoesBucketExist(containerName)) { - OssClient.CreateBucket(args.ContainerName); + ossClient.CreateBucket(containerName); } } - OssClient.PutObject(args.ContainerName, blobName, args.BlobStream); + ossClient.PutObject(containerName, blobName, args.BlobStream); + return Task.CompletedTask; } public override Task DeleteAsync(BlobProviderDeleteArgs args) { + var containerName = GetContainerName(args); var blobName = AliyunBlobNameCalculator.Calculate(args); - var OssClient = GetOssClient(args.Configuration); - var result = OssClient.DeleteObject(args.ContainerName, blobName); + var ossClient = GetOssClient(args.Configuration); + var result = ossClient.DeleteObject(containerName, blobName); //TODO: undifend delete flag //https://help.aliyun.com/document_detail/91924.html return Task.FromResult(true); @@ -60,22 +65,39 @@ namespace Volo.Abp.BlobStoring.Aliyun public override Task ExistsAsync(BlobProviderExistsArgs args) { + var containerName = GetContainerName(args); var blobName = AliyunBlobNameCalculator.Calculate(args); - var OssClient = GetOssClient(args.Configuration); - return Task.FromResult(OssClient.DoesObjectExist(args.ContainerName, blobName)); + var ossClient = GetOssClient(args.Configuration); + return Task.FromResult(BlobExistsAsync(ossClient, containerName, blobName)); } - public override async Task GetOrNullAsync(BlobProviderGetArgs args) + public override Task GetOrNullAsync(BlobProviderGetArgs args) { + var containerName = GetContainerName(args); var blobName = AliyunBlobNameCalculator.Calculate(args); - - var OssClient = GetOssClient(args.Configuration); - if (!await ExistsAsync(new BlobProviderExistsArgs(args.ContainerName, args.Configuration, args.BlobName))) + var ossClient = GetOssClient(args.Configuration); + if (!BlobExistsAsync(ossClient, containerName, blobName)) { return null; } - var result = OssClient.GetObject(args.ContainerName, blobName); - return result.Content; + var result = ossClient.GetObject(containerName, blobName); + return Task.FromResult(result.Content); } + + private static string GetContainerName(BlobProviderArgs args) + { + var configuration = args.Configuration.GetAliyunConfiguration(); + return configuration.ContainerName.IsNullOrWhiteSpace() + ? args.ContainerName + : configuration.ContainerName; + } + + private bool BlobExistsAsync(IOss ossClient,string containerName, string blobName) + { + // Make sure Blob Container exists. + return ossClient.DoesBucketExist(containerName) && + ossClient.DoesObjectExist(containerName, blobName); + } + } } diff --git a/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AliyunBlobProviderConfiguration.cs b/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AliyunBlobProviderConfiguration.cs index a4be7cabc2..058f65412c 100644 --- a/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AliyunBlobProviderConfiguration.cs +++ b/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AliyunBlobProviderConfiguration.cs @@ -4,8 +4,6 @@ namespace Volo.Abp.BlobStoring.Aliyun { /// /// Sub-account access to OSS or STS temporary authorization to access OSS - /// 子账号/STS临时授权访问OSS - /// https://help.aliyun.com/document_detail/100624.html /// public class AliyunBlobProviderConfiguration { @@ -21,20 +19,12 @@ namespace Volo.Abp.BlobStoring.Aliyun set => _containerConfiguration.SetConfiguration(AliyunBlobProviderConfigurationNames.AccessKeySecret, Check.NotNullOrWhiteSpace(value, nameof(value))); } - /// - /// https://help.aliyun.com/document_detail/31837.html - /// eg: https://oss-cn-beijing.aliyuncs.com - /// public string Endpoint { get => _containerConfiguration.GetConfiguration(AliyunBlobProviderConfigurationNames.Endpoint); set => _containerConfiguration.SetConfiguration(AliyunBlobProviderConfigurationNames.Endpoint, Check.NotNullOrWhiteSpace(value, nameof(value))); } - /// - /// STS https://help.aliyun.com/document_detail/66053.html - /// eg:cn-beijing - /// public string RegionId { get => _containerConfiguration.GetConfiguration(AliyunBlobProviderConfigurationNames.RegionId); @@ -42,7 +32,7 @@ namespace Volo.Abp.BlobStoring.Aliyun } /// - /// eg:acs:ram::$accountID:role/$roleName + /// acs:ram::$accountID:role/$roleName /// public string RoleArn { @@ -52,7 +42,6 @@ namespace Volo.Abp.BlobStoring.Aliyun /// /// The name used to identify the temporary access credentials, it is recommended to use different application users to distinguish. - /// 用来标识临时访问凭证的名称,建议使用不同的应用程序用户来区分。 /// public string RoleSessionName { @@ -62,7 +51,6 @@ namespace Volo.Abp.BlobStoring.Aliyun /// /// Set the validity period of the temporary access credential, the unit is s, the minimum is 900, and the maximum is 3600. - /// 设置临时访问凭证的有效期,单位是s,最小为900,最大为3600。 /// public int DurationSeconds { @@ -70,12 +58,27 @@ namespace Volo.Abp.BlobStoring.Aliyun set => _containerConfiguration.SetConfiguration(AliyunBlobProviderConfigurationNames.DurationSeconds, value); } + /// + /// If policy is empty, the user will get all permissions under this role + /// public string Policy { get => _containerConfiguration.GetConfiguration(AliyunBlobProviderConfigurationNames.Policy); set => _containerConfiguration.SetConfiguration(AliyunBlobProviderConfigurationNames.Policy, value); } + /// + /// This name may only contain lowercase letters, numbers, and hyphens, and must begin with a letter or a number. + /// Each hyphen must be preceded and followed by a non-hyphen character. + /// The name must also be between 3 and 63 characters long. + /// If this parameter is not specified, the ContainerName of the will be used. + /// + public string ContainerName + { + get => _containerConfiguration.GetConfiguration(AliyunBlobProviderConfigurationNames.ContainerName); + set => _containerConfiguration.SetConfiguration(AliyunBlobProviderConfigurationNames.ContainerName, Check.NotNullOrWhiteSpace(value, nameof(value))); + } + /// /// Default value: false. /// @@ -92,15 +95,11 @@ namespace Volo.Abp.BlobStoring.Aliyun _containerConfiguration = containerConfiguration; } - public string ToOssKeyString() - { - Uri uPoint = new Uri(Endpoint); - return $"memorycache:aliyun:id:{AccessKeyId},sec:{AccessKeySecret},ept:{uPoint.Host.ToLower()}"; - } - public string ToOssWithStsKeyString() + public string ToKeyString() { - return ToOssKeyString() + $",rid:{RegionId},ra:{RoleArn},rsn:{RoleSessionName},pl:{Policy}"; + Uri uPoint = new Uri(Endpoint); + return $"blobstoring:aliyun:id:{AccessKeyId},sec:{AccessKeySecret},ept:{uPoint.Host.ToLower()},rid:{RegionId},ra:{RoleArn},rsn:{RoleSessionName},pl:{Policy}"; } } } diff --git a/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AliyunBlobProviderConfigurationNames.cs b/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AliyunBlobProviderConfigurationNames.cs index c2b0e5ea7b..c889b374cc 100644 --- a/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AliyunBlobProviderConfigurationNames.cs +++ b/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AliyunBlobProviderConfigurationNames.cs @@ -10,6 +10,7 @@ public const string RoleSessionName = "Aliyun.RoleSessionName"; public const string DurationSeconds = "Aliyun.DurationSeconds"; public const string Policy = "Aliyun.Policy"; + public const string ContainerName = "Aliyun:ContainerName"; public const string CreateContainerIfNotExists = "Aliyun.CreateContainerIfNotExists"; } } diff --git a/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AssumeRoleCredentialsCacheItem.cs b/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AssumeRoleCredentialsCacheItem.cs new file mode 100644 index 0000000000..e0d75f2954 --- /dev/null +++ b/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/AssumeRoleCredentialsCacheItem.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Volo.Abp.Caching; + +namespace Volo.Abp.BlobStoring.Aliyun +{ + [Serializable] + [CacheName("AssumeRoleCredentials")] + public class AssumeRoleCredentialsCacheItem + { + public string AccessKeyId { get; set; } + + public string AccessKeySecret { get; set; } + + public string SecurityToken { get; set; } + + public AssumeRoleCredentialsCacheItem() + { + + } + + public AssumeRoleCredentialsCacheItem(string accessKeyId,string accessKeySecret,string securityToken) + { + AccessKeyId = accessKeyId; + AccessKeySecret = accessKeySecret; + SecurityToken = securityToken; + } + } +} diff --git a/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/DefaultOssClientFactory.cs b/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/DefaultOssClientFactory.cs index 8f068a4822..8b8e466232 100644 --- a/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/DefaultOssClientFactory.cs +++ b/framework/src/Volo.Abp.BlobStoring.Aliyun/Volo/Abp/BlobStoring/Aliyun/DefaultOssClientFactory.cs @@ -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 { /// /// Sub-account access to OSS or STS temporary authorization to access OSS - /// 子账号/STS临时授权访问OSS - /// STS:https://help.aliyun.com/document_detail/28756.html /// public class DefaultOssClientFactory : IOssClientFactory, ITransientDependency { - private readonly IMemoryCache _cache; + private readonly IDistributedCache _cache; public DefaultOssClientFactory( - IMemoryCache cache) + IDistributedCache 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(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.ToOssWithStsKeyString(); - var iOssClient = _cache.Get(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 - 10) + }); + } + return new OssClient(aliyunConfig.Endpoint, cacheItem.AccessKeyId, cacheItem.AccessKeySecret, cacheItem.SecurityToken); } } } diff --git a/framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Properties/serviceDependencies.json b/framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Properties/serviceDependencies.json deleted file mode 100644 index a4e7aa3d33..0000000000 --- a/framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Properties/serviceDependencies.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "dependencies": { - "secrets1": { - "type": "secrets" - } - } -} \ No newline at end of file diff --git a/framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Properties/serviceDependencies.local.json b/framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Properties/serviceDependencies.local.json deleted file mode 100644 index 09b109bc6f..0000000000 --- a/framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Properties/serviceDependencies.local.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "dependencies": { - "secrets1": { - "type": "secrets.user" - } - } -} \ No newline at end of file diff --git a/framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Volo.Abp.BlobStoring.Aliyun.Tests.csproj b/framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Volo.Abp.BlobStoring.Aliyun.Tests.csproj index c9ba95211c..121832cefb 100644 --- a/framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Volo.Abp.BlobStoring.Aliyun.Tests.csproj +++ b/framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Volo.Abp.BlobStoring.Aliyun.Tests.csproj @@ -17,4 +17,8 @@ + + + + diff --git a/framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Volo/Abp/BlobStoring/Aliyun/AbpBlobStoringAliyunTestModule.cs b/framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Volo/Abp/BlobStoring/Aliyun/AbpBlobStoringAliyunTestModule.cs index f62fe9e4b6..6c14cebbb8 100644 --- a/framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Volo/Abp/BlobStoring/Aliyun/AbpBlobStoringAliyunTestModule.cs +++ b/framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Volo/Abp/BlobStoring/Aliyun/AbpBlobStoringAliyunTestModule.cs @@ -21,6 +21,9 @@ namespace Volo.Abp.BlobStoring.Aliyun { private const string UserSecretsId = "fe9a87da-3584-40e0-a06c-aa499936015d"; + private AliyunBlobProviderConfiguration _configuration; + private readonly string _randomContainerName = "abp-aliyun-test-container-" + Guid.NewGuid().ToString("N"); + public override void ConfigureServices(ServiceConfigurationContext context) { context.Services.ReplaceConfiguration(ConfigurationHelper.BuildConfiguration(builderAction: builder => @@ -43,19 +46,28 @@ namespace Volo.Abp.BlobStoring.Aliyun { aliyun.AccessKeyId = _accessKeyId; aliyun.AccessKeySecret = _accessKeySecret; - aliyun.Endpoint = _endpoint;//eg:https://oss-cn-beijing.aliyuncs.com + aliyun.Endpoint = _endpoint; //STS - aliyun.RegionId = _regionId;//eg:cn-beijing - aliyun.RoleArn = _roleArn;//eg:acs:ram::1320235309887297:role/role-oss-xxxxx + aliyun.RegionId = _regionId; + aliyun.RoleArn = _roleArn; aliyun.RoleSessionName = Guid.NewGuid().ToString("N"); aliyun.DurationSeconds = 3600; aliyun.Policy = String.Empty; //Other aliyun.CreateContainerIfNotExists = true; + aliyun.ContainerName = _randomContainerName; + _configuration = aliyun; }); }); }); } + public override void OnApplicationShutdown(ApplicationShutdownContext context) + { + var ossClientFactory = context.ServiceProvider.GetService(); + var ossClient = ossClientFactory.Create(_configuration); + ossClient.DeleteBucket(_randomContainerName); + } + } } diff --git a/framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Volo/Abp/BlobStoring/Aliyun/AliyunBlobContainer_Tests.cs b/framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Volo/Abp/BlobStoring/Aliyun/AliyunBlobContainer_Tests.cs index f9501baa64..29f84091df 100644 --- a/framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Volo/Abp/BlobStoring/Aliyun/AliyunBlobContainer_Tests.cs +++ b/framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Volo/Abp/BlobStoring/Aliyun/AliyunBlobContainer_Tests.cs @@ -5,30 +5,13 @@ using Xunit; namespace Volo.Abp.BlobStoring.Aliyun { - //public class AliyunBlobContainer_Tests : BlobContainer_Tests - //{ - // private readonly IBlobContainer _blobContainer; - // private readonly string _blobname = "my-blob"; - // private readonly string _stringContent = "my-blob-content"; - // public AliyunBlobContainer_Tests() - // { - // _blobContainer = GetRequiredService>(); - // } - - // [Fact] - // public async void BlobContainer_Test() - // { - // var streamContent = Encoding.Default.GetBytes(_stringContent); - // await _blobContainer.SaveAsync(_blobname, streamContent, true); - // var streamData = await _blobContainer.GetAsync(_blobname); - // var tfExist = await _blobContainer.ExistsAsync(_blobname); - // if (tfExist) - // { - // await _blobContainer.DeleteAsync(_blobname); - // } - // StreamReader reader = new StreamReader(streamData); - // var stringData = reader.ReadToEnd();// Encoding.Default.GetString(bytes); - // stringData.ShouldBe(_stringContent); - // } - //} + /* + //Please set the correct connection string in secrets.json and continue the test. + public class AliyunBlobContainer_Tests : BlobContainer_Tests + { + public AliyunBlobContainer_Tests() + { + } + } + */ } diff --git a/framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Volo/Abp/BlobStoring/Aliyun/DefaultAliyunBlobNamingNormalizerProvider_Tests.cs b/framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Volo/Abp/BlobStoring/Aliyun/DefaultAliyunBlobNamingNormalizerProvider_Tests.cs index eebb43f1c8..895c87cc91 100644 --- a/framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Volo/Abp/BlobStoring/Aliyun/DefaultAliyunBlobNamingNormalizerProvider_Tests.cs +++ b/framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Volo/Abp/BlobStoring/Aliyun/DefaultAliyunBlobNamingNormalizerProvider_Tests.cs @@ -54,13 +54,5 @@ namespace Volo.Abp.BlobStoring.Aliyun filename.Length.ShouldBeLessThanOrEqualTo(63); } - - [Fact] - public void NormalizeContainerName_Trim() - { - var filename = "-abpabpabpabp-"; - filename = _blobNamingNormalizer.NormalizeContainerName(filename); - filename.ShouldBe("abpabpabpabp"); - } } } diff --git a/framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Volo/Abp/BlobStoring/Aliyun/MyTestContainer.cs b/framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Volo/Abp/BlobStoring/Aliyun/MyTestContainer.cs deleted file mode 100644 index 71daed58ff..0000000000 --- a/framework/test/Volo.Abp.BlobStoring.Aliyun.Tests/Volo/Abp/BlobStoring/Aliyun/MyTestContainer.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Volo.Abp.BlobStoring.Aliyun -{ - [BlobContainerName("my-container")] - public class MyTestContainer - { - } -}