private async Task <string> CreateStream()
        {
            await _client.CreateStreamAsync(new CreateStreamRequest()
            {
                StreamName = _streamName,
                ShardCount = _defaultShardCount
            });

            var i = 0;

            while (i < 20)
            {
                i++;
                await Task.Delay(3000);

                var poll = await _client.DescribeStreamSummaryAsync(new DescribeStreamSummaryRequest()
                {
                    StreamName = _streamName
                });

                if (poll.StreamDescriptionSummary.StreamStatus == StreamStatus.ACTIVE)
                {
                    return(poll.StreamDescriptionSummary.StreamARN);
                }
            }

            throw new TimeoutException();
        }
Пример #2
0
        private async Task WriteToStream()
        {
            const string myStreamName = "myTestStream";
            const int    myStreamSize = 1;

            try
            {
                var createStreamRequest = new CreateStreamRequest();
                createStreamRequest.StreamName = myStreamName;
                createStreamRequest.ShardCount = myStreamSize;
                var createStreamReq = createStreamRequest;

                var existingStreams = await kinesisClient.ListStreamsAsync();

                if (!existingStreams.StreamNames.Contains(myStreamName))
                {
                    var CreateStreamResponse = await kinesisClient.CreateStreamAsync(createStreamReq);

                    Console.WriteLine("Created Stream : " + myStreamName);
                }
            }
            catch (ResourceInUseException)
            {
                Console.Error.WriteLine("Producer is quitting without creating stream " + myStreamName +
                                        " to put records into as a stream of the same name already exists.");
                Environment.Exit(1);
            }

            await WaitForStreamToBecomeAvailableAsync(myStreamName);

            Console.Error.WriteLine("Putting records in stream : " + myStreamName);
            // Write 10 UTF-8 encoded records to the stream.
            for (int j = 0; j < 10; ++j)
            {
                byte[] dataAsBytes = Encoding.UTF8.GetBytes("testdata-" + j);
                using (MemoryStream memoryStream = new MemoryStream(dataAsBytes))
                {
                    try
                    {
                        PutRecordRequest requestRecord = new PutRecordRequest();
                        requestRecord.StreamName   = myStreamName;
                        requestRecord.PartitionKey = "url-response-times";
                        requestRecord.Data         = memoryStream;

                        PutRecordResponse responseRecord =
                            await kinesisClient.PutRecordAsync(requestRecord);

                        Console.WriteLine("Successfully sent record to Kinesis. Sequence number: {0}",
                                          responseRecord.SequenceNumber);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Failed to send record to Kinesis. Exception: {0}", ex.Message);
                    }
                }
            }

            Console.ReadLine();
        }
Пример #3
0
        /// <summary>
        /// This method verifies your credentials, creates a Kinesis stream, waits for the stream
        /// to become active, then puts 10 records in it, and (optionally) deletes the stream.
        /// </summary>
        public async static Task Main(string[] args)
        {
            const string myStreamName = "myTestStream";
            const int    myStreamSize = 1;

            try
            {
                var createStreamRequest = new CreateStreamRequest();
                createStreamRequest.StreamName = myStreamName;
                createStreamRequest.ShardCount = myStreamSize;
                var createStreamReq = createStreamRequest;
                await kinesisClient.CreateStreamAsync(createStreamReq);

                Console.Error.WriteLine("Created Stream : " + myStreamName);
            }
            catch (ResourceInUseException)
            {
                Console.Error.WriteLine("Producer is quitting without creating stream " + myStreamName +
                                        " to put records into as a stream of the same name already exists.");
                Environment.Exit(1);
            }

            await WaitForStreamToBecomeAvailable(myStreamName);

            Console.Error.WriteLine("Putting records in stream : " + myStreamName);
            // Write 10 UTF-8 encoded records to the stream.
            for (int j = 0; j < 10; ++j)
            {
                PutRecordRequest requestRecord = new PutRecordRequest();
                requestRecord.StreamName   = myStreamName;
                requestRecord.Data         = new MemoryStream(Encoding.UTF8.GetBytes("testData-" + j));
                requestRecord.PartitionKey = "partitionKey-" + j;
                var putResult = await kinesisClient.PutRecordAsync(requestRecord);

                Console.Error.WriteLine(
                    String.Format("Successfully putrecord {0}:\n\t partition key = {1,15}, shard ID = {2}",
                                  j, requestRecord.PartitionKey, putResult.ShardId));
            }

            // Uncomment the following if you wish to delete the stream here.
            //Console.Error.WriteLine("Deleting stream : " + myStreamName);
            //DeleteStreamRequest deleteStreamReq = new DeleteStreamRequest();
            //deleteStreamReq.StreamName = myStreamName;
            //try
            //{
            //    kinesisClient.DeleteStream(deleteStreamReq);
            //    Console.Error.WriteLine("Stream is now being deleted : " + myStreamName);
            //}
            //catch (ResourceNotFoundException ex)
            //
            //    Console.Error.WriteLine("Stream could not be found; " + ex);
            //}
            //catch (AmazonClientException ex)
            //{
            //    Console.Error.WriteLine("Error deleting stream; " + ex);
            //}
        }
Пример #4
0
        /// <summary>
        /// Kinesis Stream からイベントを受け取り、Kinesis Streamへデータを書き込む関数です
        /// ローカル開発環境として Kinesalite を使用しています
        /// </summary>
        /// <param name="kinesisEvent"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task FunctionHandler(KinesisEvent kinesisEvent, ILambdaContext context)
        {
            context.Logger.LogLine($"Beginning to process {kinesisEvent.Records.Count} records...");

            // Kinesis Eventからレコードを取得し、ループ処理を行う
            foreach (var record in kinesisEvent.Records)
            {
                context.Logger.LogLine($"Event ID: {record.EventId}");
                context.Logger.LogLine($"Event Name: {record.EventName}");

                var recordData = GetRecordContents(record.Kinesis);
                context.Logger.LogLine($"Record Data:");
                context.Logger.LogLine(recordData);
            }

            #region ローカル開発用のコード
            // Kinesis Stream の作成, 通常は AWSコンソール より作成する
            var request = new CreateStreamRequest
            {
                ShardCount = 1,
                StreamName = _streamName
            };

            var client = new AmazonKinesisClient(new AmazonKinesisConfig
            {
                ServiceURL = _serviceURL
            });

            var response = await client.ListStreamsAsync();

            if (!response.StreamNames.Any(_ => _ == _streamName))
            {
                await client.CreateStreamAsync(request);
            }
            #endregion

            // Kinesis Stream に対してデータを書き込む
            foreach (var i in Enumerable.Range(1, 10))
            {
                using (var memory = new MemoryStream(Encoding.UTF8.GetBytes($"Put Data:{i}")))
                {
                    try
                    {
                        var req = new PutRecordRequest
                        {
                            StreamName   = _streamName,
                            PartitionKey = "url-response-times",
                            Data         = memory
                        };

                        var res = await client.PutRecordAsync(req);

                        context.Logger.LogLine($"Successfully sent record to Kinesis. Sequence number: {res.SequenceNumber}.");
                    }
                    catch (Exception ex)
                    {
                        context.Logger.LogLine($"Failed to send record to Kinesis. Exception: {ex.Message}.");
                    }
                }
                context.Logger.LogLine("Stream processing complete.");
            }
        }
Пример #5
0
        public async Task TestCreate()
        {
            // ARRANGE
            AWSConfigs.AWSProfilesLocation = $"{Environment.GetEnvironmentVariable("UserProfile")}\\.aws\\credentials";

            string StreamName         = "test-stream";
            string PresignedUrlBucket = "pre-sign-url-bucket";
            string AccountNumber      = "123456789012";
            string Region             = "us-east-1";

            IAmazonS3 S3Client = new AmazonS3Client();

            GetPreSignedUrlRequest Req = new GetPreSignedUrlRequest()
            {
                BucketName = PresignedUrlBucket,
                Key        = "result.txt",
                Expires    = DateTime.Now.AddMinutes(2),
                Protocol   = Protocol.HTTPS,
                Verb       = HttpVerb.PUT
            };

            string PreSignedUrl = S3Client.GetPreSignedURL(Req);

            string Json = $@"
{{
""requestType"":""create"",
""responseUrl"":""{PreSignedUrl}"",
""stackId"":""arn:aws:cloudformation:{Region}:{AccountNumber}:stack/stack-name/{Guid.NewGuid().ToString()}"",
""requestId"":""12345678"",
""resourceType"":""Custom::KinesisStreamAwaiter"",
""logicalResourceId"":""KinesisStreamAwaiter"",
""resourceProperties"":{{
""StreamName"":""{StreamName}""
}}
}}";


            CustomResourceRequest Request = JsonConvert.DeserializeObject <CustomResourceRequest>(Json);

            TestLambdaLogger  TestLogger    = new TestLambdaLogger();
            TestClientContext ClientContext = new TestClientContext();

            TestLambdaContext Context = new TestLambdaContext()
            {
                FunctionName    = "KinesisStreamAwaiter",
                FunctionVersion = "1",
                Logger          = TestLogger,
                ClientContext   = ClientContext,
                LogGroupName    = "aws/lambda/KinesisStreamAwaiter",
                LogStreamName   = Guid.NewGuid().ToString(),
                RemainingTime   = TimeSpan.FromSeconds(300)
            };


            Entrypoint Entrypoint = new Entrypoint();

            // ACT
            IAmazonKinesis      KinesisClient = new AmazonKinesisClient();
            CreateStreamRequest CreateReq     = new CreateStreamRequest()
            {
                ShardCount = 1,
                StreamName = StreamName
            };


            CreateStreamResponse CreateResponse = await KinesisClient.CreateStreamAsync(CreateReq);

            try
            {
                CustomResourceResult Response = await Entrypoint.ExecuteAsync(Request, Context);

                // ASSERT

                Assert.True(Response.IsSuccess);
            }
            finally
            {
                DeleteStreamRequest DeleteReq = new DeleteStreamRequest()
                {
                    StreamName = StreamName
                };

                await KinesisClient.DeleteStreamAsync(DeleteReq);
            }
        }
        /// <summary>
        /// This method verifies your credentials, creates a Kinesis stream, waits for the stream
        /// to become active, then puts 10 records in it, and (optionally) deletes the stream.
        /// </summary>
        public static void Main(string[] args)
        {
            const string myStreamName = "kclnetsample";
            const int    myStreamSize = 1;

            try
            {
                var createStreamRequest = new CreateStreamRequest();
                createStreamRequest.StreamName = myStreamName;
                createStreamRequest.ShardCount = myStreamSize;
                var createStreamReq      = createStreamRequest;
                var CreateStreamResponse = kinesisClient.CreateStreamAsync(createStreamReq).Result;
                Console.Error.WriteLine("Created Stream : " + myStreamName);
            }
            catch (AggregateException ae)
            {
                ae.Handle((x) =>
                {
                    if (x is ResourceInUseException)
                    {
                        Console.Error.WriteLine("Producer is not creating stream " + myStreamName +
                                                " to put records into as a stream of the same name already exists.");
                        return(true);
                    }
                    return(false); // Let anything else stop the application.
                });
            }

            WaitForStreamToBecomeAvailable(myStreamName);

            Console.Error.WriteLine("Putting records in stream : " + myStreamName);
            // Write 10 UTF-8 encoded records to the stream.
            for (int j = 0; j < 10; ++j)
            {
                PutRecordRequest requestRecord = new PutRecordRequest();
                requestRecord.StreamName   = myStreamName;
                requestRecord.Data         = new MemoryStream(Encoding.UTF8.GetBytes("testData-" + j));
                requestRecord.PartitionKey = "partitionKey-" + j;
                var putResultResponse = kinesisClient.PutRecordAsync(requestRecord).Result;
                Console.Error.WriteLine(
                    String.Format("Successfully putrecord {0}:\n\t partition key = {1,15}, shard ID = {2}",
                                  j, requestRecord.PartitionKey, putResultResponse.ShardId));
            }

            // Uncomment the following if you wish to delete the stream here.
            //Console.Error.WriteLine("Deleting stream : " + myStreamName);
            //DeleteStreamRequest deleteStreamReq = new DeleteStreamRequest();
            //deleteStreamReq.StreamName = myStreamName;
            //try
            //{
            //    kinesisClient.DeleteStream(deleteStreamReq);
            //    Console.Error.WriteLine("Stream is now being deleted : " + myStreamName);
            //}
            //catch (ResourceNotFoundException ex)
            //
            //    Console.Error.WriteLine("Stream could not be found; " + ex);
            //}
            //catch (AmazonClientException ex)
            //{
            //    Console.Error.WriteLine("Error deleting stream; " + ex);
            //}
        }