Files
supabase/apps/docs/content/guides/functions/ephemeral-storage.mdx
supabase-vercel-tedd[bot] 9298ed68c1 docs(functions): list all supported sync Deno APIs and cover self-hosted workers (#53971)
## Problem

The list of synchronous Deno APIs on the Edge Functions ephemeral
storage page leaves out three APIs that the runtime allows during
initial script evaluation. The self-hosted Functions guide doesn't say
whether these limits apply to self-hosted functions, or where to find
them.

Related to
[FDBKIN-8800](https://linear.app/supabase/issue/FDBKIN-8800/document-which-deno-sync-functions-are-supported-in-self-hosted-edge)
Closes
[DOCS-1762](https://linear.app/supabase/issue/DOCS-1762/list-every-supported-sync-deno-api-and-say-they-apply-to-self-hosted)

## Solution

The ephemeral storage page now lists all 12 sync APIs that edge-runtime
allows during initial script evaluation, adding `Deno.lstatSync`,
`Deno.makeTempFileSync`, and `Deno.copyFileSync`. The self-hosted
Functions guide gets a short "Synchronous file APIs" section: user
workers get the same init-only rule and the `blocklisted on the current
context` error, and the main worker router isn't restricted. It links to
the list instead of repeating it.

## How to verify

1. On `/docs/guides/functions/ephemeral-storage`, the "Using synchronous
file APIs" list shows 12 APIs, including `Deno.lstatSync`,
`Deno.makeTempFileSync`, and `Deno.copyFileSync`.
2. On `/docs/guides/self-hosting/self-hosted-functions`, a "Synchronous
file APIs" section says user-worker functions can use these APIs only
during initial script evaluation, names the error thrown otherwise, and
says the main worker isn't restricted.
3. The link in that section opens the list on the ephemeral storage
page, and the self-hosted page doesn't repeat the list.

## Checks

Ran `pnpm exec oxfmt --check` on both pages. Checked the API list, the
error text, and the `kind === "user"` gate against
`ext/runtime/js/bootstrap.js` in `supabase/edge-runtime` at `v1.79.2`,
the version `docker/docker-compose.yml` pins. Checked that
`docker/volumes/functions/main/index.ts` doesn't set
`useReadSyncFileAPI` when it creates user workers. `pnpm
build:guides-markdown` couldn't run in the sandbox: it needs GitHub
credentials for federated content.

## Checklist

- [x] I have read the [contributing
guidelines](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md).
- [x] The change is docs content only.

## Feedback for Tedd

To ask Tedd for a change to this PR, reply in Tedd's session on
[DOCS-1762](https://linear.app/supabase/issue/DOCS-1762/list-every-supported-sync-deno-api-and-say-they-apply-to-self-hosted).
Tedd doesn't read comments on this PR.

---------

Co-authored-by: supabase-vercel-tedd[bot] <336548405+supabase-vercel-tedd[bot]@users.noreply.github.com>
2026-11-24 13:55:50 -07:00

202 lines
6.5 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
id: 'function-ephemeral-storage'
title: 'File Storage'
description: 'Use persistent and ephemeral file storage'
subtitle: 'Use persistent and ephemeral file storage'
---
Edge Functions provides two flavors of file storage:
- Persistent - backed by S3 protocol, can read/write from any S3 compatible bucket, including Supabase Storage
- Ephemeral - You can read and write files to the `/tmp` directory. Only suitable for temporary operations
You can use file storage to:
- Handle complex file transformations and workflows
- Do data migrations between projects
- Process user uploaded files and store them
- Unzip archives and process contents before saving to database
---
## Persistent Storage
The persistent storage option is built on top of the S3 protocol. It allows you to mount any S3-compatible bucket, including [Supabase Storage](/docs/guides/storage) Buckets, as a directory for your Edge Functions.
You can perform operations such as reading and writing files to the mounted buckets as you would in a POSIX file system.
To access an S3 bucket from Edge Functions, you must set the following for environment variables in Edge Function Secrets.
- `S3FS_ENDPOINT_URL`
- `S3FS_REGION`
- `S3FS_ACCESS_KEY_ID`
- `S3FS_SECRET_ACCESS_KEY`
[Follow this guide](/docs/guides/storage/s3/authentication) to enable and create an access key for Supabase Storage S3.
To access a file path in your mounted bucket from your Edge Function, use the prefix `/s3/YOUR-BUCKET-NAME`.
```tsx
// read from S3 bucket
const data = await Deno.readFile('/s3/my-bucket/results.csv')
// make a directory
await Deno.mkdir('/s3/my-bucket/sub-dir')
// write to S3 bucket
await Deno.writeTextFile('/s3/my-bucket/demo.txt', 'hello world')
```
## Ephemeral storage
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.
You can use [Deno File System APIs](https://docs.deno.com/api/deno/file-system/) or the [`node:fs`](https://docs.deno.com/api/node/fs/) module to access the `/tmp` path.
```tsx
Deno.serve(async (req) => {
if (req.headers.get('content-type') !== 'application/zip') {
return new Response('file must be a zip file', {
status: 400,
})
}
const uploadId = crypto.randomUUID()
await Deno.writeFile('/tmp/' + uploadId, req.body)
// E.g. extract and process the zip file
const zipFile = await Deno.readFile('/tmp/' + uploadId)
// You could use a zip library to extract contents
const extracted = await extractZip(zipFile)
// Or process the file directly
console.log(`Processing zip file: ${uploadId}, size: ${zipFile.length} bytes`)
})
```
---
## Common use cases
### Archive processing with background tasks
You can use ephemeral storage with [Background Tasks](/docs/guides/functions/background-tasks) to handle large file processing operations that exceed memory limits.
Imagine you have a Photo Album application that accepts photo uploads as zip files. A streaming implementation will run into memory limit errors with zip files exceeding 100MB, as it retains all archive files in memory simultaneously.
You can write the zip file to ephemeral storage first, then use a background task to extract and upload files to [Supabase Storage](/docs/guides/storage/quickstart). This way, you only read parts of the zip file to the memory.
```tsx
import { BlobWriter, ZipReader } from 'https://deno.land/x/zipjs/index.js'
import { createClient } from 'jsr:@supabase/supabase-js@2'
const SUPABASE_SECRET_KEYS = JSON.parse(Deno.env.get('SUPABASE_SECRET_KEYS')!)
// If you want to use a different api key, change 'default' to your preferred key name
const supabase = createClient(Deno.env.get('SUPABASE_URL')!, SUPABASE_SECRET_KEYS['default'])
async function processZipFile(uploadId: string, filepath: string) {
const file = await Deno.open(filepath, { read: true })
const zipReader = new ZipReader(file.readable)
const entries = await zipReader.getEntries()
await supabase.storage.createBucket(uploadId, { public: false })
await Promise.all(
entries.map(async (entry) => {
if (entry.directory) return
// Read file entry from temp storage
const blobWriter = new BlobWriter()
const blob = await entry.getData(blobWriter)
// Upload to permanent storage
await supabase.storage.from(uploadId).upload(entry.filename, blob)
console.log('uploaded', entry.filename)
})
)
await zipReader.close()
}
Deno.serve(async (req) => {
const uploadId = crypto.randomUUID()
const filepath = `/tmp/${uploadId}.zip`
// Write zip to ephemeral storage
await Deno.writeFile(filepath, req.body)
// Process in background to avoid memory limits
EdgeRuntime.waitUntil(processZipFile(uploadId, filepath))
return new Response(JSON.stringify({ uploadId }), {
headers: { 'Content-Type': 'application/json' },
})
})
```
### Image manipulation
Custom image manipulation workflows using [`magick-wasm`](/docs/guides/functions/examples/image-manipulation).
```tsx
Deno.serve(async (req) => {
// Save uploaded image to temp storage
const imagePath = `/tmp/input-${crypto.randomUUID()}.jpg`
await Deno.writeFile(imagePath, req.body)
// Process image with magick-wasm
const processedPath = `/tmp/output-${crypto.randomUUID()}.jpg`
// ... image manipulation logic
// Read processed image and return
const processedImage = await Deno.readFile(processedPath)
return new Response(processedImage, {
headers: { 'Content-Type': 'image/jpeg' },
})
})
```
---
## Using synchronous file APIs
You can safely use the following synchronous Deno APIs (and their Node counterparts) _during initial script evaluation_:
- Deno.lstatSync
- Deno.statSync
- Deno.removeSync
- Deno.writeFileSync
- Deno.writeTextFileSync
- Deno.readFileSync
- Deno.readTextFileSync
- Deno.mkdirSync
- Deno.makeTempDirSync
- Deno.makeTempFileSync
- Deno.readDirSync
- Deno.copyFileSync
**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`.
```tsx
Deno.statSync('...') // ✅
setTimeout(() => {
Deno.statSync('...') // 💣 PermissionDenied: Deno.statSync is blocklisted on the current context
})
Deno.serve(() => {
Deno.statSync('...') // 💣 PermissionDenied: Deno.statSync is blocklisted on the current context
})
```
---
## Limits
There are no limits on S3 buckets you mount for Persistent storage.
Ephemeral Storage:
- Free projects: Up to 256MB of ephemeral storage
- Paid projects: Up to 512MB of ephemeral storage