Beispiel #1
0
        public override async Task <object> ProcessMessageAsync(S3EventNotification message, ILambdaContext context)
        {
            LogInfo(JsonConvert.SerializeObject(message));

            // Use S3EventNotification to get location of the file which was uploaded
            var key        = message.Records[0].S3.Object.Key;
            var bucketName = message.Records[0].S3.Bucket.Name;
            // Read S3 object contents
            var objectContents = await _s3Client.GetObjectAsync(bucketName, key);

            // Separate messages by line ending
            String contents = null;

            using (var reader = new System.IO.StreamReader(objectContents.ResponseStream)){
                contents = reader.ReadToEnd();
            }
            // Use BatchInsertMessagesAsync from the Messages.Tables library to write messages to DynamoDB

            await _table.BatchInsertMessagesAsync(GetMessages(contents.Split('\n'), bucketName));

            return(null);
        }
        public override async Task <object> ProcessMessageAsync(S3EventNotification message, ILambdaContext context)
        {
            LogInfo(JsonConvert.SerializeObject(message));

            // Use S3EventNotification to get location of the file which was uploaded
            var bucket = message.Records[0].S3.Bucket.Name;
            var key    = message.Records[0].S3.Object.Key;
            // Read S3 object contents
            var request = new GetObjectRequest {
                BucketName = bucket,
                Key        = key
            };
            var text = "";

            using (GetObjectResponse response = await _s3Client.GetObjectAsync(request))
                using (Stream responseStream = response.ResponseStream)
                    using (StreamReader reader = new StreamReader(responseStream)){
                        text = await reader.ReadToEndAsync();
                    }

            // Separate messages by line ending
            var splitText = text.Split('\n');

            // Use BatchInsertMessagesAsync from the Messages.Tables library to write messages to DynamoDB
            var messages = new List <Message>();

            for (int i = 0; i < splitText.Length; i++)
            {
                LogInfo(splitText[i]);
                var msg = new Message();
                msg.Source = "S3";
                msg.Text   = splitText[i];
                messages.Add(msg);
            }
            await _table.BatchInsertMessagesAsync(messages);

            return(null);
        }