Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ internal ClientFactory(AWSOptions awsOptions, IAWSCredentialsFactory credentials
/// Creates the AWS service client that implements the service client interface.
/// </summary>
/// <returns>The AWS service client</returns>
internal IAmazonService CreateServiceClient()
internal IAmazonService CreateServiceClient(AWSCredentials credentials = null)
{
PerformGlobalConfig(_logger, _options);
var credentials = _credentialsFactory.Create();
credentials = credentials ?? _credentialsFactory.Create();

if (!string.IsNullOrEmpty(_options?.SessionRoleArn))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,94 @@ public static IServiceCollection AddDefaultAWSOptions(
return collection;
}

/// <summary>
///
/// </summary>
/// <param name="collection"></param>
/// <param name="lifetime"></param>
/// <returns></returns>
public static IServiceCollection AddAWSCredentials(
this IServiceCollection collection,
ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
collection.Add(new ServiceDescriptor(typeof(AWSCredentials), sp => sp.CreateDefaultAWSCredentials(), lifetime));
return collection;
}

/// <summary>
///
/// </summary>
/// <param name="collection"></param>
/// <param name="credentials"></param>
/// <returns></returns>
public static IServiceCollection AddAWSCredentials(
this IServiceCollection collection,
AWSCredentials credentials)
{
collection.Add(new ServiceDescriptor(typeof(AWSCredentials), credentials));
return collection;
}

/// <summary>
///
/// </summary>
/// <param name="collection"></param>
/// <param name="credentialsFunc"></param>
/// <param name="lifetime"></param>
/// <returns></returns>
public static IServiceCollection AddAWSCredentials(
this IServiceCollection collection,
Func<IServiceProvider, AWSCredentials> credentialsFunc,
ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
collection.Add(new ServiceDescriptor(typeof(AWSCredentials), credentialsFunc, lifetime));
return collection;
}

/// <summary>
///
/// </summary>
/// <param name="collection"></param>
/// <param name="lifetime"></param>
/// <returns></returns>
public static IServiceCollection TryAddAWSCredentials(
this IServiceCollection collection,
ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
collection.TryAdd(new ServiceDescriptor(typeof(AWSCredentials), sp => sp.CreateDefaultAWSCredentials(), lifetime));
return collection;
}

/// <summary>
///
/// </summary>
/// <param name="collection"></param>
/// <param name="credentials"></param>
/// <returns></returns>
public static IServiceCollection TryAddAWSCredentials(
this IServiceCollection collection,
AWSCredentials credentials)
{
collection.TryAdd(new ServiceDescriptor(typeof(AWSCredentials), credentials));
return collection;
}

/// <summary>
///
/// </summary>
/// <param name="collection"></param>
/// <param name="credentialsFunc"></param>
/// <param name="lifetime"></param>
/// <returns></returns>
public static IServiceCollection TryAddAWSCredentials(
this IServiceCollection collection,
Func<IServiceProvider, AWSCredentials> credentialsFunc,
ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
collection.TryAdd(new ServiceDescriptor(typeof(AWSCredentials), credentialsFunc, lifetime));
return collection;
}

/// <summary>
///
/// </summary>
Expand Down Expand Up @@ -394,13 +482,19 @@ private static object CreateServiceClient<T>(AWSOptions options, IServiceProvide

var factory = new ClientFactory<T>(awsOptions, credentialsFactory, logger);

return factory.CreateServiceClient();
return factory.CreateServiceClient(sp.GetService<AWSCredentials>());
}

private static IAWSCredentialsFactory CreateDefaultCredentialsFactory(IServiceProvider sp)
private static IAWSCredentialsFactory CreateDefaultCredentialsFactory(this IServiceProvider sp)
{
var options = sp.GetService<AWSOptions>() ?? new AWSOptions();
return new DefaultAWSCredentialsFactory(options, sp.GetService<ILogger>());
}

private static AWSCredentials CreateDefaultAWSCredentials(this IServiceProvider sp)
{
var credentialsFactory = sp.GetService<IAWSCredentialsFactory>() ?? sp.CreateDefaultCredentialsFactory();
return credentialsFactory.Create();
}
}
}