Files
supabase/apps/docs/content/guides/functions/storage-caching.mdx
Lydia HallieandCharis a26c01217d Update Edge Functions docs (#36798)
* Update Environment Variables docs

* Update Dependencies docs

* Add Function Configuration d ocs

* Add custom Configuration section in Navigation

* Add Error Handling section

* Add new HTTP Methods section

* Update Deno 2 Guide

* Update nav

* Rm deno2 nav

* Update Deploy section

* Update CI/CD section

* Update Logging

* Update Testing

* Update Local Debugging

* Update Troubleshooting

* Update Nav

* Update CLI Quickstart

* Update Dashboard Quickstart

* Update Development Environment

* Update Nav

* Fix links

* Update apps/docs/content/guides/functions/quickstart-dashboard.mdx

Co-authored-by: Charis <26616127+charislam@users.noreply.github.com>

* Update apps/docs/content/guides/functions/development-environment.mdx

Co-authored-by: Charis <26616127+charislam@users.noreply.github.com>

* Update apps/docs/content/guides/functions/development-environment.mdx

Co-authored-by: Charis <26616127+charislam@users.noreply.github.com>

* Update apps/docs/content/guides/functions/quickstart.mdx

Co-authored-by: Charis <26616127+charislam@users.noreply.github.com>

* Use $CodeSample for multi-root example

* Use tocVideo instead of video embed

* Rm typo

* Switch Quickstart ordering

* Rephrase Docker requirement for deployment

* Add Project Structure section

* style: fix docs lint issues

* Update Regional Invocations

* Update Error Handing

* Fix linting

* Fixes

* Remove duplicate sections

* Update Auth integration

* Update Storage

* Update Database/Postgres

* Update Nav

* Update Background Tasks

* Update Ephemeral Storage

* Update Websockets

* Update Routing

* Update wasm

* Update AI models

* Fix linting issues

* Update apps/docs/content/guides/functions/ai-models.mdx

Co-authored-by: Charis <26616127+charislam@users.noreply.github.com>

* Update apps/docs/content/guides/functions/ai-models.mdx

Co-authored-by: Charis <26616127+charislam@users.noreply.github.com>

* Update apps/docs/content/guides/functions/ai-models.mdx

Co-authored-by: Charis <26616127+charislam@users.noreply.github.com>

* Update apps/docs/content/guides/functions/storage-caching.mdx

Co-authored-by: Charis <26616127+charislam@users.noreply.github.com>

* Update apps/docs/content/guides/functions/websockets.mdx

Co-authored-by: Charis <26616127+charislam@users.noreply.github.com>

* Update apps/docs/content/guides/functions/websockets.mdx

Co-authored-by: Charis <26616127+charislam@users.noreply.github.com>

* Update apps/docs/content/guides/functions/ai-models.mdx

Co-authored-by: Charis <26616127+charislam@users.noreply.github.com>

* Rm unnecessary lines

* Rm wrong example

* Fix typo

* Rm deno2

* Move CICD to deployment and rename routing

* Fix whitespace

* rm unnecessary spelling

---------

Co-authored-by: Charis <26616127+charislam@users.noreply.github.com>
2025-07-10 14:18:01 +00:00

98 lines
2.5 KiB
Plaintext

---
id: 'storage-caching'
title: 'Integrating with Supabase Storage'
description: 'Integrate Edge Functions with Supabase Storage.'
tocVideo: 'wW6L52v9Ldo'
---
Edge Functions work seamlessly with [Supabase Storage](/docs/guides/storage). This allows you to:
- Upload generated content directly from your functions
- Implement cache-first patterns for better performance
- Serve files with built-in CDN capabilities
---
## Basic file operations
Use the Supabase client to upload files directly from your Edge Functions. You'll need the service role key for server-side storage operations:
```typescript
import { createClient } from 'npm:@supabase/supabase-js@2'
Deno.serve(async (req) => {
const supabaseAdmin = createClient(
Deno.env.get('SUPABASE_URL') ?? '',
Deno.env.get('SUPABASE_SERVICE_ROLE_KEY') ?? ''
)
// Generate your content
const fileContent = await generateImage()
// Upload to storage
const { data, error } = await supabaseAdmin.storage
.from('images')
.upload(`generated/${filename}.png`, fileContent.body!, {
contentType: 'image/png',
cacheControl: '3600',
upsert: false,
})
if (error) {
throw error
}
return new Response(JSON.stringify({ path: data.path }))
})
```
<Admonition type="caution">
Always use the `SUPABASE_SERVICE_ROLE_KEY` for server-side operations. Never expose this key in client-side code!
</Admonition>
---
## Cache-first pattern
Check storage before generating new content to improve performance:
```typescript
const STORAGE_URL = 'https://your-project.supabase.co/storage/v1/object/public/images'
Deno.serve(async (req) => {
const url = new URL(req.url)
const username = url.searchParams.get('username')
try {
// Try to get existing file from storage first
const storageResponse = await fetch(`${STORAGE_URL}/avatars/${username}.png`)
if (storageResponse.ok) {
// File exists in storage, return it directly
return storageResponse
}
// File doesn't exist, generate it
const generatedImage = await generateAvatar(username)
// Upload to storage for future requests
const { error } = await supabaseAdmin.storage
.from('images')
.upload(`avatars/${username}.png`, generatedImage.body!, {
contentType: 'image/png',
cacheControl: '86400', // Cache for 24 hours
})
if (error) {
console.error('Upload failed:', error)
}
return generatedImage
} catch (error) {
return new Response('Error processing request', { status: 500 })
}
})
```