Skip to content

Commit 49ba13f

Browse files
laktekw3b6x9charislam
authored
chore: update file storage guide (supabase#37268)
* chore: update file storage guide * doc lints * prettier * Update apps/docs/content/guides/functions/ephemeral-storage.mdx Co-authored-by: Charis <[email protected]> --------- Co-authored-by: Wen Bo Xie <[email protected]> Co-authored-by: Charis <[email protected]>
1 parent 68fe370 commit 49ba13f

File tree

2 files changed

+64
-22
lines changed

2 files changed

+64
-22
lines changed

apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,7 @@ export const functions: NavMenuConstant = {
14431443
url: undefined,
14441444
items: [
14451445
{ name: 'Background Tasks', url: '/guides/functions/background-tasks' },
1446-
{ name: 'Ephemeral Storage', url: '/guides/functions/ephemeral-storage' },
1446+
{ name: 'File Storage', url: '/guides/functions/ephemeral-storage' },
14471447
{ name: 'WebSockets', url: '/guides/functions/websockets' },
14481448
{ name: 'Custom Routing', url: '/guides/functions/routing' },
14491449
{ name: 'Wasm Modules', url: '/guides/functions/wasm' },

apps/docs/content/guides/functions/ephemeral-storage.mdx

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,52 @@
11
---
22
id: 'function-ephemeral-storage'
3-
title: 'Ephemeral Storage'
4-
description: 'Read and write from temporary directory'
5-
subtitle: 'Read and write from temporary directory'
3+
title: 'File Storage'
4+
description: 'Use persistent and ephemeral file storage'
5+
subtitle: 'Use persistent and ephemeral file storage'
66
---
77

8-
Edge Functions provides ephemeral file storage. You can read and write files to the `/tmp` directory.
8+
Edge Functions provides two flavors of file storage:
99

10-
This allows you to:
10+
- Persistent - backed by S3 protocol, can read/write from any S3 compatible bucket, including Supabase Storage
11+
- Ephemeral - You can read and write files to the `/tmp` directory. Only suitable for temporary operations
12+
13+
You can use file storage to:
1114

12-
- Process uploaded files temporarily without permanent storage
1315
- Handle complex file transformations and workflows
16+
- Do data migrations between projects
17+
- Process user uploaded files and store them
1418
- Unzip archives and process contents before saving to database
1519

1620
---
1721

18-
## Overview
22+
## Persistent Storage
23+
24+
The persistent storage option is built on top of the S3 protocol. It allows you to mount any S3-compatible bucket, including Supabase Storage Buckets, as a directory for your Edge Functions.
25+
You can perform operations such as reading and writing files to the mounted buckets as you would in a POSIX file system.
26+
27+
To access an S3 bucket from Edge Functions, you must set the following for environment variables in Edge Function Secrets.
28+
29+
- `S3FS_ENDPOINT_URL`
30+
- `S3FS_REGION`
31+
- `S3FS_ACCESS_KEY_ID`
32+
- `S3FS_SECRET_ACCESS_KEY`
33+
34+
[Follow this guide](https://supabase.com/docs/guides/storage/s3/authentication) to enable and create an access key for Supabase Storage S3.
35+
36+
To access a file path in your mounted bucket from your Edge Function, use the prefix `/s3/YOUR-BUCKET-NAME`.
37+
38+
```tsx
39+
// read from S3 bucket
40+
const data = await Deno.readFile('/s3/my-bucket/results.csv')
41+
42+
// make a directory
43+
await Deno.mkdir('/s3/my-bucket/sub-dir')
44+
45+
// write to S3 bucket
46+
await Deno.writeTextFile('/s3/my-bucket/demo.txt', 'hello world')
47+
```
48+
49+
## Ephemeral storage
1950

2051
Ephemeral storage will reset on each function invocation. This means the files you write during an invocation can only be read within the same invocation.
2152

@@ -128,30 +159,41 @@ Deno.serve(async (req) => {
128159

129160
---
130161

131-
## Limitations
162+
## Using synchronous file APIs
132163

133-
Currently, the synchronous APIs for creating or writing files are not supported:
164+
You can safely use the following synchronous Deno APIs (and their Node counterparts) _during initial script evaluation_:
134165

135-
```tsx
136-
// ❌ Not supported
137-
Deno.writeFileSync('/tmp/file.txt', data)
138-
Deno.mkdirSync('/tmp/directory')
166+
- Deno.statSync
167+
- Deno.removeSync
168+
- Deno.writeFileSync
169+
- Deno.writeTextFileSync
170+
- Deno.readFileSync
171+
- Deno.readTextFileSync
172+
- Deno.mkdirSync
173+
- Deno.makeTempDirSync
174+
- Deno.readDirSync
139175

140-
// ✅ Supported
141-
await Deno.writeFile('/tmp/file.txt', data)
142-
await Deno.mkdir('/tmp/directory')
143-
```
144-
145-
You can use sync variations of read APIs:
176+
**Keep in mind** that the sync APIs are available only during initial script evaluation and aren’t supported in callbacks like HTTP handlers or `setTimeout`.
146177

147178
```tsx
148-
// ✅ Supported
149-
const data = Deno.readFileSync('/tmp/file.txt')
179+
Deno.statSync('...') //
180+
181+
setTimeout(() => {
182+
Deno.statSync('...') // 💣 ERROR! Deno.statSync is blocklisted on the current context
183+
})
184+
185+
Deno.serve(() => {
186+
Deno.statSync('...') // 💣 ERROR! Deno.statSync is blocklisted on the current context
187+
})
150188
```
151189

152190
---
153191

154192
## Limits
155193

194+
There are no limits on S3 buckets you mount for Persistent storage.
195+
196+
Ephemeral Storage:
197+
156198
- Free projects: Up to 256MB of ephemeral storage
157199
- Paid projects: Up to 512MB of ephemeral storage

0 commit comments

Comments
 (0)