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.SystemTextJson.DefaultLambdaJsonSerializer))]
namespace BlueprintBaseName._1;
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 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 async Task FunctionHandler(S3Event evnt, ILambdaContext context)
{
var eventRecords = evnt.Records ?? new List();
foreach (var record in eventRecords)
{
var s3Event = record.S3;
if (s3Event == null)
{
continue;
}
try
{
var response = await this.S3Client.GetObjectMetadataAsync(s3Event.Bucket.Name, s3Event.Object.Key);
context.Logger.LogInformation(response.Headers.ContentType);
}
catch (Exception e)
{
context.Logger.LogError($"Error getting object {s3Event.Object.Key} from bucket {s3Event.Bucket.Name}. Make sure they exist and your bucket is in the same region as this function.");
context.Logger.LogError(e.Message);
context.Logger.LogError(e.StackTrace);
throw;
}
}
}
}