UNPKG

@opendatalabs/vana-sdk

Version:

A TypeScript library for interacting with Vana Network smart contracts.

92 lines (91 loc) 3.13 kB
import { type StorageProvider, type StorageUploadResult, type StorageFile, type StorageListOptions, type StorageProviderConfig } from "../index"; /** * Configuration for {@link R2Storage}. * * @category Storage */ export interface R2Config { /** Cloudflare account ID. Used to derive the S3 endpoint when `endpoint` is not given. */ accountId?: string; /** * Full S3 endpoint URL. Overrides `accountId`. * * Defaults to `https://{accountId}.r2.cloudflarestorage.com`. */ endpoint?: string; /** R2 access key ID. */ accessKeyId: string; /** R2 secret access key. */ secretAccessKey: string; /** Bucket name. */ bucket: string; /** * Public URL prefix used when constructing the URL returned by {@link R2Storage.upload}. * * If unset, defaults to `https://{bucket}.{accountId}.r2.dev` (R2's default public hostname). * Set this to a custom domain bound to the bucket if you have one. */ publicUrl?: string; /** * S3 region. R2 expects `auto`; consumers can override for compatibility with other * S3-compatible stores reachable through the same code path. */ region?: string; } /** * Cloudflare R2 storage provider. * * @remarks * Uses R2's S3-compatible API with SigV4 signed `fetch` requests, so the same * implementation runs in browsers, Node.js, and Workers without pulling in * `@aws-sdk/client-s3`. Treats blobs as opaque - encryption, access control, * and per-tenant prefixing are out of scope and live in higher layers. * * The SDK exposes R2 as the recommended default backend. Other providers * (Pinata, IPFS, Google Drive, Dropbox, CallbackStorage) remain available for * teams that need IPFS semantics or user-owned storage. * * @category Storage * * @example * ```typescript * import { R2Storage, StorageManager } from "@opendatalabs/vana-sdk/node"; * * const storage = new StorageManager(); * storage.register( * "r2", * new R2Storage({ * accountId: process.env.R2_ACCOUNT_ID!, * accessKeyId: process.env.R2_ACCESS_KEY_ID!, * secretAccessKey: process.env.R2_SECRET_ACCESS_KEY!, * bucket: process.env.R2_BUCKET!, * publicUrl: "https://files.example.com", * }), * true, * ); * * const result = await storage.upload(myBlob, "report.json"); * console.log(result.url); * ``` */ export declare class R2Storage implements StorageProvider { private readonly config; private readonly endpoint; private readonly publicUrl; private readonly region; private readonly bucket; constructor(config: R2Config); upload(file: Blob, filename?: string): Promise<StorageUploadResult>; download(url: string): Promise<Blob>; list(options?: StorageListOptions): Promise<StorageFile[]>; delete(url: string): Promise<boolean>; getConfig(): StorageProviderConfig; /** * Extract the object key from a URL. Accepts public URLs minted by `upload()` * as well as raw keys for callers that already track them out-of-band. * * @internal */ private extractKey; private signRequest; }