import { AwsClient } from "aws4fetch"; import type { Context } from "../context.js"; import { Resource } from "../resource.js"; import type { Secret } from "../secret.js"; import { CloudflareApi } from "./api.js"; /** * Properties for creating or updating an R2 Bucket */ export interface BucketProps { /** * Name of the bucket * Names can only contain lowercase letters (a-z), numbers (0-9), and hyphens (-) * Cannot begin or end with a hyphen * * @default - the id of the resource */ name?: string; /** * Optional location hint for the bucket * Indicates the primary geographical location data will be accessed from */ locationHint?: string; /** * Optional jurisdiction for the bucket * Determines the regulatory jurisdiction the bucket data falls under */ jurisdiction?: "default" | "eu" | "fedramp"; /** * Whether to allow public access through the r2.dev subdomain * Only for development purposes - use custom domains for production */ allowPublicAccess?: boolean; /** * Whether to delete the bucket. * If set to false, the bucket will remain but the resource will be removed from state * * @default true */ delete?: boolean; /** * Whether to empty the bucket and delete all objects during resource deletion * @default false */ empty?: boolean; /** * API Token to use for the bucket */ apiToken?: Secret; /** * API Key to use for the bucket */ apiKey?: Secret; /** * Email to use for the bucket */ email?: string; /** * Account ID to use for the bucket */ accountId?: string; /** * Access Key to use for the bucket */ accessKey?: Secret; /** * Secret Access Key to use for the bucket */ secretAccessKey?: Secret; /** * Whether to adopt an existing bucket */ adopt?: boolean; } /** * Output returned after R2 Bucket creation/update */ export interface R2Bucket extends Resource<"cloudflare::R2Bucket">, BucketProps { /** * Resource type identifier */ type: "r2_bucket"; /** * The name of the bucket */ name: string; /** * Location of the bucket */ location: string; /** * Time at which the bucket was created */ creationDate: Date; } /** * Creates and manages Cloudflare R2 Buckets for object storage. * * R2 Buckets provide S3-compatible object storage with automatic data replication * across multiple regions for high availability and durability. * * @example * // Create a basic R2 bucket with default settings * const basicBucket = await R2Bucket("my-app-data", { * name: "my-app-data" * }); * * @example * // Create a bucket with location hint for optimal performance * const euBucket = await R2Bucket("eu-user-data", { * name: "eu-user-data", * locationHint: "eu", * jurisdiction: "eu" * }); * * @example * // Create a development bucket with public access enabled * const publicBucket = await R2Bucket("public-assets", { * name: "public-assets", * allowPublicAccess: true * }); * * @example * // Create a FedRAMP compliant bucket for government workloads * const fedRampBucket = await R2Bucket("gov-data", { * name: "gov-data", * jurisdiction: "fedramp" * }); * * @example * // Create a bucket that will be automatically emptied when deleted * // This will delete all objects in the bucket before deleting the bucket itself * const temporaryBucket = await R2Bucket("temp-storage", { * name: "temp-storage", * empty: true // All objects will be deleted when this resource is destroyed * }); * * @see https://developers.cloudflare.com/r2/buckets/ */ export declare const R2Bucket: (((this: any, id: string, props?: {}) => never) & (new (_: never) => never)) | ((this: Context, id: string, props?: BucketProps) => Promise); /** * Configuration for R2 client to connect to Cloudflare R2 */ export interface R2ClientConfig { accountId: string; accessKeyId?: Secret; secretAccessKey?: Secret; jurisdiction?: string; } type R2Client = AwsClient & { accountId: string; }; /** * Creates an aws4fetch client configured for Cloudflare R2 * * @see https://developers.cloudflare.com/r2/examples/aws/aws-sdk-js-v3/ */ export declare function createR2Client(config?: R2ClientConfig): Promise; interface CloudflareBucketResponse { /** * The bucket information returned from the Cloudflare REST API * @see https://developers.cloudflare.com/api/node/resources/r2/subresources/buckets/models/bucket/#(schema) */ result: { creation_date: string; location?: "apac" | "eeur" | "enam" | "weur" | "wnam" | "oc"; name: string; storage_class?: "Standard" | "InfrequentAccess"; }; success: boolean; errors: Array<{ code: number; message: string; }>; messages: string[]; } /** * Adds jurisdiction header to the headers object if specified in props * * @param headers Headers object to modify * @param props Props or jurisdiction string * @returns Modified headers object */ export declare function withJurisdiction(headers: Record | undefined, props: BucketProps | { jurisdiction?: string; } | string | undefined): Record; /** * Get a bucket */ export declare function getBucket(api: CloudflareApi, bucketName: string, props?: BucketProps): Promise; /** * Create a new bucket */ export declare function createBucket(api: CloudflareApi, bucketName: string, props?: BucketProps): Promise; /** * Delete a bucket */ export declare function deleteBucket(api: CloudflareApi, bucketName: string, props: BucketProps): Promise; /** * List objects in an R2 bucket * * @param r2 R2Client instance * @param bucketName Name of the bucket * @param continuationToken Optional token for pagination * @param jurisdiction Optional jurisdiction for the bucket * @returns Object containing the list of objects and the next continuation token */ export declare function listObjects(r2: R2Client, bucketName: string, continuationToken?: string, jurisdiction?: string): Promise<{ objects: { Key: string; }[]; continuationToken?: string; }>; /** * Helper function to empty a bucket by deleting all objects */ export declare function emptyBucket(r2: R2Client, bucketName: string, jurisdiction?: string): Promise; /** * Update public access setting for a bucket * * This operation is not available through the S3 API for R2, * so we still use the Cloudflare API directly. */ export declare function updatePublicAccess(api: CloudflareApi, bucketName: string, allowPublicAccess: boolean, jurisdiction?: string): Promise; /** * Set CORS configuration for a bucket using aws4fetch */ export declare function setCorsConfiguration(r2: R2Client, bucketName: string, allowedOrigins?: string[], allowedMethods?: string[], allowedHeaders?: string[], maxAgeSeconds?: number, jurisdiction?: string): Promise; /** * Information about an R2 bucket returned by list operations */ export interface R2BucketInfo { /** * Name of the bucket */ Name: string; /** * Creation date of the bucket */ CreationDate: Date; } /** * List all R2 buckets in an account * * @param api CloudflareApi instance * @param options Optional listing options * @returns Array of bucket information */ export declare function listBuckets(api: CloudflareApi, options?: { nameContains?: string; perPage?: number; cursor?: string; direction?: "asc" | "desc"; jurisdiction?: string; }): Promise; export {}; //# sourceMappingURL=bucket.d.ts.map