S3

There are two different S3-based constructors depending on whether your lambda function should use an Object Key Name filter. The S3 subscriber is preconfigured to be notified of both s3:ObjectCreated:* and s3:ObjectRemoved:* events.

Object Key Name Filtering

Object key name filtering only invokes a lambda function when objects with the given prefix are created.

To subscribe to object events created by objects with a given prefix, use the NewS3ScopedReactor constructor as in:

import (
  awsLambdaEvents "github.com/aws/aws-lambda-go/events"
  spartaArchetype "github.com/mweagle/Sparta/v3/archetype"
)
// KinesisStream reactor function
func reactorFunc(ctx context.Context,
  s3Event awsLambdaEvents.S3Event) (interface{}, error) {
  logger, _ := ctx.Value(sparta.ContextKeyRequestLogger).(*zerolog.Logger)

  logger.Info().
    Interface("Event", s3Event).
    Msg("S3 Event")

  return "Hello World 👋. Welcome to AWS Lambda! 🙌🎉🍾", nil
}

func main() {
  // ...
  handler := spartaArchetype.S3ReactorFunc(reactorFunc)
  lambdaFn, lambdaFnErr := spartaArchetype.NewS3ScopedReactor(handler,
    "S3_BUCKET_ARN_OR_CLOUDFORMATION_REF",
    "/my/special/prefix",
    nil)
}

All Events

To subscribe to all S3 bucket events, use the NewS3Reactor version:

func main() {
  // ...
  handler := spartaArchetype.S3ReactorFunc(reactorFunc)
  lambdaFn, lambdaFnErr := spartaArchetype.NewS3Reactor(handler,
    "/my/special/prefix",
    nil)
}