Skip to content

bb-file-bucket: run a handler when an object is created (onObjectCreated) #1297

Open

Description

@xyos

Which area does this feature relate to?

Building Blocks (existing), Local development / mocking

Describe the feature you'd like to request

FileBucket has no way to run code when a file lands in the bucket. Its runtime API (put, get, delete, deleteBatch, scan, getUrl, putUrl, getFileHandle, createUploadHandle, listVersions, restoreVersion) is request-driven only. Today, code in AWS Blocks can run on an RPC request (ApiNamespace), a queued job (AsyncJob) or a schedule (CronJob). An "object created" event is the missing trigger.

I'd like a handler on FileBucket that runs when an object is created under an optional prefix. It should behave the same way locally (mock) and on AWS.

Use case

Event-driven backends that aren't request/response: CSV/data imports, media processing (thumbnails, transcoding), document ingestion, and pipelines where another system drops files into a bucket.

The recommended way to upload large files is putUrl() or createUploadHandle(): the client sends the bytes straight to S3, and the backend never sees the request. Without a storage event, the backend can't find out a file arrived unless every client remembers to call a follow-up RPC. That misses uploads from CLIs, other services and interrupted browser sessions.

Proposed solution

Add a handler option to the constructor, matching how AsyncJob takes its handler:

const importedRows = new KVStore(scope, 'imported-rows');

const inbox = new FileBucket(scope, 'inbox', {           // versioned by default
	onObjectCreated: {
		prefix: 'incoming/',
		handler: async (event, ctx) => {                     // event: { path, size, versionId }
			// read the version that was notified, not whatever is latest now
			const file = await inbox.get(event.path, { versionId: event.versionId });
			if (!file) return;                                 // that version was permanently deleted
			const upload = `${event.path}@${event.versionId}`;
			for (const row of parseCsv(file.body)) {
				// per-upload output: a duplicate event rewrites identical keys,
				// and a late older upload can't overwrite a newer one's rows
				await importedRows.put(`${upload}:${row.id}`, row);
			}
		},
	},
});

export const api = new ApiNamespace(scope, 'api', (context) => ({
	async getUploadUrl(name: string) {
		await auth.requireAuth(context);
		return inbox.putUrl(`incoming/${name}`);           // client uploads directly; the handler still fires
	},
}));

Suggested behavior:

Concern Behavior
AWS wiring S3 ObjectCreated notification → SQS → Lambda, with retries and a dead-letter queue (reusing bb-async-job's SQS/DLQ approach)
Event payload { path, size, versionId }. versionId is required in the type for versioned buckets (the default) and absent when versioned: false, mirroring GetOptionsFor
Delivery semantics At least once and unordered, as with S3 notifications. Docs should require handlers to tolerate both duplicates and reordering
Latest-wins handlers Need an ordering signal: either expose S3's per-key sequencer, or document immutable per-upload outputs (as above) as the supported pattern
Filtering prefix (possibly suffix), mapped to S3 notification filters
Local mock Calls the handler asynchronously after put() and after uploads through putUrl/createUploadHandle complete, so npm run dev behaves the same as AWS
fromExisting() buckets Out of scope for now. S3 notification configuration is per-bucket, and managing it could clobber notifications owned elsewhere
Deletes A separate onObjectRemoved can come later; not part of this request

Alternatives considered

  • Submit an AsyncJob after each write, plus a "confirm upload" RPC for presigned uploads. Works locally and on AWS, but relies on every producer to cooperate, so direct uploads from other clients are missed.
  • Poll with CronJob + scan({ prefix }). scan only sees objects present at poll time, so files deleted or overwritten between polls are missed. It also adds latency and per-poll cost. Only acceptable for write-once, unique keys that are never deleted.
  • Raw CDK s3.Bucket + addEventNotification. Gives the real trigger but loses the FileBucket API and local mock. It can't be added to a FileBucket either, because the underlying bucket is private in the CDK layer, so customer code would need a cast.

Additional context

The change is purely additive: a new optional constructor option, with no change to existing methods or types.

Is this something you'd be interested in working on?

  • I may be able to implement this feature request
  • This feature might incur a breaking change

Activity

  1. added
    pending-triage
    pending-maintainer-response
    on Nov 4, 2026
  2. added 2 commits that reference this issue on Nov 6, 2026
    8d236de
    fd6f476
  3. added a commit that references this issue on Nov 25, 2026
    15ee63a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    pending-maintainer-responsepending-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions