44using System . Net . Http ;
55using Amazon ;
66using Amazon . Runtime ;
7+ using Amazon . Runtime . CredentialManagement ;
78#if NETSTANDARD
89using Amazon . Extensions . NETCore . Setup ;
910#endif
@@ -18,13 +19,34 @@ public class AwsHttpConnection : HttpConnection
1819 private readonly AWSCredentials _credentials ;
1920 private readonly RegionEndpoint _region ;
2021
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+
2130#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+
2243 /// <summary>
2344 /// Initializes a new instance of the AwsHttpConnection using AWSOptions.
2445 /// </summary>
2546 /// <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 ( ) )
2850 {
2951 }
3052#endif
@@ -45,16 +67,16 @@ public AwsHttpConnection(AWSCredentials credentials, RegionEndpoint region)
4567 /// </summary>
4668 /// <param name="region">AWS region</param>
4769 public AwsHttpConnection ( string region )
48- : this ( FallbackCredentialsFactory . GetCredentials ( ) , RegionEndpoint . GetBySystemName ( region ) )
70+ : this ( GetCredentials ( ) , RegionEndpoint . GetBySystemName ( region ) )
4971 {
5072 }
5173
5274 /// <summary>
5375 /// Initializes a new instance of the AwsHttpConnection class with credentials from the Instance Profile service
5476 /// </summary>
5577 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 ( ) )
5880 {
5981 }
6082
0 commit comments