|
1 | 1 | --- |
2 | 2 | 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' |
6 | 6 | --- |
7 | 7 |
|
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: |
9 | 9 |
|
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: |
11 | 14 |
|
12 | | -- Process uploaded files temporarily without permanent storage |
13 | 15 | - Handle complex file transformations and workflows |
| 16 | +- Do data migrations between projects |
| 17 | +- Process user uploaded files and store them |
14 | 18 | - Unzip archives and process contents before saving to database |
15 | 19 |
|
16 | 20 | --- |
17 | 21 |
|
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 |
19 | 50 |
|
20 | 51 | 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. |
21 | 52 |
|
@@ -128,30 +159,41 @@ Deno.serve(async (req) => { |
128 | 159 |
|
129 | 160 | --- |
130 | 161 |
|
131 | | -## Limitations |
| 162 | +## Using synchronous file APIs |
132 | 163 |
|
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_: |
134 | 165 |
|
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 |
139 | 175 |
|
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`. |
146 | 177 |
|
147 | 178 | ```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 | +}) |
150 | 188 | ``` |
151 | 189 |
|
152 | 190 | --- |
153 | 191 |
|
154 | 192 | ## Limits |
155 | 193 |
|
| 194 | +There are no limits on S3 buckets you mount for Persistent storage. |
| 195 | + |
| 196 | +Ephemeral Storage: |
| 197 | + |
156 | 198 | - Free projects: Up to 256MB of ephemeral storage |
157 | 199 | - Paid projects: Up to 512MB of ephemeral storage |
0 commit comments