using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
using Amazon.Lambda.S3Events;
using Amazon.S3;
using Amazon.S3.Util;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
namespace S3EventFunction
{
public class Function
{
IAmazonS3 S3Client { get; set; }
///
/// Default constructor. This constructor is used by Lambda to construct the instance. When invoked in a Lambda environment
/// the AWS credentials will come from the IAM role associated with the function and the AWS region will be set to the
/// region the Lambda function is executed in.
///
public Function()
{
S3Client = new AmazonS3Client();
}
///
/// Constructs an instance with a preconfigured S3 client. This can be used for testing the outside of the Lambda environment.
///
///
public Function(IAmazonS3 s3Client)
{
this.S3Client = s3Client;
}
///
/// This method is called for every Lambda invocation. This method takes in an S3 event object and can be used
/// to respond to S3 notifications.
///
///
///
///
public void FunctionHandler(S3Event evnt, ILambdaContext context)
{
foreach (var record in evnt.Records)
{
context.Logger.LogLine($"Processing {record.S3.Object.Key}");
if (string.Equals("error.txt", record.S3.Object.Key))
{
throw new Exception("Simulated Failure");
}
}
}
}
}