# Storage event functions

Source: https://cubibase.com/docs/functions/storage-events

Also available: [Markdown version](https://cubibase.com/docs/functions/storage-events.md) · [llms.txt](https://cubibase.com/llms.txt) · [full site content](https://cubibase.com/llms-full.txt)

Run a CubiBase Function when an object is created, updated or deleted in a bucket you choose. Events come from the existing Storage event stream — only successfully completed operations emit them.

## Configure a trigger

In the function editor open Triggers → Add trigger → Storage events. Pick the bucket, the lifecycle events, and optionally an object-key prefix filter.

**Trigger configuration**

```json
{
  "kind": "storage",
  "config": {
    "bucket": "images",
    "events": ["storage.object.created"],
    "prefix": "uploads/"
  }
}
```

| Event | Fires when |
| --- | --- |
| `storage.object.created` | An upload was verified and finalized |
| `storage.object.updated` | An existing path was replaced (explicit overwrite) |
| `storage.object.deleted` | An object was deleted |

- A failed, abandoned or expired upload never emits an event.
- The payload carries bucket, object id, path, size, MIME type, actor and timestamp — never file contents.
- Read and write files through ctx.storage; every function write counts toward the same storage and bandwidth quotas and emits its own events.

## Process an upload

**Derivative writer**

```ts
export default async function handler(request: any, ctx: any) {
  const event = request.event; // storage.object.created, schemaVersion 1
  console.log('New object:', event.path, event.sizeBytes, 'bytes');

  const source = await ctx.storage.from(event.bucketName).download(event.path);
  // ...transform source.body (a Buffer) here...

  // Write derivatives to a DIFFERENT prefix so this function does not
  // re-trigger itself. Function writes count toward the same quotas.
  await ctx.storage.from(event.bucketName).upload(
    event.path.replace(/^uploads\//, 'processed/'),
    source.body,
    { mimeType: source.mimeType, overwrite: true },
  );
  return { processed: event.path };
}
```

> **Avoid trigger loops**: A function that writes into the same bucket and prefix it watches would re-trigger itself. Keep input and output prefixes separate (uploads/ → processed/). As a backstop, the platform tracks invocation depth and drops chains beyond the configured cap — but filtering is the correct fix.

## Delivery semantics

The same durable queue as database events: at-least-once delivery, dedupe on eventId, exponential-backoff retries, dead-letter state after exhaustion. The event contract is shared with the Storage events documentation.

## Additional resources

- [Storage event contract](https://cubibase.com/docs/storage/events) The schemaVersion 1 payload in full.
- [Templates](https://cubibase.com/docs/functions/templates) Image Processing starts from this pattern.
