public async Task TestS3EventLambdaFunction() { // Setup the S3 event object that S3 notifications would create with the // fields used by the Lambda function. var s3Event = new S3Event { Records = new List <S3EventNotificationRecord> { new S3EventNotificationRecord { AwsRegion = "eu-west-1", S3 = new S3Entity { Bucket = new S3BucketEntity { Name = BucketName }, Object = new S3ObjectEntity { Key = Key } } } } }; // Invoke the lambda function and confirm the content type was returned. var function = new Function(new OptimizerStub()); var result = await function.FunctionHandler(s3Event, null); var imageUrl = $"https://s3.eu-west-1.amazonaws.com/{BucketName}/{Key}"; var optimizedSlug = "optimized-images/jake-the-dog.png"; var expected = $"{imageUrl} <> {optimizedSlug}"; Assert.Equal(expected, result); }
public async Task <string> FunctionHandler(S3Event evnt, ILambdaContext context) { var s3Event = evnt.Records?[0].S3; if (s3Event == null) { return(null); } try { var response = await this.S3Client.GetObjectMetadataAsync(s3Event.Bucket.Name, s3Event.Object.Key); Console.WriteLine(s3Event.Bucket.Name.ToString()); Console.WriteLine(s3Event.Object.Key.ToString()); await ProcessTranscribe(s3Event.Bucket.Name, s3Event.Object.Key); return(response.Headers.ContentType); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); throw; } }
public BulkEventLambdaFunctionalTests() { _fakeS3Event = new S3Event() { Records = new List <S3EventNotification.S3EventNotificationRecord> { new S3EventNotification.S3EventNotificationRecord { S3 = new S3EventNotification.S3Entity { Bucket = new S3EventNotification.S3BucketEntity { Name = "fakeBucket" }, Object = new S3EventNotification.S3ObjectEntity { Key = "fakeKey" } } } } }; _fakeSns = A.Fake <AmazonSimpleNotificationServiceClient>(); _fakeS3 = A.Fake <AmazonS3Client>(); _s3GetObjectRequest = new GetObjectRequest() { BucketName = "fakeBucket", Key = "fakeKey" }; _snsTopic = "test-topic"; }
/// <summary> /// 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. /// </summary> /// <param name="evnt"></param> /// <param name="context"></param> /// <returns></returns> public async Task <string?> FunctionHandler(S3Event evnt, ILambdaContext context) { Console.WriteLine("Hello world"); Console.WriteLine(JsonSerializer.Serialize(evnt)); var s3Event = evnt.Records?[0].S3; if (s3Event == null) { return(null); } try { var response = await this.S3Client.GetObjectMetadataAsync(s3Event.Bucket.Name, s3Event.Object.Key); Console.WriteLine(JsonSerializer.Serialize(await GetLatestWilburWednesday())); var latestWilbur = await GetLatestWilburWednesday(); await SendEmail(new EmailItem(latestWilbur)); return(response.Headers.ContentType); } catch (Exception e) { context.Logger.LogInformation($"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.LogInformation(e.Message); context.Logger.LogInformation(e.StackTrace); throw; } }
/// <summary> /// 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. /// </summary> //public async Task<string> FunctionHandler(S3Event events, ILambdaContext context) public async Task FunctionHandler(S3Event events, ILambdaContext context) { foreach (var evnt in events.Records) { var s3Event = evnt.S3; if (s3Event == null) { return; } context.Logger.LogLine($"Received notification of {evnt.EventName} for s3://{s3Event.Bucket.Name}/{s3Event.Object.Key}"); // create some random temperature forecasts StringBuilder bld = new StringBuilder(); Random r = new Random(); for (int i = 0; i < 10; i++) { int hour = r.Next(0, 24); int tempInt = r.Next(0, 30); int tempDec = r.Next(0, 9); bld.AppendLine($"2020-09-28 {(hour < 10 ? "0" + hour.ToString() : hour.ToString())}:00:00,{tempInt}.{tempDec}"); } // change the file extension to .csv from .nc string newKey = s3Event.Object.Key.Replace(".nc", ".csv"); context.Logger.LogLine($"Going to write to file s3://bigwind-curated/{newKey} content: {bld}"); var response = await S3Client.PutObjectAsync(new Amazon.S3.Model.PutObjectRequest() { BucketName = "bigwind-curated", Key = newKey, ContentBody = bld.ToString(), ContentType = "text/plain" }); } return; }
public async Task FunctionHandler(S3Event s3Event, ILambdaContext context) { foreach (var record in s3Event.Records) { if (!_supportedImageTypes.Contains(Path.GetExtension(record.S3.Object.Key).ToLower())) { Console.WriteLine($"Object {record.S3.Bucket.Name}:{record.S3.Object.Key} is not a supported image type"); continue; } Console.WriteLine($"Determining whether image {record.S3.Bucket.Name}:{record.S3.Object.Key} has been compressed"); // Get the existing tag set var taggingResponse = await _s3Client.GetObjectTaggingAsync(new GetObjectTaggingRequest { BucketName = record.S3.Bucket.Name, Key = record.S3.Object.Key }); if (taggingResponse.Tagging.Any(tag => tag.Key == "Compressed" && tag.Value == "true")) { Console.WriteLine($"Image {record.S3.Bucket.Name}:{record.S3.Object.Key} has already been compressed"); continue; } // Get the existing image using (var objectResponse = await _s3Client.GetObjectAsync(record.S3.Bucket.Name, record.S3.Object.Key)) using (Stream responseStream = objectResponse.ResponseStream) { Console.WriteLine($"Compressing image {record.S3.Bucket.Name}:{record.S3.Object.Key}"); // Use TinyPNG to compress the image TinyPngClient tinyPngClient = new TinyPngClient(Environment.GetEnvironmentVariable("TinyPNG_API_Key")); var compressResponse = await tinyPngClient.Compress(responseStream); var downloadResponse = await tinyPngClient.Download(compressResponse); // Upload the compressed image back to S3 using (var compressedStream = await downloadResponse.GetImageStreamData()) { Console.WriteLine($"Uploading compressed image {record.S3.Bucket.Name}:{record.S3.Object.Key}"); await _s3Client.PutObjectAsync(new PutObjectRequest { BucketName = record.S3.Bucket.Name, Key = record.S3.Object.Key, InputStream = compressedStream, TagSet = new List <Tag> { new Tag { Key = "Compressed", Value = "true" } } }); } } } }
/// <summary> /// 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. /// </summary> /// <param name="evnt"></param> /// <param name="context"></param> /// <returns></returns> public string FunctionHandler(S3Event evnt, ILambdaContext context) { var s3Event = evnt.Records?[0].S3; if (s3Event == null) { return("Empty event"); } try { bktKey = s3Event.Object.Key; context.Logger.LogLine($"Getting object {s3Event.Object.Key} from bucket: {s3Event.Bucket.Name}."); ProcessObjectDataAsync(context).Wait(); context.Logger.LogLine($"Returned from data processing"); //GetObjectMetadataRequest mdReq = new GetObjectMetadataRequest //{ // BucketName = s3Event.Bucket.Name, // Key = s3Event.Object.Key, //}; //var response = await this.S3Client.GetObjectMetadataAsync(mdReq); return("Success"); } catch (Exception e) { context.Logger.LogLine($"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.LogLine(e.Message); context.Logger.LogLine(e.StackTrace); return("Exception"); } }
/// <summary> /// 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. /// </summary> /// <param name="input"></param> /// <param name="context"></param> /// <returns></returns> public async Task Handler(S3Event input, ILambdaContext context) { var key = input.Records?[0].S3.Object.Key; var sourceKey = WebUtility.UrlDecode(key); var outputKey = sourceKey.Split('.')[0]; context.Logger.LogLine($"key: {key}, sourceKey: {sourceKey}, outputKey: {outputKey}"); var job = new CreateJobRequest { PipelineId = "1505766337361-sv0ahs", OutputKeyPrefix = $"{outputKey}/", Input = new JobInput { Key = sourceKey }, Outputs = new List <CreateJobOutput>(new[] { new CreateJobOutput { Key = $"{outputKey}-1080p.mp4", PresetId = "1351620000001-000001" }, new CreateJobOutput { Key = $"{outputKey}-720p.mp4", PresetId = "1351620000001-000010" }, new CreateJobOutput { Key = $"{outputKey}-web-720p.mp4", PresetId = "1351620000001-100070" } }) }; await _elasticTranscoder.CreateJobAsync(job); }
/// <summary> /// 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. /// </summary> /// <param name="evnt"></param> /// <param name="context"></param> /// <returns></returns> public async Task ExecuteAsync(S3Event evnt, ILambdaContext context) { try { var s3Event = evnt.Records?[0].S3; var getResponse = await GetS3Object(s3Event.Bucket.Name, s3Event.Object.Key); using (var responseStream = getResponse.ResponseStream) { using (var resizedStream = GetResizedStream(responseStream, 0.2m, getResponse.Headers.ContentType)) { resizedStream.Seek(0, SeekOrigin.Begin); await WriteS3Object( System.Environment.GetEnvironmentVariable("ThumbnailBucketName", EnvironmentVariableTarget.Process), $"thumb_{s3Event.Object.Key}", resizedStream); } } context.Logger.LogLine("Operation Complete"); } catch (Exception ex) { context.Logger.LogLine(ex.Message); } }
/// <summary> /// 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. /// </summary> /// <param name="evnt"></param> /// <param name="context"></param> /// <returns></returns> public async Task <string> FunctionHandler(S3Event evnt, ILambdaContext context) { context.Logger.LogLine("Geppetto.StorageEvents.Function Invoked"); var s3Event = evnt.Records?[0].S3; if (s3Event == null) { return(null); } try { var gridEvents = GetEvents(evnt); if (gridEvents != null && gridEvents.Count > 0) { await PublishEvents(gridEvents); } return("success"); } catch (Exception e) { context.Logger.LogLine($"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.LogLine(e.Message); context.Logger.LogLine(e.StackTrace); throw; } }
private IList <EventGridEvent> GetEvents(S3Event evnt) { var events = new List <EventGridEvent>(); foreach (var eventNotification in evnt.Records) { var s3Event = eventNotification.S3; var file = new AzReplicator.AWS.Models.FileEvent() { Name = s3Event.Object.Key, Url = GeneratePreSignedUrl(s3Event.Bucket.Name, s3Event.Object.Key, this.S3Client) }; events.Add(new EventGridEvent() { Id = Guid.NewGuid().ToString(), Data = file, EventTime = DateTime.UtcNow, EventType = "Geppetto.NewFile", Subject = s3Event.Object.Key, DataVersion = "1.0" }); } return(events); }
/// <summary> /// 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. /// </summary> /// <param name="evnt"></param> /// <param name="context"></param> /// <returns></returns> public async Task <SendMessageResponse> FunctionHandler(S3Event evnt, ILambdaContext context) { var s3Event = evnt.Records?[0].S3; if (s3Event == null) { return(null); } try { SendMessageResponse result; using (var response = await S3Client.GetObjectAsync(s3Event.Bucket.Name, s3Event.Object.Key)) using (Stream responseStream = response.ResponseStream) using (var reader = new StreamReader(responseStream)) { var message = await reader.ReadToEndAsync(); result = await SqsClient.SendMessageAsync(_queueUrl, $"New Task Created:\n{message}"); } return(result); } catch (Exception e) { context.Logger.LogLine($"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.LogLine(e.Message); context.Logger.LogLine(e.StackTrace); throw; } }
/// <summary> /// Needed initialization before running the main logic of the function /// </summary> /// <param name="evnt"></param> /// <param name="context"></param> private void Initialization(S3Event evnt, ILambdaContext context) { try { var s3Event = evnt.Records?[0].S3; if (s3Event == null) { context.Logger.LogLine("No events detected from S3. Lambda function returns!"); } zipFileName = s3Event.Object.Key; context.Logger.LogLine("Zip file " + zipFileName + " detected!"); zipFileNameWithoutExtension = zipFileName.Substring(0, zipFileName.Length - 4); srcBucketName = s3Event.Bucket.Name; extractedZipFilePath = dstBucketName + "/" + zipFileNameWithoutExtension; tmpExtractPath = "/tmp/" + zipFileNameWithoutExtension; tmpFileName = System.IO.Path.GetTempFileName(); context.Logger.LogLine("Temp file name: " + tmpFileName); } catch (Exception e) { context.Logger.LogLine("Error in Initialization: " + e.Message); } }
/// <summary> /// This method is called when associated S3 bucket has new file and trigger AWS Glue to transform it. /// </summary> public async Task <string> FunctionHandler(S3Event evnt, ILambdaContext context) { context.Logger.LogLine("Started!"); var s3Event = evnt.Records?[0].S3; if (s3Event == null) { context.Logger.LogLine("Event not found!"); return("Event not found!"); } try { context.Logger.LogLine("Starting job run..."); await GlueClient.StartJobRunAsync(new StartJobRunRequest() { AllocatedCapacity = 2, JobName = Environment.GetEnvironmentVariable("GLUE_JOB_NAME") }); context.Logger.LogLine("Done"); return("Done"); } catch (Exception e) { context.Logger.LogLine(e.Message); context.Logger.LogLine(e.StackTrace); throw; } }
/// <summary> /// 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. /// </summary> /// <param name="evnt"></param> /// <param name="context"></param> /// <returns></returns> public async Task <string> ObjectCreatedHandler(S3Event evnt, ILambdaContext context) { var s3Event = evnt.Records?[0].S3; if (s3Event == null) { return(null); } try { var record = evnt.Records?.FirstOrDefault(); var s3ObjectName = record?.S3?.Object?.Key; s3ObjectName = s3ObjectName ?? string.Empty; context.Logger.LogLine(string.Format("Lambda funtion {0} - A new object {1} has been created in bucket named {2} ", context.FunctionName, s3ObjectName, s3Event.Bucket.Name)); var response = await this.S3Client.GetObjectMetadataAsync(s3Event.Bucket.Name, s3Event.Object.Key); return(response.Headers.ContentType); } catch (Exception e) { context.Logger.LogLine($"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.LogLine(e.Message); context.Logger.LogLine(e.StackTrace); throw; } }
public async Task FunctionHandler(S3Event @event, ILambdaContext context) { context.Logger.Log("START"); context.Logger.Log("S3Event: " + JsonConvert.SerializeObject(@event)); foreach (var record in @event.Records) { context.Logger.Log($"Processing file {record.S3.Bucket.Name}:{record.S3.Object.Key}."); var metadata = await _s3client.GetObjectMetadataAsync(record.S3.Bucket.Name, record.S3.Object.Key); context.Logger.Log("Metadata: " + JsonConvert.SerializeObject(metadata)); if (metadata.Metadata.Keys.Contains(metadataKey)) { var value = Convert.ToInt32(metadata.Metadata[metadataKey]); context.Logger.Log($"{metadataKey} = {value}"); await UpdateDatabase(value, record.S3.Object.Key); } } context.Logger.Log("END"); }
/// <summary> /// 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. /// </summary> /// <param name="evnt"></param> /// <param name="context"></param> /// <returns></returns> public async Task <string> FunctionHandler(S3Event evnt, ILambdaContext context) { var s3Event = evnt.Records?[0].S3; if (s3Event == null) { return(null); } try { var response = await S3Client.GetObjectMetadataAsync(s3Event.Bucket.Name, s3Event.Object.Key); string message = $"{s3Event.Object.Key} - {s3Event.Object.Size} Bytes"; context.Logger.LogLine(message); PublishRequest request = new PublishRequest { Message = message, PhoneNumber = "+34642375554" }; var smsResponse = await SnsClient.PublishAsync(request); context.Logger.LogLine($"Response from SNS: {smsResponse.HttpStatusCode}"); return(response.Headers.ContentType); } catch (Exception e) { context.Logger.LogLine($"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.LogLine(e.Message); context.Logger.LogLine(e.StackTrace); throw; } }
/// <summary> /// 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. /// </summary> /// <param name="evnt"></param> /// <param name="context"></param> /// <returns></returns> public async Task <string> S3EventHandler(S3Event evnt, ILambdaContext context) { var s3Event = evnt.Records?[0].S3; if (s3Event == null) { return(null); } try { string decodedKey = HttpUtility.UrlDecode(s3Event.Object.Key); context.Logger.LogLine($"Reading \"{decodedKey}\"..."); Stream stream = await S3Client.GetObjectStreamAsync(s3Event.Bucket.Name, decodedKey, null); string applicantJSON = ParseDataFromS3(stream, decodedKey); context.Logger.LogLine($"\"{decodedKey}\" read\n{applicantJSON}"); context.Logger.LogLine($"Putting data in DynamoDb Table..."); await PutDataInDynamoDB(applicantJSON); context.Logger.LogLine($"Data put successfully! Check the DynamoDB table for more details."); return("Success!"); } catch (Exception e) { context.Logger.LogLine(e.Message); context.Logger.LogLine(e.StackTrace); throw; } }
/// <summary> /// 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. /// </summary> /// <param name="evnt"></param> /// <param name="context"></param> /// <returns></returns> public async Task <string> FunctionHandler(S3Event evnt, ILambdaContext context) { var s3Event = evnt.Records?[0].S3; if (s3Event == null) { return(null); } try { var response = await this.S3Client.GetObjectMetadataAsync(s3Event.Bucket.Name, s3Event.Object.Key); context.Logger.LogLine($"S3 Object Key: {s3Event.Object.Key}"); context.Logger.LogLine(JsonSerializer.Serialize(response)); return(response.Headers.ContentType); } catch (Exception e) { context.Logger.LogLine($"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.LogLine(e.Message); context.Logger.LogLine(e.StackTrace); throw; } }
/// <summary> /// A simple function that takes a string and does a ToUpper /// </summary> /// <param name="input"></param> /// <param name="context"></param> /// <returns></returns> public void FunctionHandler(S3Event input, ILambdaContext context) { foreach (var record in input.Records) { context.Logger.Log($"Bucket: {record.S3.Bucket.Name}, Key: {record.S3.Object.Key}"); } }
public async Task FunctionHandler(S3Event s3Event, ILambdaContext context) { var srcBucket = s3Event.Records[0].S3.Bucket; var srcObject = s3Event.Records[0].S3.Object; using (var s3Service = new AmazonS3Client()) { var incoming = await s3Service.GetObjectAsync(srcBucket.Name, srcObject.Key); using (var streamReader = new StreamReader(incoming.ResponseStream)) { var content = streamReader.ReadToEnd(); var payload = new string(content.Reverse().ToArray()); var putRequest = new PutObjectRequest { BucketName = "vinlewt-tx-output", Key = srcObject.Key, ContentBody = payload }; await s3Service.PutObjectAsync(putRequest); } } }
public async Task RunAsync(S3Event s3Event, ILambdaContext context) { _logger.LogInformation("Copy Build Triggered: "); _logger.LogInformation(s3Event.ToJsonString(true)); //used by third party zip library Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); if (s3Event == null || s3Event.Records == null || s3Event.Records.Count <= 0) { return; } foreach (var record in s3Event.Records.Where(r => r.S3.Object.Key.ToUpper().Contains("BUILD"))) { var buildBucket = Environment.GetEnvironmentVariable("BUILDBUCKET"); _logger.LogInformation("Build bucket: " + buildBucket); var sourceKey = record.S3.Object.Key; _logger.LogInformation($"Source Key: {sourceKey}"); var solutionName = sourceKey.Substring(0, sourceKey.IndexOf("/")); _logger.LogInformation($"Solution: {solutionName}"); await BuildProcessorFactory.GetBuildProcessor().ProcessBuildAsync(solutionName, sourceKey, record.S3.Bucket.Name, buildBucket); } }
/// <summary> /// 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. /// </summary> /// <param name="evnt"></param> /// <param name="context"></param> /// <returns></returns> public async Task <string> FunctionHandler(S3Event ev, ILambdaContext context) { try { IList <FileAddedBusMessage> messages = PrepareMessages(ev); if (messages.Count > 0) { await _busSender.SendBatch(messages, context.Logger); context.Logger.LogLine($"Sent {messages.Count} messages to bus"); return($"Num queued messages: {messages.Count}"); } else { return("No S3 messages to queue up"); } } catch (Exception e) { context.Logger.LogLine("Problem queing up bus messages!"); context.Logger.LogLine(e.Message); context.Logger.LogLine(e.StackTrace); throw; } }
/// <summary> /// 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. /// </summary> /// <param name="evnt"></param> /// <param name="context"></param> /// <returns></returns> public async Task GTCToPlinkS3LambdaFunctionHandler(S3Event evnt, ILambdaContext context) { JObject preference = new Preference().preference; var s3Event = evnt.Records?[0].S3; if (s3Event == null) { return; } try { var result = GTCToPlinkS3.ConvertGTCToPlinkS3(bucketNameGTC: s3Event.Bucket.Name, bucketNameBPM: preference["S3"]["BPM"]["Name"].ToString(), bucketNamePlink: preference["S3"]["Plink"]["Name"].ToString(), bucketRegionGTC: preference["S3"]["GTC"]["Name"].ToString(), bucketRegionBPM: preference["S3"]["BPM"]["Region"].ToString(), bucketRegionPlink: preference["S3"]["Plink"]["Region"].ToString(), keyNameGTCs: new List <string> { s3Event.Object.Key }, keyNamePlinkBasedir: preference["PlinkBase"].ToString(), tableRegion: preference["Dynamo"]["Progress"]["Region"].ToString(), tableName: preference["Dynamo"]["Progress"]["Name"].ToString(), credential: @"embeded", s3client: (AmazonS3Client)this.S3Client); } catch (Exception e) { context.Logger.LogLine(e.Message); context.Logger.LogLine(e.StackTrace); throw; } }
public async Task <string> ConvertToJson(S3Event s3event, ILambdaContext context) { var logger = context.Logger; var s3Record = s3event.Records[0].S3; logger.LogLine(String.Format("bucket {0}, key {1}", s3Record.Bucket.Name, s3Record.Object.Key)); var request = new GetObjectRequest() { BucketName = s3Record.Bucket.Name, Key = s3Record.Object.Key, ServerSideEncryptionCustomerMethod = ServerSideEncryptionCustomerMethod.None }; var response = await s3client.GetObjectAsync(request); var ddexAsJson = ParseResponse(response, logger); logger.LogLine(ddexAsJson); var putResponse = await s3client.PutObjectAsync(new PutObjectRequest( ) { BucketName = "ddex-samples-as-json", Key = s3Record.Object.Key + ".json", ContentBody = ddexAsJson }); return(putResponse.HttpStatusCode.ToString()); }
public async Task <string> FunctionHandler(S3Event evt, ILambdaContext context) { var s3Event = evt.Records?[0].S3; var region = evt.Records?[0].AwsRegion; if (s3Event == null) { return(null); } var imageUrl = $"https://s3.{region}.amazonaws.com/{s3Event.Bucket.Name}/{s3Event.Object.Key}"; try { return(Optimizer.Optimize(imageUrl, CreateOutputUrl(s3Event.Object.Key))); } catch (Exception e) { context.Logger.LogLine($"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.LogLine(e.Message); context.Logger.LogLine(e.StackTrace); throw; } }
private static S3Event CreateS3Event() { var s3Event = new S3Event { Records = new List <S3EventNotification.S3EventNotificationRecord> { new S3EventNotification.S3EventNotificationRecord { S3 = new S3EventNotification.S3Entity { Bucket = new S3EventNotification.S3BucketEntity { Name = "TestBucketName" }, Object = new S3EventNotification.S3ObjectEntity { Key = "TestKey" } } } } }; return(s3Event); }
public async Task TestS3EventLambdaFunction() { var s3Event = new S3Event { Records = new List <S3EventNotification.S3EventNotificationRecord> { new S3EventNotification.S3EventNotificationRecord { S3 = new S3EventNotification.S3Entity { Bucket = new S3EventNotification.S3BucketEntity { Name = "videos-2-process" }, Object = new S3EventNotification.S3ObjectEntity { Key = "unsafe-video.mp4" } } } } }; Function function = new Function(); // Invoke the lambda function and confirm the content type was returned. await function.FunctionHandler(s3Event, null).ConfigureAwait(false); }
/// <summary> /// 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. /// </summary> /// <param name="evnt"></param> /// <param name="context"></param> /// <returns></returns> public async Task <ResponeModel> FunctionHandler(S3Event evnt, ILambdaContext context) { var s3Event = evnt.Records?[0].S3; if (s3Event == null) { return(null); } try { var response = await this.S3Client.GetObjectMetadataAsync(s3Event.Bucket.Name, s3Event.Object.Key); var result = new ResponeModel { BucketName = s3Event.Bucket.Name, Key = s3Event.Object.Key, ContentType = response.Headers.ContentType, Size = s3Event.Object.Size, EventSource = evnt.Records?[0].EventSource }; context.Logger.LogLine($"EventSource {evnt.Records?[0].EventSource} from bucket {s3Event.Bucket.Name} Key {s3Event.Object.Key} ContentType {response.Headers.ContentType}."); return(result); } catch (Exception e) { context.Logger.LogLine($"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.LogLine(e.Message); context.Logger.LogLine(e.StackTrace); throw; } }
/// <summary> /// 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. /// </summary> /// <param name="evnt"></param> /// <param name="context"></param> /// <returns></returns> public async Task <string> FunctionHandler(S3Event evnt, ILambdaContext context) { var s3Event = evnt.Records?[0].S3; string thumnailResultBucket = "thumbnail-result-s3-bucket"; if (s3Event == null) { return(null); } try { var thumbnailStream = await GenerateThumbnail(s3Event.Bucket.Name, s3Event.Object.Key, context); var thumbnailKey = await UploadThumbnail(thumnailResultBucket, s3Event.Object.Key, thumbnailStream); string res = $"Thumbnail saved to s3://{thumnailResultBucket}/{thumbnailKey}"; context.Logger.LogLine(res); return(res); } catch (Exception e) { context.Logger.LogLine($"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.LogLine(e.Message); context.Logger.LogLine(e.StackTrace); throw; } }