Skip to content

Commit a64f431

Browse files
agents-git-bot[bot]github-actions[bot]elithrar
authored
Document prefix option for mounting bucket subdirectories (#27459)
* Document prefix option for mounting bucket subdirectories Add documentation for the new prefix option in mountBucket() that allows mounting specific subdirectories within S3-compatible buckets. Changes: - Add prefix field to MountBucketOptions type documentation - Add example of mounting bucket subdirectory in API reference - Add dedicated section in mount-buckets guide with multiple examples - Document prefix format requirements (must start and end with /) Related: cloudflare/sandbox-sdk#335 * Add documentation for mountBucket prefix option Documents the new prefix parameter that allows mounting S3 bucket subdirectories. Updates both the API reference and mount-buckets guide with examples and validation requirements. Syncs cloudflare-docs with sandbox-sdk PR #335 --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Matt Silverlock <msilverlock@cloudflare.com>
1 parent 9913223 commit a64f431

2 files changed

Lines changed: 47 additions & 7 deletions

File tree

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ await sandbox.mountBucket('datasets', '/datasets', {
5959
// Mount a subdirectory within the bucket
6060
await sandbox.mountBucket('shared-bucket', '/user-data', {
6161
endpoint: 'https://YOUR_ACCOUNT_ID.r2.cloudflarestorage.com',
62-
prefix: '/users/user-123'
62+
prefix: '/users/user-123/'
6363
});
6464
```
6565
</TypeScriptExample>
@@ -115,7 +115,7 @@ interface MountBucketOptions {
115115
credentials?: BucketCredentials;
116116
readOnly?: boolean;
117117
prefix?: string;
118-
s3fsOptions?: string[];
118+
s3fsOptions?: Record<string, string>;
119119
}
120120
```
121121

@@ -137,12 +137,13 @@ interface MountBucketOptions {
137137
- `readOnly` (optional) - Mount in read-only mode
138138
- Default: `false`
139139

140-
- `prefix` (optional) - Mount a subdirectory within the bucket
141-
- Must start with `/` (e.g., `'/sessions/user123'` or `'/data/uploads/'`)
142-
- Enables multi-tenant isolation within a single bucket
140+
- `prefix` (optional) - Subdirectory within the bucket to mount
141+
- When specified, only contents under this prefix are visible at the mount point
142+
- Must start and end with `/` (e.g., `/data/uploads/`)
143+
- Default: Mount entire bucket
143144

144-
- `s3fsOptions` (optional) - Advanced s3fs mount flags as string array
145-
- Example: `['use_cache=/tmp/cache']`
145+
- `s3fsOptions` (optional) - Advanced s3fs mount flags
146+
- Example: `{ 'use_cache': '/tmp/cache' }`
146147

147148
### `BucketProvider`
148149

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,44 @@ await sandbox.mountBucket('my-r2-bucket', '/data', {
9393
```
9494
</TypeScriptExample>
9595

96+
## Mount bucket subdirectories
97+
98+
Mount a specific subdirectory within a bucket using the `prefix` option. Only contents under the prefix are visible at the mount point:
99+
100+
<TypeScriptExample>
101+
```typescript
102+
// Mount only the /uploads/images/ subdirectory
103+
await sandbox.mountBucket('my-bucket', '/images', {
104+
endpoint: 'https://YOUR_ACCOUNT_ID.r2.cloudflarestorage.com',
105+
prefix: '/uploads/images/'
106+
});
107+
108+
// Files appear at mount point without the prefix
109+
// Bucket: my-bucket/uploads/images/photo.jpg
110+
// Mounted path: /images/photo.jpg
111+
await sandbox.exec('ls', { args: ['/images'] });
112+
113+
// Write to subdirectory
114+
await sandbox.writeFile('/images/photo.jpg', imageData);
115+
// Creates my-bucket:/uploads/images/photo.jpg
116+
117+
// Mount different prefixes to different paths
118+
await sandbox.mountBucket('datasets', '/training-data', {
119+
endpoint: 'https://YOUR_ACCOUNT_ID.r2.cloudflarestorage.com',
120+
prefix: '/ml/training/'
121+
});
122+
123+
await sandbox.mountBucket('datasets', '/test-data', {
124+
endpoint: 'https://YOUR_ACCOUNT_ID.r2.cloudflarestorage.com',
125+
prefix: '/ml/testing/'
126+
});
127+
```
128+
</TypeScriptExample>
129+
130+
:::note[Prefix format]
131+
The `prefix` must start and end with `/` (e.g., `/data/`, `/logs/2024/`). This is required by the underlying s3fs tool.
132+
:::
133+
96134
## Read-only mounts
97135

98136
Protect data by mounting buckets in read-only mode:
@@ -264,6 +302,7 @@ await sandbox.exec('cp', { args: ['/workspace/results.json', '/data/results/outp
264302
- **Use R2 for Cloudflare** - Zero egress fees and optimized configuration
265303
- **Secure credentials** - Always use Worker secrets, never hardcode
266304
- **Read-only when possible** - Protect data with read-only mounts
305+
- **Use prefixes for isolation** - Mount subdirectories when working with specific datasets
267306
- **Mount paths** - Use `/data`, `/storage`, or `/mnt/*` (avoid `/workspace`, `/tmp`)
268307
- **Handle errors** - Wrap mount operations in try/catch blocks
269308
- **Optimize access** - Copy frequently accessed files locally

0 commit comments

Comments
 (0)