/// <summary> /// Initializes a new instance. /// </summary> protected AWSQueue(AWSSettings settings, string queueName) { _client = AWSClient.Create( settings, (creds, reg) => new AmazonSQSClient(creds, reg), creds => new AmazonSQSClient(creds), reg => new AmazonSQSClient(reg), () => new AmazonSQSClient()); _queueUrl = new Lazy <string>(() => _client.GetQueueUrlAsync(queueName) .ConfigureAwait(false).GetAwaiter().GetResult().QueueUrl); }
/// <summary> /// Helps creating AWS client based on specified settings. /// Provide AWS constructors for each case, and have specific client initialized based on which /// settings components are provided. /// </summary> /// <example><![CDATA[ /// public class MyQueue /// { /// private readonly AmazonSQSClient _client; /// private readonly Lazy<string> _queueUrl; /// /// public MyQueue(AWSSettings settings, string queueName) /// { /// _client = AWSClient.Create( /// settings, /// (creds, reg) => new AmazonSQSClient(creds, reg), /// creds => new AmazonSQSClient(creds), /// reg => new AmazonSQSClient(reg), /// () => new AmazonSQSClient()); /// /// _queueUrl = new Lazy<string>(() => _client.GetQueueUrlAsync(queueName) /// .ConfigureAwait(false).GetAwaiter().GetResult().QueueUrl); /// } /// } /// ]]></example> public static T Create <T>( AWSSettings settings, Func <AWSCredentials, RegionEndpoint, T> createByCredentialsAndRegion, Func <AWSCredentials, T> createByCredentials, Func <RegionEndpoint, T> createByRegion, Func <T> createByDefault) { SharedCredentialsFile file = null; CredentialProfile profile = null; AWSCredentials credentials = null; RegionEndpoint region = null; var hasKey = !String.IsNullOrWhiteSpace(settings?.Key); var hasSecret = !String.IsNullOrWhiteSpace(settings?.Secret); var hasRegion = !String.IsNullOrWhiteSpace(settings?.Region); var hasProfile = !String.IsNullOrWhiteSpace(settings?.UseProfile); if (hasProfile) { file = new SharedCredentialsFile(); if (!file.TryGetProfile(settings.UseProfile, out profile)) { throw new InvalidOperationException($"Cannot read AWS profile '{settings.UseProfile}'"); } } if (hasKey && hasSecret) { credentials = new BasicAWSCredentials(settings.Key, settings.Secret); } else if (hasProfile) { if (!AWSCredentialsFactory.TryGetAWSCredentials(profile, file, out credentials)) { throw new InvalidOperationException($"Cannot get AWS credentials from profile '{profile.Name}'"); } } if (hasRegion) { region = RegionEndpoint.GetBySystemName(settings.Region); } else if (hasProfile) { region = profile.Region; } if (credentials != null && region != null) { return(createByCredentialsAndRegion(credentials, region)); } if (credentials != null) { return(createByCredentials(credentials)); } if (region != null) { return(createByRegion(region)); } return(createByDefault()); }