Skip to content

Commit 77522f9

Browse files
scuffiscuffi
andauthored
Update docs for Sandbox local mount support (#28890)
* Update docs for local mount support * Update confusing env example --------- Co-authored-by: scuffi <aferguson@cloudflare.com>
1 parent f05632c commit 77522f9

4 files changed

Lines changed: 92 additions & 19 deletions

File tree

src/content/docs/sandbox/api/storage.mdx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ Mounted buckets are automatically unmounted when the container is destroyed.
110110

111111
```ts
112112
interface MountBucketOptions {
113-
endpoint: string;
113+
endpoint?: string;
114+
localBucket?: boolean;
114115
provider?: BucketProvider;
115116
credentials?: BucketCredentials;
116117
readOnly?: boolean;
@@ -121,11 +122,17 @@ interface MountBucketOptions {
121122

122123
**Fields**:
123124

124-
- `endpoint` (required) - S3-compatible endpoint URL
125+
- `endpoint` (required when `localBucket` is `false` or unset) - S3-compatible endpoint URL
125126
- R2: `'https://YOUR_ACCOUNT_ID.r2.cloudflarestorage.com'`
126127
- S3: `'https://s3.amazonaws.com'`
127128
- GCS: `'https://storage.googleapis.com'`
128129

130+
- `localBucket` (optional) - Mount an R2 bucket using the Worker's R2 binding during local development with `wrangler dev`
131+
- When `true`, the SDK syncs the R2 binding directly instead of using an S3 endpoint
132+
- `endpoint` and `credentials` are not required when this is `true`
133+
- `provider` and `s3fsOptions` are not used when this is `true`
134+
- Default: `false`
135+
129136
- `provider` (optional) - Storage provider hint
130137
- Enables provider-specific optimizations
131138
- Values: `'r2'`, `'s3'`, `'gcs'`

src/content/docs/sandbox/configuration/environment-variables.mdx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,6 @@ wrangler secret put AWS_SECRET_ACCESS_KEY
296296
# Paste your R2 Secret Access Key
297297
```
298298

299-
:::caution[Production only]
300-
Bucket mounting requires production deployment. It does not work with `wrangler dev` due to FUSE support limitations. See [Mount buckets guide](/sandbox/guides/mount-buckets/) for details.
301-
:::
302-
303299
**Mount buckets with automatic credential detection:**
304300

305301
```typescript

src/content/docs/sandbox/guides/mount-buckets.mdx

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,14 @@ sidebar:
66
description: Mount S3-compatible object storage as local filesystems for persistent data storage.
77
---
88

9-
import { TypeScriptExample } from "~/components";
9+
import { TypeScriptExample, WranglerConfig } from "~/components";
1010

1111
Mount S3-compatible object storage buckets as local filesystem paths. Access object storage using standard file operations.
1212

1313
:::note[S3-compatible providers]
1414
The SDK works with any S3-compatible object storage provider. Examples include Cloudflare R2, Amazon S3, Google Cloud Storage, Backblaze B2, MinIO, and [many others](https://github.com/s3fs-fuse/s3fs-fuse/wiki/Non-Amazon-S3). The SDK automatically detects and optimizes for R2, S3, and GCS.
1515
:::
1616

17-
:::caution[Production only]
18-
Bucket mounting does not work with `wrangler dev` because it requires FUSE support that wrangler does not currently provide. Deploy your Worker with `wrangler deploy` to use this feature. All other Sandbox SDK features work in local development.
19-
:::
20-
2117
## When to mount buckets
2218

2319
Mount S3-compatible buckets when you need:
@@ -150,6 +146,86 @@ await sandbox.writeFile('/data/new-file.txt', 'data'); // Error: Read-only file
150146
```
151147
</TypeScriptExample>
152148

149+
## Local development
150+
151+
You can mount R2 buckets during local development with `wrangler dev` by passing the `localBucket` option. This uses the R2 binding from your Worker environment directly, so no S3-compatible endpoint or credentials are required.
152+
153+
### Configure R2 bindings
154+
155+
Add an R2 bucket binding to your Wrangler configuration:
156+
157+
<WranglerConfig>
158+
```jsonc
159+
{
160+
"r2_buckets": [
161+
{
162+
"binding": "MY_BUCKET",
163+
"bucket_name": "my-test-bucket"
164+
}
165+
]
166+
}
167+
```
168+
</WranglerConfig>
169+
170+
### Mount with `localBucket`
171+
172+
Pass `localBucket: true` in the options to mount the bucket locally:
173+
174+
<TypeScriptExample>
175+
```typescript
176+
await sandbox.mountBucket('MY_BUCKET', '/data', {
177+
localBucket: true
178+
});
179+
180+
// Access files using standard operations
181+
await sandbox.exec('ls', { args: ['/data'] });
182+
await sandbox.writeFile('/data/results.json', JSON.stringify(results));
183+
```
184+
</TypeScriptExample>
185+
186+
:::note
187+
You can use an environment variable to toggle `localBucket` between local development and production. Set an environment variable such as `LOCAL_DEV` in your Wrangler configuration using `vars` for local development, then reference it in your code:
188+
189+
```typescript
190+
await sandbox.mountBucket('MY_BUCKET', '/data', {
191+
localBucket: Boolean(env.LOCAL_DEV),
192+
endpoint: 'https://YOUR_ACCOUNT_ID.r2.cloudflarestorage.com'
193+
});
194+
```
195+
196+
When `localBucket` is `true`, the `endpoint` is ignored and the SDK uses the R2 binding directly. For more information on setting environment variables, refer to [Environment variables in Wrangler configuration](/workers/configuration/environment-variables/).
197+
:::
198+
199+
The `readOnly` and `prefix` options work the same way in local mode:
200+
201+
<TypeScriptExample>
202+
```typescript
203+
// Read-only local mount
204+
await sandbox.mountBucket('MY_BUCKET', '/data', {
205+
localBucket: true,
206+
readOnly: true
207+
});
208+
209+
// Mount a subdirectory
210+
await sandbox.mountBucket('MY_BUCKET', '/images', {
211+
localBucket: true,
212+
prefix: '/uploads/images/'
213+
});
214+
```
215+
</TypeScriptExample>
216+
217+
### Local development considerations
218+
219+
During local development, files are synchronized between R2 and the container using a periodic sync process rather than a direct filesystem mount. Keep the following in mind:
220+
221+
- **Synchronization window** - A brief delay exists between when a file is written and when it appears on the other side. For example, if you upload a file to R2 and then immediately read it from the mounted path in the container, the file may not yet be available. Allow a short window for synchronization to complete before reading recently written data.
222+
- **High-frequency writes** - Rapid successive writes to the same file path may take slightly longer to fully propagate. For best results, avoid writing to the same file from both R2 and the container at the same time.
223+
- **Bidirectional sync** - Changes made in the container are synced to R2, and changes made in R2 are synced to the container. Both directions follow the same periodic sync model.
224+
225+
:::note
226+
These considerations apply to local development with `wrangler dev` only. In production, bucket mounts use a direct filesystem mount with no synchronization delay.
227+
:::
228+
153229
## Unmount buckets
154230

155231
<TypeScriptExample>

src/content/docs/sandbox/tutorials/persistent-storage.mdx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,7 @@ Try this flow:
176176
Replace `YOUR_ACCOUNT_ID` in the endpoint URL with your Cloudflare account ID. Find it in the [dashboard](https://dash.cloudflare.com/) under **R2** > **Overview**.
177177
:::
178178

179-
## 4. Local development limitation
180-
181-
:::caution[Requires production deployment]
182-
Bucket mounting does not work with `wrangler dev` because it requires FUSE support that wrangler does not currently provide. You must deploy to production to test this feature. All other Sandbox SDK features work locally - only `mountBucket()` and `unmountBucket()` require production deployment.
183-
:::
184-
185-
## 5. Deploy to production
179+
## 4. Deploy to production
186180

187181
**Generate R2 API tokens:**
188182
1. Go to **R2** > **Overview** in the [Cloudflare dashboard](https://dash.cloudflare.com/)
@@ -210,7 +204,7 @@ npx wrangler deploy
210204

211205
After deployment, wrangler outputs your Worker URL (e.g., `https://data-pipeline.yourname.workers.dev`).
212206

213-
## 6. Test the persistence flow
207+
## 5. Test the persistence flow
214208

215209
Now test against your deployed Worker. Replace `YOUR_WORKER_URL` with your actual Worker URL:
216210

0 commit comments

Comments
 (0)