Go - Bucket.On()

Create a new bucket notification trigger when certain files are created or deleted.

import (
  "context"
  "fmt"

  "github.com/nitrictech/go-sdk/handler"
  "github.com/nitrictech/go-sdk/nitric"
)

func main() {
  assets := nitric.NewBucket("assets")

  readableAssets, err := nitric.NewBucket("assets").Allow(nitric.BucketRead)
  if err != nil {
    return
  }

  assets.On(handler.DeleteNotification, "*", func(ctx *handler.BlobEventContext, _ handler.BlobEventHandler) (*handler.BlobEventContext, error) {
    fmt.Printf("a file named %s was deleted\n", ctx.Request.Key())

    return ctx, nil
  })

  assets.On(handler.WriteNotification, "/images/cat", func(ctx *handler.BlobEventContext, _ handler.BlobEventHandler) (*handler.BlobEventContext, error) {
    fmt.Printf("a cat image was written")

    return ctx, nil
  })

  assets.On(handler.WriteNotification, "/images/dog", func(ctx *handler.BlobEventContext, _ handler.BlobEventHandler) (*handler.BlobEventContext, error) {
    dogImage, err := readableAssets.File(ctx.Request.Key()).Read(context.TODO())
    if err != nil {
      return ctx, err
    }

    fmt.Println(dogImage)

    return ctx, nil
  })

  if err := nitric.Run(); err != nil {
    fmt.Println(err)
  }
}

Parameters

  • Name
    notificationType
    Required
    Required
    Type
    WriteNotification or DeleteNotification
    Description

    The notification type for a triggered event, either on a file write or a file delete.

  • Name
    notificationPrefixFilter
    Required
    Required
    Type
    string
    Description

    The file prefix filter that must match for a triggered event. If multiple filters overlap across notifications, an error will be thrown when registering the resource.

  • Name
    middleware
    Required
    Required
    Type
    BlobEventMiddleware
    Description

    The middleware (code) to be triggered by the bucket notification being triggered.

Available trigger types

WriteNotification

Run when a file in the bucket is created using: File.Write()

DeleteNotification

Run when a file in the bucket is deleted using: File.Delete()

Trigger type cloud mapping

PermissionAWSGCPAzure
writes3:ObjectCreated:*OBJECT_FINALIZEMicrosoft.Storage.BlobCreated
deletes3:ObjectRemoved:*OBJECT_DELETEMicrosoft.Storage.BlobDeleted