Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GCImagingAWSLambdaS3
public async Task<string> FunctionHandler(S3Event evnt, ILambdaContext context)
{
var s3Event = evnt.Records?[0].S3;
if(s3Event == null)
{
return null;
}
try
{
var rs = await this.S3Client.GetObjectMetadataAsync(
s3Event.Bucket.Name,
s3Event.Object.Key);
if (rs.Headers.ContentType.StartsWith("image/"))
{
using (GetObjectResponse response = await S3Client.GetObjectAsync(
s3Event.Bucket.Name,
s3Event.Object.Key))
{
using (Stream responseStream = response.ResponseStream)
{
using (StreamReader reader = new StreamReader(responseStream))
{
using (var memstream = new MemoryStream())
{
var buffer = new byte[512];
var bytesRead = default(int);
while ((bytesRead = reader.BaseStream.Read(buffer, 0, buffer.Length)) > 0)
memstream.Write(buffer, 0, bytesRead);
// Perform image manipulation
// We are choosing to grayscale
var transformedImage = GcImagingOperations.GetGrayScale(memstream);
PutObjectRequest putRequest = new PutObjectRequest()
{
BucketName = DestBucket,
Key = $"grayscale-{s3Event.Object.Key}",
ContentType = rs.Headers.ContentType,
ContentBody = transformedImage
};
await S3Client.PutObjectAsync(putRequest);
}
}
}
}
}
return rs.Headers.ContentType;
}
catch (Exception e)
{
throw;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.