|
| 1 | +--- |
| 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' |
| 6 | +--- |
| 7 | + |
| 8 | +Edge Functions provides ephemeral file storage. You can read and write files to the `/tmp` directory. |
| 9 | + |
| 10 | +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. |
| 11 | + |
| 12 | +### Use cases |
| 13 | + |
| 14 | +Here are some use cases where ephemeral storage can be useful: |
| 15 | + |
| 16 | +- Unzip an archive of CSVs and then add them as records to the DB |
| 17 | +- Custom image manipulation workflows (using [MagickWasm](https://supabase.com/docs/guides/functions/examples/image-manipulation)) |
| 18 | + |
| 19 | +You can use [Background Tasks](https://supabase.com/docs/guides/functions/background-tasks) to handle slow file processing outside of a request. |
| 20 | + |
| 21 | +### How to use |
| 22 | + |
| 23 | +You can use [Deno File System APIs](https://docs.deno.com/api/deno/file-system) or the [`node:fs` module](https://docs.deno.com/api/node/fs/) to access the `/tmp` path. |
| 24 | + |
| 25 | +### Example |
| 26 | + |
| 27 | +Here is an example of how to write a user-uploaded zip file into a temporary file for further processing. |
| 28 | + |
| 29 | +```js |
| 30 | +Deno.serve(async (req) => { |
| 31 | + if (req.headers.get('content-type') !== 'application/zip') { |
| 32 | + return new Response('file must be a zip file', { |
| 33 | + status: 400, |
| 34 | + }) |
| 35 | + } |
| 36 | + |
| 37 | + const uploadId = crypto.randomUUID() |
| 38 | + await Deno.writeFile('/tmp/' + uploadId, req.body) |
| 39 | + |
| 40 | + // do something with the written zip file |
| 41 | + |
| 42 | + return new Response('ok') |
| 43 | +}) |
| 44 | +``` |
| 45 | + |
| 46 | +### Unavailable APIs |
| 47 | + |
| 48 | +Currently, the synchronous APIs (eg: `Deno.writeFileSync` or `Deno.mkdirSync`) for creating or writing files are not supported. |
| 49 | + |
| 50 | +You can use sync variations of read APIs (eg: `Deno.readFileSync`). |
| 51 | + |
| 52 | +### Limits |
| 53 | + |
| 54 | +In the hosted platform, a free project can write up to 256MB of data to ephemeral storage. A paid project can write up to 512MB. |
0 commit comments