Bucket models binary object storage for PDFs, images, exports, and attachments. It enforces key and MIME types at compile time and checks allowed content types and maximum object sizes at runtime.
Use Bucket for blobs and files. On Cloudflare Workers, @chimp-labs/keel-alchemy provisions and binds an R2 bucket. On self-hosted Docker, it provisions a Garage S3 bucket with scoped credentials.
Declaring a Bucket
Plain TypeScript SDK (@chimp-labs/keel/sdk)
import { k } from "@chimp-labs/keel/sdk"
export const ReceiptsBucket = k.bucket("receipts", {
contentTypes: ["application/pdf"] as const,
maxBytes: 5 * 1024 * 1024, // 5 MiB
metadata: k.object({
orderId: k.string(),
customerId: k.string(),
}),
})
Configuration options
| Option | Type | Required | Description |
|---|---|---|---|
contentTypes |
readonly string[] |
Yes | Allowed MIME types, validated at compile time and runtime. |
maxBytes |
number |
Yes | Maximum allowed payload size in bytes. Oversized uploads fail with ObjectTooLargeError. |
metadata |
SchemaLike |
No | Optional typed metadata schema validated on put and decoded on get. |
Effect v4 (@chimp-labs/keel/bucket)
import { Schema } from "effect"
import { Bucket } from "@chimp-labs/keel/bucket"
const Meta = Schema.Struct({ orderId: Schema.String })
export class MediaBucket extends Bucket<
"media",
`products/${string}/${string}`,
readonly ["image/webp", "image/png"],
typeof Meta
>("media", {
contentTypes: ["image/webp", "image/png"] as const,
maxBytes: 10 * 1024 * 1024,
metadata: Meta,
}) {}
Attaching to a service
Declaring a bucket in a service’s resources grants only that service unit access to the bucket:
export const BillingService = k.service("billing", {
resources: [ReceiptsBucket],
operations: { /* ... */ },
})
Operations reference
Upload, read, stream, and list
// Upload with MIME-type, byte-size, and metadata validation
await keel.receipts.put(`orders/${orderId}.pdf`, pdfBytes, {
contentType: "application/pdf",
metadata: { orderId, customerId },
})
// Download full bytes into memory
const file = await keel.receipts.getBytes(`orders/${orderId}.pdf`)
// { contentType, size, etag, metadata, bytes: Uint8Array }
// Stream large objects chunk-by-chunk
const streamObj = await keel.receipts.get(`orders/${orderId}.pdf`, { signal })
for await (const chunk of streamObj.body) {
// chunk: Uint8Array
}
// Inspect metadata without downloading payload
const info = await keel.receipts.head(`orders/${orderId}.pdf`)
// List objects by prefix (buffered or streamed)
const entries = await keel.receipts.list({ prefix: "orders/" })
for await (const entry of keel.receipts.listStream({ prefix: "orders/", signal })) {
console.log(entry.key, entry.size, entry.etag)
}
// Delete an object
await keel.receipts.delete(`orders/${orderId}.pdf`)
Presigned URLs
Generate time-limited upload and download URLs for browsers or external clients. This requires ObjectPresigner, which is included in the Memory, S3/Garage, and R2 adapters:
const downloadUrl = await keel.receipts.presignGet(`orders/${orderId}.pdf`, "15 minutes")
const uploadUrl = await keel.receipts.presignPut(`orders/${orderId}.pdf`, {
contentType: "application/pdf",
expiresIn: "5 minutes",
})
SDK and Effect API mapping
SDK method (keel.<bucket>.*) |
Effect function (@chimp-labs/keel/bucket) |
Description |
|---|---|---|
put(key, bytes, opts) |
putObject(bucket, key, body, opts) |
Uploads bytes (or Stream<Uint8Array> in Effect) with MIME and size checks. |
get(key) |
getObject(bucket, key) |
Streams object body with metadata and ETag. |
getBytes(key) |
getObjectBytes(bucket, key) |
Reads the entire object into a Uint8Array. |
head(key) |
ObjectStore.head(name, key) |
Fetches object size, contentType, and ETag without the body. |
delete(key) |
deleteObject(bucket, key) |
Deletes an object by key. |
list({ prefix? }) |
listObjects(bucket, { prefix? }) |
Lists matching object entries. |
listStream({ prefix? }) |
listObjects(bucket, { prefix? }) |
Streams matching object entries. |
presignGet(key, ttl?) |
presignGet(bucket, key, ttl?) |
Generates a SigV4 presigned GET URL. |
presignPut(key, opts) |
presignPut(bucket, key, opts) |
Generates a SigV4 presigned PUT URL. |
Errors and failure modes
| Error tag | Category | HTTP status | Retryable | When it occurs |
|---|---|---|---|---|
ObjectNotFoundError |
not_found |
404 |
No | get, getBytes, head, or delete targets a missing key |
InvalidContentTypeError |
invalid |
415 |
No | Content type is not listed in bucket.contentTypes |
ObjectTooLargeError |
invalid |
413 |
No | Payload exceeds bucket.maxBytes |
ObjectStoreError |
internal |
500 |
Yes | Underlying S3, Garage, or R2 request failure |
SchemaError |
invalid |
400 |
No | Object metadata fails schema validation |
Backends and infrastructure
- Memory uses an in-process object store with deterministic local presigner URLs.
- S3, Garage S3, and AWS S3 (
@chimp-labs/keel/s3) use a zero-dependency SigV4 client built on nativefetchand Web Crypto (crypto.subtle). - Cloudflare R2 (
@chimp-labs/keel-alchemy/cloudflare) provisions a native R2 Worker binding (BUCKET_<NAME>) bound only to the owning worker.
Testing in Memory
await using keel = await createKeel({
backend: "memory",
resources: { receipts: ReceiptsBucket },
})
const bytes = new TextEncoder().encode("%PDF-1.7 ...")
await keel.receipts.put("orders/ord_1.pdf", bytes, {
contentType: "application/pdf",
metadata: { orderId: "ord_1", customerId: "cus_1" },
})
const out = await keel.receipts.getBytes("orders/ord_1.pdf")
// out.size === bytes.byteLength, out.metadata.orderId === "ord_1"