Integrating with Supabase Storage
A guide from Supabase
Apps on Plentiful run on Supabase's open-source database, sign-in, file storage and live updates, so most of this guide applies. The differences: your app reaches its database through its own server — not Supabase's data API (supabase.from(…)) — there are no Edge Functions (your app's server does that work), and scheduled work uses Jobs. Where it says "Supabase dashboard", use your app's Dashboard. Ask the builder when in doubt.
Edge Functions work seamlessly with Supabase 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 secret key for server-side storage operations:
1import { createClient } from 'npm:@supabase/supabase-js@2'23const SUPABASE_SECRET_KEYS = JSON.parse(Deno.env.get('SUPABASE_SECRET_KEYS')!)45Deno.serve(async (req) => {6 const supabaseAdmin = createClient(7 Deno.env.get('SUPABASE_URL')!,8 // If you want to use a different api key, change 'default' to your preferred key name9 SUPABASE_SECRET_KEYS['default']10 )1112 // Generate your content13 const fileContent = await generateImage()1415 // Upload to storage16 const { data, error } = await supabaseAdmin.storage17 .from('images')18 .upload(`generated/${filename}.png`, fileContent.body!, {19 contentType: 'image/png',20 cacheControl: '3600',21 upsert: false,22 })2324 if (error) {25 throw error26 }2728 return new Response(JSON.stringify({ path: data.path }))29})Always use one of the SUPABASE_SECRET_KEYS for server-side operations. Never expose this key in client-side code!
Cache-first pattern#
Check storage before generating new content to improve performance:
1const STORAGE_URL = 'https://your-project.supabase.co/storage/v1/object/public/images'23Deno.serve(async (req) => {4 const url = new URL(req.url)5 const username = url.searchParams.get('username')67 try {8 // Try to get existing file from storage first9 const storageResponse = await fetch(`${STORAGE_URL}/avatars/${username}.png`)1011 if (storageResponse.ok) {12 // File exists in storage, return it directly13 return storageResponse14 }1516 // File doesn't exist, generate it17 const generatedImage = await generateAvatar(username)1819 // Upload to storage for future requests20 const { error } = await supabaseAdmin.storage21 .from('images')22 .upload(`avatars/${username}.png`, generatedImage.body!, {23 contentType: 'image/png',24 cacheControl: '86400', // Cache for 24 hours25 })2627 if (error) {28 console.error('Upload failed:', error)29 }3031 return generatedImage32 } catch (error) {33 return new Response('Error processing request', { status: 500 })34 }35})