import { type ApiFromModules, type FunctionReference, type GenericActionCtx, type GenericDataModel, type GenericMutationCtx, type GenericQueryCtx } from "convex/server"; import { type Infer } from "convex/values"; import type { ComponentApi } from "../component/_generated/component.js"; import { S3Client } from "@aws-sdk/client-s3"; import { r2ConfigValidator } from "../shared.js"; export type R2Callbacks = { onSyncMetadata?: FunctionReference<"mutation", "internal", { bucket: string; key: string; isNew: boolean; }>; }; export declare const DEFAULT_BATCH_SIZE = 10; export type ClientApi = ApiFromModules<{ client: ReturnType; }>["client"]; type QueryCtx = Pick, "runQuery">; type MutationCtx = Pick, "runQuery" | "runMutation">; type ActionCtx = Pick, "runQuery" | "runMutation" | "runAction">; export declare class R2 { component: ComponentApi; options: { bucket?: string; endpoint?: string; accessKeyId?: string; secretAccessKey?: string; /** @deprecated Use `bucket` instead. */ R2_BUCKET?: string; /** @deprecated Use `endpoint` instead. */ R2_ENDPOINT?: string; /** @deprecated Use `accessKeyId` instead. */ R2_ACCESS_KEY_ID?: string; /** @deprecated Use `secretAccessKey` instead. */ R2_SECRET_ACCESS_KEY?: string; defaultBatchSize?: number; }; readonly config: Infer; private _client; /** * The configured S3Client for direct access to the R2 bucket. */ get client(): S3Client; /** * @deprecated Use `client` instead. */ get r2(): S3Client; /** * Backend API for the R2 component. * Responsible for exposing the `client` API to the client, and having * convenience methods for interacting with the component from the backend. * * Typically used like: * * ```ts * const r2 = new R2(components.r2); * export const { * ... // see {@link clientApi} docstring for details * } = r2.clientApi({...}); * ``` * * @param component - Generally `components.r2` from * `./_generated/api` once you've configured it in `convex.config.ts`. * @param options - Optional config object. If not provided, values are read * from environment variables. * - `bucket` - The R2 bucket name. Falls back to `R2_BUCKET` env var. * - `endpoint` - The R2 endpoint URL. Falls back to `R2_ENDPOINT` env var. * - `accessKeyId` - The R2 access key ID. Falls back to `R2_ACCESS_KEY_ID` env var. * - `secretAccessKey` - The R2 secret access key. Falls back to `R2_SECRET_ACCESS_KEY` env var. * - `defaultBatchSize` - The default batch size to use for pagination. */ constructor(component: ComponentApi, options?: { bucket?: string; endpoint?: string; accessKeyId?: string; secretAccessKey?: string; /** @deprecated Use `bucket` instead. */ R2_BUCKET?: string; /** @deprecated Use `endpoint` instead. */ R2_ENDPOINT?: string; /** @deprecated Use `accessKeyId` instead. */ R2_ACCESS_KEY_ID?: string; /** @deprecated Use `secretAccessKey` instead. */ R2_SECRET_ACCESS_KEY?: string; defaultBatchSize?: number; }); /** * Get a signed URL for serving an object from R2. * * @param key - The R2 object key. * @param options - Optional config object. * - `expiresIn` - The number of seconds until the URL expires (default: 900, max: 604800 for 7 days). * @returns A promise that resolves to a signed URL for the object. */ getUrl(key: string, options?: { expiresIn?: number; }): Promise; /** * Generate a signed URL for uploading an object to R2. * * @param customKey (optional) - A custom R2 object key to use. Must be unique. * @returns A promise that resolves to an object with the following fields: * - `key` - The R2 object key. * - `url` - A signed URL for uploading the object. */ generateUploadUrl(customKey?: string): Promise<{ key: string; url: string; }>; /** * Store a blob in R2 and sync the metadata to Convex. * * @param ctx - A Convex action context. * @param blob - The blob to store. * @param opts - Optional config object. * - `key` - A custom R2 object key to use (uuid if not provided). * - `type` - The MIME type of the blob (will be inferred if not provided). * - `disposition` - The ContentDisposition header to let the browser know how to handle the file. * - `cacheControl` - The Cache-Control header to set on the object (e.g. "max-age=3600"). * @returns A promise that resolves to the key of the stored object. */ store(ctx: ActionCtx, file: Uint8Array | Buffer | Blob, opts?: string | { key?: string; type?: string; disposition?: string; cacheControl?: string; }): Promise; /** * Retrieve R2 object metadata and store in Convex. * * @param ctx - A Convex action context. * @param key - The R2 object key. * @returns A promise that resolves when the metadata is synced. */ syncMetadata(ctx: ActionCtx, key: string): Promise; /** * Retrieve R2 object metadata from Convex. * * @param ctx - A Convex query context. * @param key - The R2 object key. * @returns A promise that resolves to the metadata for the object. */ getMetadata(ctx: QueryCtx | MutationCtx | ActionCtx, key: string): Promise<{ bucket: string; bucketLink: string; contentType?: string; key: string; lastModified: string; link: string; sha256?: string; size?: number; url: string; } | null>; /** * Retrieve all metadata from Convex for a given bucket. * * @param ctx - A Convex query context. * @param limit (optional) - The maximum number of documents to return. * @returns A promise that resolves to an array of metadata documents. */ listMetadata(ctx: QueryCtx | MutationCtx | ActionCtx, limit?: number, cursor?: string | null): Promise<{ continueCursor: string; isDone: boolean; page: Array<{ bucket: string; bucketLink: string; contentType?: string; key: string; lastModified: string; link: string; sha256?: string; size?: number; url: string; }>; pageStatus?: null | "SplitRecommended" | "SplitRequired"; splitCursor?: null | string; }>; /** * Delete an object from R2. * * @param ctx - A Convex action context. * @param key - The R2 object key. * @returns A promise that resolves when the object is deleted. */ deleteObject(ctx: MutationCtx | ActionCtx, key: string): Promise; /** * Expose the client API to the client for use with the `useUploadFile` hook. * If you export these in `convex/r2.ts`, pass `api.r2` * to the `useUploadFile` hook. * * It allows you to define optional read, upload, and delete permissions. * * You can pass the optional type argument `` to have the `ctx` * parameter specific to your tables. * * ```ts * import { DataModel } from "./convex/_generated/dataModel"; * // ... * export const { ... } = r2.clientApi({...}); * ``` * * To define just one function to use for both, you can define it like this: * ```ts * async function checkPermissions(ctx: QueryCtx, id: string) { * const user = await getAuthUser(ctx); * if (!user || !(await canUserAccessDocument(user, id))) { * throw new Error("Unauthorized"); * } * } * ``` * @param opts - Optional callbacks. * @returns functions to export, so the `useUploadFile` hook can use them, or * for direct use in your own client code. */ clientApi(opts?: { checkReadKey?: (ctx: GenericQueryCtx, bucket: string, key: string) => void | Promise; checkReadBucket?: (ctx: GenericQueryCtx, bucket: string) => void | Promise; checkUpload?: (ctx: GenericQueryCtx, bucket: string) => void | Promise; checkDelete?: (ctx: GenericQueryCtx, bucket: string, key: string) => void | Promise; onUpload?: (ctx: GenericMutationCtx, bucket: string, key: string) => void | Promise; onSyncMetadata?: (ctx: GenericMutationCtx, args: { bucket: string; key: string; isNew: boolean; }) => void | Promise; onDelete?: (ctx: GenericMutationCtx, bucket: string, key: string) => void | Promise; callbacks?: R2Callbacks; }): { /** * Generate a signed URL for uploading an object to R2. */ generateUploadUrl: import("convex/server").RegisteredMutation<"public", {}, Promise<{ key: string; url: string; }>>; /** * Retrieve R2 object metadata and store in Convex. */ syncMetadata: import("convex/server").RegisteredMutation<"public", { key: string; }, Promise>; onSyncMetadata: import("convex/server").RegisteredMutation<"internal", { bucket: string; key: string; isNew: boolean; }, Promise>; /** * Retrieve metadata for an R2 object from Convex. */ getMetadata: import("convex/server").RegisteredQuery<"public", { key: string; }, Promise<{ bucket: string; bucketLink: string; contentType?: string; key: string; lastModified: string; link: string; sha256?: string; size?: number; url: string; } | null>>; /** * Retrieve all metadata for a given bucket from Convex. */ listMetadata: import("convex/server").RegisteredQuery<"public", { paginationOpts: { id?: number; endCursor?: string | null; maximumRowsRead?: number; maximumBytesRead?: number; numItems: number; cursor: string | null; }; }, Promise<{ continueCursor: string; isDone: boolean; page: Array<{ bucket: string; bucketLink: string; contentType?: string; key: string; lastModified: string; link: string; sha256?: string; size?: number; url: string; }>; pageStatus?: null | "SplitRecommended" | "SplitRequired"; splitCursor?: null | string; }>>; /** * Delete an object from R2 and remove its metadata from Convex. */ deleteObject: import("convex/server").RegisteredMutation<"public", { key: string; }, Promise>; }; } export {}; //# sourceMappingURL=index.d.ts.map