namespace Amazon.Lambda.CloudWatchLogsEvents { using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Runtime.Serialization; using System.Text; /// /// AWS CloudWatch Logs event /// http://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/Subscriptions.html /// http://docs.aws.amazon.com/lambda/latest/dg/eventsources.html#eventsources-cloudwatch-logs /// public class CloudWatchLogsEvent { /// /// The Log from the CloudWatch that is invoking the Lambda function. /// public Log Awslogs { get; set; } /// /// The class identifies the Log from the CloudWatch that is invoking the Lambda function. /// [DataContract] public class Log { /// /// The data that are base64 encoded and gziped messages in LogStreams. /// [DataMember(Name = "data", IsRequired = false)] #if NETCOREAPP_3_1 [System.Text.Json.Serialization.JsonPropertyName("data")] #endif public string EncodedData { get; set; } /// /// Decodes the data stored in the EncodedData property. /// public string DecodeData() { if (string.IsNullOrEmpty(this.EncodedData)) return this.EncodedData; var bytes = Convert.FromBase64String(this.EncodedData); var uncompressedStream = new MemoryStream(); using (var stream = new GZipStream(new MemoryStream(bytes), CompressionMode.Decompress)) { stream.CopyTo(uncompressedStream); uncompressedStream.Position = 0; } var decodedString = Encoding.UTF8.GetString(uncompressedStream.ToArray()); return decodedString; } } } }