Skip to content

Commit

Permalink
feat(Storage.Aliyun): Added AddAliyunStorage (#69)
Browse files Browse the repository at this point in the history
* feat(Storage.Aliyun): Added AddAliyunStorage

* chore: Optimize AddAliyunStorage

* test(Storage.Aliyun): Handling unit tests
  • Loading branch information
zhenlei520 authored May 26, 2022
1 parent 1976e16 commit 6350c39
Showing 1 changed file with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,31 @@ public static IServiceCollection AddAliyunStorage(this IServiceCollection servic
return services.AddAliyunStorage(() => options);
}

public static IServiceCollection AddAliyunStorage(this IServiceCollection services, Func<AliyunStorageOptions> func)
public static IServiceCollection AddAliyunStorage(this IServiceCollection services, Func<IServiceProvider, AliyunStorageOptions> func)
{
ArgumentNullException.ThrowIfNull(func, nameof(func));

services.AddAliyunStorageDepend();
services.TryAddSingleton<IOssClientFactory, DefaultOssClientFactory>();
services.TryAddSingleton<ICredentialProvider>(serviceProvider => new DefaultCredentialProvider(
GetOssClientFactory(serviceProvider),
func.Invoke(),
func.Invoke(serviceProvider),
GetMemoryCache(serviceProvider),
GetDefaultCredentialProviderLogger(serviceProvider)));
services.TryAddSingleton<IClient>(serviceProvider => new Client(
GetCredentialProvider(serviceProvider),
func.Invoke(),
func.Invoke(serviceProvider),
GetClientLogger(serviceProvider)));
return services;
}

public static IServiceCollection AddAliyunStorage(this IServiceCollection services, Func<AliyunStorageOptions> func)
{
ArgumentNullException.ThrowIfNull(func, nameof(func));

return services.AddAliyunStorage(_ => func.Invoke());
}

private static IServiceCollection AddAliyunStorageDepend(this IServiceCollection services)
{
services.AddMemoryCache();
Expand Down Expand Up @@ -113,7 +120,8 @@ private static void CheckAliYunStorageOptions(AliyunStorageOptions options)
ArgumentNullException.ThrowIfNull(options, nameof(options));

if (options.AccessKeyId == null && options.AccessKeySecret == null)
throw new ArgumentException($"{nameof(options.AccessKeyId)}, {nameof(options.AccessKeySecret)} are required and cannot be empty");
throw new ArgumentException(
$"{nameof(options.AccessKeyId)}, {nameof(options.AccessKeySecret)} are required and cannot be empty");

ObjectStorageExtensions.CheckNullOrEmptyAndReturnValue(options.AccessKeyId, nameof(options.AccessKeyId));
ObjectStorageExtensions.CheckNullOrEmptyAndReturnValue(options.AccessKeySecret, nameof(options.AccessKeySecret));
Expand Down

0 comments on commit 6350c39

Please sign in to comment.