import type { S3Client } from "@aws-sdk/client-s3"; import type { R2Bucket } from "@cloudflare/workers-types"; import type { AwsClient } from "aws4fetch"; import type { Adapter } from "../index.js"; export interface R2HttpOptions { /** R2 bucket name. */ bucket: string; /** * Cloudflare account ID. Falls back to `R2_ACCOUNT_ID` env var; required * if no env var is set — unless an explicit `endpoint` is passed, which * makes `accountId` unnecessary. */ accountId?: string; /** * R2 access key ID. Falls back to `R2_ACCESS_KEY_ID` env var; required if * no env var is set. */ accessKeyId?: string; /** * R2 secret access key. Falls back to `R2_SECRET_ACCESS_KEY` env var; * required if no env var is set. */ secretAccessKey?: string; /** * Origin used to build URLs from `url()` — typically an `r2.dev` * subdomain or a custom domain bound to the bucket. When set, `url()` * returns `${publicBaseUrl}/${key}` and skips signing. When unset, * `url()` returns a presigned GetObject URL (default expiry: 1 hour). */ publicBaseUrl?: string; /** * Default expiry, in seconds, for `url()` when `publicBaseUrl` is unset. * Defaults to 3600. */ defaultUrlExpiresIn?: number; /** * Override the S3 API endpoint. Defaults to * `https://.r2.cloudflarestorage.com`. Set it for jurisdiction * buckets, which live on their own hostnames (e.g. * `https://.eu.r2.cloudflarestorage.com`), or to point the * adapter at an S3-compatible stand-in (MinIO, LocalStack) in tests. * When set, `accountId` is not required. */ endpoint?: string; /** * Which HTTP engine backs the adapter. * * - `"aws-sdk"` (default): `@aws-sdk/client-s3` — the full surface, * including multipart/resumable uploads, byte-level upload progress, and * batched `deleteMany`. Requires the `@aws-sdk/*` optional peer * dependencies. Loaded lazily on first use. * - `"fetch"`: SigV4-signed `fetch` via `aws4fetch` (~2.5 KB) — no * `@aws-sdk/*` install needed, ideal for Workers and other edge * runtimes. Covers upload, download (+ ranges), head, exists, delete, * list (+ delimiter), server-side copy, presigned `url()`, and * `signedUploadUrl()`. Trade-offs: `ReadableStream` bodies are buffered * before the single PUT, `multipart`/`control` uploads throw, and bulk * deletes fan out per-key instead of batching. */ client?: "aws-sdk" | "fetch"; /** * Override the `fetch` implementation used by the `"fetch"` client — for * tests, or runtimes that hand out a bound/instrumented fetch. Defaults to * `globalThis.fetch`. Ignored by the `"aws-sdk"` client. */ fetch?: (request: Request) => Promise; } export interface R2BindingOptions { /** Workers `R2Bucket` binding. Reads and writes go through the binding. */ binding: R2Bucket; /** * R2 bucket name. Required for hybrid signing — it names the bucket in the * signed URL path that `url()` / `signedUploadUrl()` produce. Without it, * the HTTP credentials below are ignored and signing throws with guidance. */ bucket?: string; /** * Origin used to build URLs from `url()` — typically an `r2.dev` * subdomain or a custom domain bound to the bucket. Without this (and * without HTTP credentials below), `url()` throws because a Workers * binding has no signing primitive. */ publicBaseUrl?: string; /** * Hybrid mode: Cloudflare account ID, used alongside `accessKeyId` + * `secretAccessKey` so `url()` and `signedUploadUrl()` can fall back to * an S3-compatible SigV4 signer (aws4fetch — no `@aws-sdk/*` install * needed) instead of throwing. Reads and writes still go through the * binding so they stay intra-Worker (no egress fees). Useful for Workers * that need browser-facing presigned URLs without giving up the binding's * I/O performance. An explicit `endpoint` can stand in for `accountId`, * which only feeds the default signing hostname. */ accountId?: string; /** Hybrid mode: R2 access key ID. See `accountId`. */ accessKeyId?: string; /** Hybrid mode: R2 secret access key. See `accountId`. */ secretAccessKey?: string; /** * Default expiry, in seconds, for `url()` when it falls back to HTTP * signing (hybrid mode without `publicBaseUrl`). Defaults to 3600. */ defaultUrlExpiresIn?: number; /** * Hybrid mode: override the S3 API endpoint used for signing. Defaults to * `https://.r2.cloudflarestorage.com`. Set it for jurisdiction * buckets, which live on their own hostnames (e.g. * `https://.eu.r2.cloudflarestorage.com`). When set, * `accountId` is not required for hybrid signing. */ endpoint?: string; } export type R2AdapterOptions = R2BindingOptions | R2HttpOptions; export type R2Adapter = Adapter; export declare const r2: (opts: R2AdapterOptions) => R2Adapter; export type { R2Bucket } from "@cloudflare/workers-types"; //# sourceMappingURL=index.d.ts.map