4
4
using System . Net . Http ;
5
5
using Amazon ;
6
6
using Amazon . Runtime ;
7
+ using Amazon . Runtime . CredentialManagement ;
7
8
#if NETSTANDARD
8
9
using Amazon . Extensions . NETCore . Setup ;
9
10
#endif
@@ -18,13 +19,34 @@ public class AwsHttpConnection : HttpConnection
18
19
private readonly AWSCredentials _credentials ;
19
20
private readonly RegionEndpoint _region ;
20
21
22
+ static AWSCredentials GetCredentials ( )
23
+ => FallbackCredentialsFactory . GetCredentials ( )
24
+ ?? throw new Exception ( "Unable to obtain AWS Credentials." ) ;
25
+
26
+ static RegionEndpoint GetRegion ( )
27
+ => FallbackRegionFactory . GetRegionEndpoint ( )
28
+ ?? throw new Exception ( "Unable to determine the correct AWS region. Please try providing it explicitly." ) ;
29
+
21
30
#if NETSTANDARD
31
+ static AWSCredentials GetCredentialsFromOptions ( AWSOptions options )
32
+ {
33
+ var ret = options . Credentials ;
34
+ if ( ret != null ) return ret ;
35
+ if ( ! string . IsNullOrEmpty ( options . Profile ) )
36
+ {
37
+ var chain = new CredentialProfileStoreChain ( options . ProfilesLocation ) ;
38
+ if ( chain . TryGetAWSCredentials ( options . Profile , out ret ) ) return ret ;
39
+ }
40
+ return GetCredentials ( ) ;
41
+ }
42
+
22
43
/// <summary>
23
44
/// Initializes a new instance of the AwsHttpConnection using AWSOptions.
24
45
/// </summary>
25
46
/// <param name="options">The AWS options.</param>
26
- public AwsHttpConnection ( AWSOptions options )
27
- : this ( options . Credentials , options . Region )
47
+ public AwsHttpConnection ( AWSOptions options ) : this (
48
+ GetCredentialsFromOptions ( options ) ,
49
+ options . Region ?? GetRegion ( ) )
28
50
{
29
51
}
30
52
#endif
@@ -45,16 +67,16 @@ public AwsHttpConnection(AWSCredentials credentials, RegionEndpoint region)
45
67
/// </summary>
46
68
/// <param name="region">AWS region</param>
47
69
public AwsHttpConnection ( string region )
48
- : this ( FallbackCredentialsFactory . GetCredentials ( ) , RegionEndpoint . GetBySystemName ( region ) )
70
+ : this ( GetCredentials ( ) , RegionEndpoint . GetBySystemName ( region ) )
49
71
{
50
72
}
51
73
52
74
/// <summary>
53
75
/// Initializes a new instance of the AwsHttpConnection class with credentials from the Instance Profile service
54
76
/// </summary>
55
77
public AwsHttpConnection ( ) : this (
56
- FallbackCredentialsFactory . GetCredentials ( ) ,
57
- FallbackRegionFactory . GetRegionEndpoint ( ) ?? throw new Exception ( "Unable to determine the correct AWS region. Please try providing it explicitly." ) )
78
+ GetCredentials ( ) ,
79
+ GetRegion ( ) )
58
80
{
59
81
}
60
82
0 commit comments