Skip to content
Cloudflare Docs

Storage

Mount S3-compatible storage buckets (R2, S3, GCS) into the sandbox filesystem for persistent data access.

Methods

mountBucket()

Mount an S3-compatible bucket to a local path in the sandbox.

TypeScript
await sandbox.mountBucket(
bucket: string,
mountPath: string,
options: MountBucketOptions
): Promise<void>

Parameters:

  • bucket - Bucket name (e.g., "my-r2-bucket")
  • mountPath - Local filesystem path to mount at (e.g., "/data")
  • options - Mount configuration (see MountBucketOptions)
JavaScript
// Mount R2 bucket to /data
await sandbox.mountBucket("my-bucket", "/data", {
endpoint: "https://YOUR_ACCOUNT_ID.r2.cloudflarestorage.com",
provider: "r2",
});
// Read/write files directly
const data = await sandbox.readFile("/data/config.json");
await sandbox.writeFile("/data/results.json", JSON.stringify(data));
// Mount with explicit credentials
await sandbox.mountBucket("my-bucket", "/storage", {
endpoint: "https://s3.amazonaws.com",
credentials: {
accessKeyId: env.AWS_ACCESS_KEY_ID,
secretAccessKey: env.AWS_SECRET_ACCESS_KEY,
},
});
// Read-only mount
await sandbox.mountBucket("datasets", "/datasets", {
endpoint: "https://YOUR_ACCOUNT_ID.r2.cloudflarestorage.com",
readOnly: true,
});
// Mount a subdirectory within the bucket
await sandbox.mountBucket("shared-bucket", "/user-data", {
endpoint: "https://YOUR_ACCOUNT_ID.r2.cloudflarestorage.com",
prefix: "/users/user-123",
});

Throws:

  • InvalidMountPointError - Invalid mount path or conflicts with existing mounts
  • BucketAccessError - Bucket does not exist or insufficient permissions

unmountBucket()

Unmount a previously mounted bucket.

TypeScript
await sandbox.unmountBucket(mountPath: string): Promise<void>

Parameters:

  • mountPath - Path where the bucket is mounted (e.g., "/data")
JavaScript
// Mount, process, unmount
await sandbox.mountBucket("data", "/data", { endpoint: "..." });
await sandbox.exec("python process.py");
// Unmount
await sandbox.unmountBucket("/data");

Types

MountBucketOptions

TypeScript
interface MountBucketOptions {
endpoint: string;
provider?: BucketProvider;
credentials?: BucketCredentials;
readOnly?: boolean;
prefix?: string;
s3fsOptions?: string[];
}

Fields:

  • endpoint (required) - S3-compatible endpoint URL

    • R2: 'https://YOUR_ACCOUNT_ID.r2.cloudflarestorage.com'
    • S3: 'https://s3.amazonaws.com'
    • GCS: 'https://storage.googleapis.com'
  • provider (optional) - Storage provider hint

    • Enables provider-specific optimizations
    • Values: 'r2', 's3', 'gcs'
  • credentials (optional) - API credentials

    • Contains accessKeyId and secretAccessKey
    • If not provided, uses environment variables
  • readOnly (optional) - Mount in read-only mode

    • Default: false
  • prefix (optional) - Mount a subdirectory within the bucket

    • Must start with / (e.g., '/sessions/user123' or '/data/uploads/')
    • Enables multi-tenant isolation within a single bucket
  • s3fsOptions (optional) - Advanced s3fs mount flags as string array

    • Example: ['use_cache=/tmp/cache']

BucketProvider

Storage provider hint for automatic s3fs flag optimization.

TypeScript
type BucketProvider = "r2" | "s3" | "gcs";
  • 'r2' - Cloudflare R2 (recommended, applies nomixupload flag)
  • 's3' - Amazon S3
  • 'gcs' - Google Cloud Storage