示例#1
0
        public async Task <IEnumerable <S3Object> > GetObjectsList()
        {
            using var s3Client = awsOptions.CreateServiceClient <IAmazonS3>();
            var listObjectsResponse = await s3Client.ListObjectsAsync(new ListObjectsRequest
            {
                BucketName = bucketName
            });

            return(listObjectsResponse.S3Objects);
        }
示例#2
0
        protected void ResolveHostConfig()
        {
            _logger.LogInformation("Resolving Host Configuration");
            var configBuilder = new ConfigurationBuilder()
                                .SetBasePath(Directory.GetCurrentDirectory())
                                .AddEnvironmentVariables(prefix: HostSettings.ConfigEnvPrefix);

            _hostConfig = configBuilder.Build();
            _settings   = _hostConfig.Get <HostSettings>();
            _awsOptions = _hostConfig.GetAWSOptions();

            if (_settings.AppSettingsS3Bucket != null && _settings.AppSettingsS3Key != null)
            {
                _logger.LogInformation($"Resolved AppSettings S3 source as"
                                       + $" [{_settings.AppSettingsS3Bucket}][{_settings.AppSettingsS3Key}]");
                var s3      = _awsOptions.CreateServiceClient <IAmazonS3>();
                var getResp = s3.GetObjectAsync(_settings.AppSettingsS3Bucket, _settings.AppSettingsS3Key).Result;

                var localJson = HostSettings.AppSettingsLocalJsonFile;

                using (getResp)
                    using (var rs = getResp.ResponseStream)
                        using (var fs = File.OpenWrite(localJson))
                        {
                            rs.CopyTo(fs);
                        }
                _logger.LogInformation($"Copied AppSettings from S3 source to local file at [{localJson}]");
            }
        }
示例#3
0
        public async Task Start(AWSOptions awsOptions, string queueName, CancellationToken cancel)
        {
            using (var client = awsOptions.CreateServiceClient <IAmazonSQS>())
            {
                var getQueueResult = await client.GetQueueUrlAsync(queueName, cancel);

                while (cancel.IsCancellationRequested == false)
                {
                    Console.WriteLine("Waiting for messages....");

                    ReceiveMessageRequest rxMessage = new ReceiveMessageRequest();
                    rxMessage.QueueUrl            = getQueueResult.QueueUrl;
                    rxMessage.WaitTimeSeconds     = 20;
                    rxMessage.MaxNumberOfMessages = 10;

                    ReceiveMessageResponse rxMessageResponse = await client.ReceiveMessageAsync(rxMessage);

                    if (rxMessageResponse.Messages.Any() == false)
                    {
                        return;
                    }

                    Console.WriteLine($"Got {rxMessageResponse.Messages.Count} messages");

                    var deletions = rxMessageResponse.Messages.Select(m => new DeleteMessageBatchRequestEntry(m.MessageId, m.ReceiptHandle));

                    Console.WriteLine("Deleting messages....");

                    await client.DeleteMessageBatchAsync(getQueueResult.QueueUrl, deletions.ToList());
                }
            }
        }
        /// <summary>
        /// This exists only to populate some sample data to be used by this example project
        /// </summary>
        private static async Task PopulateSampleDataForThisProject()
        {
            var awsOptions = new AWSOptions {
                Region = RegionEndpoint.USEast1
            };

            var root       = $"/dotnet-aws-samples/systems-manager-sample/common";
            var parameters = new[]
            {
                new { Name = "StringValue", Value = "string-value" },
                new { Name = "IntegerValue", Value = "10" },
                new { Name = "DateTimeValue", Value = "2000-01-01" },
                new { Name = "BooleanValue", Value = "True" },
                new { Name = "TimeSpanValue", Value = "00:05:00" },
            };

            using (var client = awsOptions.CreateServiceClient <IAmazonSimpleSystemsManagement>())
            {
                var result = await client.GetParametersByPathAsync(new GetParametersByPathRequest { Path = root, Recursive = true }).ConfigureAwait(false);

                if (result.Parameters.Count == parameters.Length)
                {
                    return;
                }

                foreach (var parameter in parameters)
                {
                    var name = $"{root}/settings/{parameter.Name}";
                    await client.PutParameterAsync(new PutParameterRequest { Name = name, Value = parameter.Value, Type = ParameterType.String, Overwrite = true }).ConfigureAwait(false);
                }
            }
        }
示例#5
0
        //[Fact]
        public async Task TestGenerateCredentialReport()
        {
            IAmazonIdentityManagementService iamClient =
                _awsOptions.CreateServiceClient <IAmazonIdentityManagementService>();
            IAmazonSimpleEmailService sesClient =
                _awsOptions.CreateServiceClient <IAmazonSimpleEmailService>();

            var logger  = new TestLambdaLogger();
            var context = new TestLambdaContext
            {
                Logger = logger
            };

            var function = new Function(iamClient, sesClient);
            await function.FunctionHandler(Function.GenerateReportCommand, context);
        }
示例#6
0
        public T GetAWSClient <T>(AWSCredentials credentials, string region) where T : IAmazonService
        {
            var awsOptions = new AWSOptions {
                Credentials = credentials, Region = RegionEndpoint.GetBySystemName(region)
            };

            return(awsOptions.CreateServiceClient <T>());
        }
示例#7
0
 public SnsSink(IFormatProvider formatProvider, AWSOptions awsOptions, string logTopicArn, LogEventLevel minimumLevel, string outputTemplate)
 {
     _minimumLevel   = minimumLevel;
     _formatProvider = formatProvider;
     _textFormatter  = new Serilog.Formatting.Display.MessageTemplateTextFormatter(outputTemplate, formatProvider);
     _logTopicArn    = logTopicArn;
     _snsClient      = awsOptions.CreateServiceClient <IAmazonSimpleNotificationService>();
 }
示例#8
0
        public T GetAWSClient <T>() where T : IAmazonService
        {
            var awsOptions = new AWSOptions();

            _awsOptionsAction?.Invoke(awsOptions);

            return(awsOptions.CreateServiceClient <T>());
        }
示例#9
0
 public SQSQueueMananger(
     AWSOptions options,
     string queueName)
 {
     _AmazonSQSClient = options.CreateServiceClient <IAmazonSQS>();
     _QueueName       = queueName;
     _QueueUrl        = GetQueueUrl().Result.QueueUrl;
 }
示例#10
0
        private void seedTestData()
        {
            bool success = false;

            using (var client = AWSOptions.CreateServiceClient <IAmazonSimpleSystemsManagement>())
            {
                var tasks = new List <Task>();
                foreach (var kv in TestData)
                {
                    Console.WriteLine($"Adding parameter: ({ParameterPrefix + kv.Key}, {kv.Value})");
                    tasks.Add(client.PutParameterAsync(new PutParameterRequest
                    {
                        Name  = ParameterPrefix + kv.Key,
                        Value = kv.Value,
                        Type  = ParameterType.String
                    }));
                }
                ;
                Task.WaitAll(tasks.ToArray());

                // due to eventual consistency, wait for 5 sec increments for 3 times to verify
                // test data is correctly set before executing tests.
                const int tries = 3;
                for (int i = 0; i < tries; i++)
                {
                    int count = 0;
                    GetParametersByPathResponse response;
                    do
                    {
                        response = client.GetParametersByPathAsync(new GetParametersByPathRequest
                        {
                            Path = ParameterPrefix
                        }).Result;

                        count += response.Parameters.Count;
                    } while (!string.IsNullOrEmpty(response.NextToken));

                    success = (count == TestData.Count);

                    if (success)
                    {
                        Console.WriteLine("Verified that test data is available.");
                        break;
                    }
                    else
                    {
                        Console.WriteLine($"Waiting on test data to be available. Waiting {count + 1}/{tries}");
                        Thread.Sleep(5 * 1000);
                    }
                }
            }

            if (!success)
            {
                throw new Exception("Failed to seed integration test data");
            }
        }
        public AwsManagers(AwsConfig config)
        {
            var options = new AWSOptions
            {
                Credentials = new BasicAWSCredentials(config.AccessKey, config.SecretKey),
                Region      = RegionEndpoint.GetBySystemName(config.Region)
            };

            s3Client       = options.CreateServiceClient <IAmazonS3>();
            sqsCLient      = options.CreateServiceClient <IAmazonSQS>();
            textractClient = options.CreateServiceClient <IAmazonTextract>();


            dbClient = new AmazonDynamoDBClient(config.AccessKey, config.SecretKey, RegionEndpoint.GetBySystemName(config.Region));

            bucketName = config.Bucket;
            queueUrl   = config.Queue;
            tableName  = config.Table;
        }
示例#12
0
        public DynamoLogic()
        {
            var options = new AWSOptions
            {
                Profile = "PortfolioDBUser",
                Region  = RegionEndpoint.USEast1
            };

            DynamoClient = options.CreateServiceClient <IAmazonDynamoDB>();
        }
示例#13
0
        public static async Task <AssumeRoleResponse> ManualAssume(AWSOptions options)
        {
            var stsClient           = options.CreateServiceClient <IAmazonSecurityTokenService>();
            var assumedRoleResponse = await stsClient.AssumeRoleAsync(new AssumeRoleRequest()
            {
                RoleArn         = "arn:aws:iam::464534050515:role/SimpleStepFunction-StateMachineRole-1N638DC3RLA16",
                RoleSessionName = "test"
            });

            return(assumedRoleResponse);
        }
示例#14
0
        public static async Task AssumeGivenRole(AWSOptions awsOptions, string roleArn, string roleSessionName, Action <AssumeRoleResponse> execute)
        {
            var stsClient           = awsOptions.CreateServiceClient <IAmazonSecurityTokenService>();
            var assumedRoleResponse = await stsClient.AssumeRoleAsync(new AssumeRoleRequest()
            {
                RoleArn         = roleArn,
                RoleSessionName = roleSessionName
            });

            execute(assumedRoleResponse);
        }
    public static IS3ClientFactory Create(string[] regions, AWSOptions options)
    {
        var factory = new S3ClientFactory();

        foreach (var region in regions)
        {
            var regionEndPoint = RegionEndpoint.GetBySystemName(region);
            options.Region = regionEndPoint;
            factory._container.Add(region, options.CreateServiceClient <IAmazonS3>());
        }
        return(factory);
    }
示例#16
0
        public S3FileService(ILogger <S3FileService> logger, IOptions <S3StorageOptions> awsConfiguration)
        {
            _logger          = logger;
            _s3Configuration = awsConfiguration.Value;
            var awsOptions = new AWSOptions
            {
                Region      = RegionEndpoint.GetBySystemName(_s3Configuration.Region),
                Credentials = new BasicAWSCredentials(_s3Configuration.ApiKey, _s3Configuration.Secret)
            };

            _client = awsOptions.CreateServiceClient <IAmazonS3>();
        }
 /// <summary>
 /// Creates a new <see cref="S3StorageSession"/> with a specified configuration.
 /// </summary>
 /// <param name="storageOptions"><see cref="PiranhaS3StorageOptions"/> used to configure the Piranda S3 storage.</param>
 /// <param name="awsOptions">The <see cref="AWSOptions"/> used to create the S3 service client.</param>
 /// <param name="logger">Namespace <see cref="ILogger"/> used for logging.</param>
 public S3StorageSession(PiranhaS3StorageOptions storageOptions, AWSOptions awsOptions, ILogger logger)
 {
     StorageOptions = storageOptions ?? throw new ArgumentNullException(nameof(storageOptions));
     if (awsOptions != null)
     {
         S3Client = awsOptions.CreateServiceClient <IAmazonS3>();
     }
     else
     {
         S3Client = new AmazonS3Client();
     }
     Logger = logger;
 }
示例#18
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors();
            services.AddControllers();

            AWSOptions            options   = Configuration.GetAWSOptions();
            IAmazonSecretsManager client    = options.CreateServiceClient <IAmazonSecretsManager>();
            DBSettings            DBSecret  = JsonConvert.DeserializeObject <DBSettings>(GetDBSecret(client));
            JWTSettings           JWTSecret = JsonConvert.DeserializeObject <JWTSettings>(GetJWTSecret(client));

            services.AddSingleton <DBSettings>(DBSecret);
            services.AddSingleton <JWTSettings>(JWTSecret);


            // configure jwt authentication

            var key = Encoding.ASCII.GetBytes(JWTSecret.JWTSecret);

            services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(x =>
            {
                x.RequireHttpsMetadata      = false;
                x.SaveToken                 = true;
                x.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(key),
                    ValidateIssuer           = false,
                    ValidateAudience         = false
                };
            });

            // configure DI for application services
            services.AddScoped <IJWTService, JWTService>();
            services.AddScoped <IDBService, DBService>();
        }
示例#19
0
        private void cleanupTestData()
        {
            Console.Write($"Delete all test parameters with prefix '{ParameterPrefix}'... ");
            using (var client = AWSOptions.CreateServiceClient <IAmazonSimpleSystemsManagement>())
            {
                GetParametersByPathResponse response;
                do
                {
                    response = client.GetParametersByPathAsync(new GetParametersByPathRequest
                    {
                        Path = ParameterPrefix
                    }).Result;

                    client.DeleteParametersAsync(new DeleteParametersRequest
                    {
                        Names = response.Parameters.Select(p => p.Name).ToList()
                    }).Wait();
                } while (!string.IsNullOrEmpty(response.NextToken));

                // no need to wait for eventual consistency here given we are not running tests back-to-back
            }
            Console.WriteLine("Done");
        }
示例#20
0
        public string CreateQueue(string QueueName, bool FIFOQueue = false, bool ContentBasedDeDuplication = false)
        {
            var attrs = new Dictionary <string, string>();

            if (FIFOQueue)
            {
                attrs.Add(QueueAttributeName.FifoQueue, "true");
            }

            if (ContentBasedDeDuplication)
            {
                attrs.Add(QueueAttributeName.ContentBasedDeduplication, "true");
            }

            Client = Options.CreateServiceClient <IAmazonSQS>();
            var sqsResponse = Client.CreateQueueAsync(new CreateQueueRequest
            {
                QueueName  = QueueName,
                Attributes = attrs
            }).Result;

            return(sqsResponse.QueueUrl);
        }
示例#21
0
        internal static async Task <int> SendToQ(AWSOptions options, string sqsQName, int numberOfMessage, bool confirm)
        {
            try
            {
                using (var sqs = options.CreateServiceClient <IAmazonSQS>())
                {
                    var qUrl = await GetQueueUrl(sqs, sqsQName);

                    if (!confirm)
                    {
                        confirm = Confirmation(numberOfMessage, qUrl);
                    }

                    Console.WriteLine($"Sending at {DateTime.UtcNow}:{DateTime.UtcNow.Millisecond}");
                    while (confirm && _messageCount < numberOfMessage)
                    {
                        var requestMsg = Math.Abs(numberOfMessage - _messageCount);

                        requestMsg = requestMsg > BATCH_SIZE ? BATCH_SIZE : requestMsg;

                        await SendBatchMessage(sqs, qUrl, requestMsg);

                        _messageCount += requestMsg;
                    }
                }
            }
            catch (AmazonSQSException ex)
            {
                ConsoleError(ex);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Caught Exception: " + ex.Message);
            }
            return(_messageCount);
        }
示例#22
0
 protected virtual IAmazonSQS CreateAmazonSqsClient(AWSOptions options)
 {
     return(options.CreateServiceClient <IAmazonSQS>());
 }
示例#23
0
 public S3ImagePersistor(IConfiguration config)
 {
     awsOptions = config.GetAWSOptions();
     bucketName = config.GetSection("S3")["BucketName"];
     client     = awsOptions.CreateServiceClient <IAmazonS3>();
 }