Storage
Mount S3-compatible storage buckets (R2, S3, GCS) into the sandbox filesystem for persistent data access.
Mount an S3-compatible bucket to a local path in the sandbox.
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 (seeMountBucketOptions)
// Mount R2 bucket to /dataawait sandbox.mountBucket("my-bucket", "/data", { endpoint: "https://YOUR_ACCOUNT_ID.r2.cloudflarestorage.com", provider: "r2",});
// Read/write files directlyconst data = await sandbox.readFile("/data/config.json");await sandbox.writeFile("/data/results.json", JSON.stringify(data));
// Mount with explicit credentialsawait 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 mountawait sandbox.mountBucket("datasets", "/datasets", { endpoint: "https://YOUR_ACCOUNT_ID.r2.cloudflarestorage.com", readOnly: true,});
// Mount a subdirectory within the bucketawait sandbox.mountBucket("shared-bucket", "/user-data", { endpoint: "https://YOUR_ACCOUNT_ID.r2.cloudflarestorage.com", prefix: "/users/user-123",});// Mount R2 bucket to /dataawait sandbox.mountBucket('my-bucket', '/data', { endpoint: 'https://YOUR_ACCOUNT_ID.r2.cloudflarestorage.com', provider: 'r2'});
// Read/write files directlyconst data = await sandbox.readFile('/data/config.json');await sandbox.writeFile('/data/results.json', JSON.stringify(data));
// Mount with explicit credentialsawait 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 mountawait sandbox.mountBucket('datasets', '/datasets', { endpoint: 'https://YOUR_ACCOUNT_ID.r2.cloudflarestorage.com', readOnly: true});
// Mount a subdirectory within the bucketawait 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 mountsBucketAccessError- Bucket does not exist or insufficient permissions
Unmount a previously mounted bucket.
await sandbox.unmountBucket(mountPath: string): Promise<void>Parameters:
mountPath- Path where the bucket is mounted (e.g.,"/data")
// Mount, process, unmountawait sandbox.mountBucket("data", "/data", { endpoint: "..." });await sandbox.exec("python process.py");
// Unmountawait sandbox.unmountBucket("/data");// Mount, process, unmountawait sandbox.mountBucket('data', '/data', { endpoint: '...' });await sandbox.exec('python process.py');
// Unmountawait sandbox.unmountBucket('/data');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'
- R2:
-
provider(optional) - Storage provider hint- Enables provider-specific optimizations
- Values:
'r2','s3','gcs'
-
credentials(optional) - API credentials- Contains
accessKeyIdandsecretAccessKey - If not provided, uses environment variables
- Contains
-
readOnly(optional) - Mount in read-only mode- Default:
false
- Default:
-
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
- Must start with
-
s3fsOptions(optional) - Advanced s3fs mount flags as string array- Example:
['use_cache=/tmp/cache']
- Example:
Storage provider hint for automatic s3fs flag optimization.
type BucketProvider = "r2" | "s3" | "gcs";'r2'- Cloudflare R2 (recommended, appliesnomixuploadflag)'s3'- Amazon S3'gcs'- Google Cloud Storage
- Mount Buckets guide - Complete bucket mounting walkthrough
- Files API - Read and write files
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Directory
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- © 2026 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark
-