Storage
Mount S3-compatible object storage as local filesystem paths for persistent storage across sandbox lifecycles.
Mount an S3-compatible bucket as a local directory.
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 bucketawait sandbox.mountBucket("my-r2-bucket", "/data", { endpoint: "https://YOUR_ACCOUNT_ID.r2.cloudflarestorage.com",});
// Access mounted bucketawait sandbox.exec("ls", { args: ["/data"] });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 R2 bucketawait sandbox.mountBucket('my-r2-bucket', '/data', { endpoint: 'https://YOUR_ACCOUNT_ID.r2.cloudflarestorage.com'});
// Access mounted bucketawait sandbox.exec('ls', { args: ['/data'] });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});Throws:
MissingCredentialsError- No credentials found in environment or optionsInvalidMountConfigError- Invalid endpoint, bucket name, or mount pathS3FSMountError- Mount operation failed (network, permissions, bucket not found)
Unmount a previously mounted bucket.
await sandbox.unmountBucket(mountPath: string): Promise<void>Parameters:
mountPath- Path where the bucket is mounted (e.g.,"/data")
await sandbox.mountBucket("my-bucket", "/data", { endpoint: "..." });
// Do workawait sandbox.exec("python process.py");
// Unmountawait sandbox.unmountBucket("/data");await sandbox.mountBucket('my-bucket', '/data', { endpoint: '...' });
// Do workawait sandbox.exec('python process.py');
// Unmountawait sandbox.unmountBucket('/data');Configuration for mounting a bucket.
interface MountBucketOptions { endpoint: string; provider?: BucketProvider; credentials?: BucketCredentials; readOnly?: boolean; s3fsOptions?: Record<string, string>;}Fields:
-
endpoint(required) - S3-compatible endpoint URL- R2:
https://YOUR_ACCOUNT_ID.r2.cloudflarestorage.com - S3:
https://s3.amazonaws.comor regional endpoint - GCS:
https://storage.googleapis.com
- R2:
-
provider(optional) - Storage provider hint ('r2'|'s3'|'gcs')- Default: Auto-detected from endpoint URL
-
credentials(optional) - S3 access credentials- Default: Uses
AWS_ACCESS_KEY_IDandAWS_SECRET_ACCESS_KEYenvironment variables
- Default: Uses
-
readOnly(optional) - Mount in read-only mode- Default:
false
- Default:
-
s3fsOptions(optional) - Advanced s3fs mount flags- 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
The SDK auto-detects the provider from the endpoint URL and applies optimized flags. For other S3-compatible providers (Backblaze, MinIO, etc.), omit this field or use the s3fsOptions parameter for custom configuration.
S3-compatible access credentials.
interface BucketCredentials { accessKeyId: string; secretAccessKey: string;}await sandbox.mountBucket("my-bucket", "/data", { endpoint: "https://YOUR_ACCOUNT_ID.r2.cloudflarestorage.com", credentials: { accessKeyId: env.AWS_ACCESS_KEY_ID, secretAccessKey: env.AWS_SECRET_ACCESS_KEY, },});await sandbox.mountBucket('my-bucket', '/data', { endpoint: 'https://YOUR_ACCOUNT_ID.r2.cloudflarestorage.com', credentials: { accessKeyId: env.AWS_ACCESS_KEY_ID, secretAccessKey: env.AWS_SECRET_ACCESS_KEY }});- Mount buckets guide - Complete mounting reference
- Persistent storage tutorial - Example with R2
- Environment variables - Credential configuration
- R2 documentation - Learn about Cloudflare R2
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
- © 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark