Skip to content
Edge Functions

与 Supabase 存储整合

Edge Functions 可以无缝配合 Supabase Storage 使用。这让你能够:

🌐 Edge Functions work seamlessly with Supabase Storage. This allows you to:

  • 直接上传你职能生成的内容
  • 为了更好的性能,优先使用缓存模式
  • 使用内置 CDN 功能提供文件

基础文件操作 #

🌐 Basic file operations

使用 Supabase 客户端可以直接从你的 Edge Functions 上传文件。你将需要服务器端存储操作的密钥:

🌐 Use the Supabase client to upload files directly from your Edge Functions. You'll need the secret key for server-side storage operations:

1
import { createClient } from 'npm:@supabase/supabase-js@2'
2
3
const SUPABASE_SECRET_KEYS = JSON.parse(Deno.env.get('SUPABASE_SECRET_KEYS')!)
4
5
Deno.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 name
9
SUPABASE_SECRET_KEYS['default']
10
)
11
12
// Generate your content
13
const fileContent = await generateImage()
14
15
// Upload to storage
16
const { data, error } = await supabaseAdmin.storage
17
.from('images')
18
.upload(`generated/${filename}.png`, fileContent.body!, {
19
contentType: 'image/png',
20
cacheControl: '3600',
21
upsert: false,
22
})
23
24
if (error) {
25
throw error
26
}
27
28
return new Response(JSON.stringify({ path: data.path }))
29
})

优先缓存模式 #

🌐 Cache-first pattern

在生成新内容前检查存储,以提高性能:

🌐 Check storage before generating new content to improve performance:

1
const STORAGE_URL = 'https://your-project.supabase.co/storage/v1/object/public/images'
2
3
Deno.serve(async (req) => {
4
const url = new URL(req.url)
5
const username = url.searchParams.get('username')
6
7
try {
8
// Try to get existing file from storage first
9
const storageResponse = await fetch(`${STORAGE_URL}/avatars/${username}.png`)
10
11
if (storageResponse.ok) {
12
// File exists in storage, return it directly
13
return storageResponse
14
}
15
16
// File doesn't exist, generate it
17
const generatedImage = await generateAvatar(username)
18
19
// Upload to storage for future requests
20
const { error } = await supabaseAdmin.storage
21
.from('images')
22
.upload(`avatars/${username}.png`, generatedImage.body!, {
23
contentType: 'image/png',
24
cacheControl: '86400', // Cache for 24 hours
25
})
26
27
if (error) {
28
console.error('Upload failed:', error)
29
}
30
31
return generatedImage
32
} catch (error) {
33
return new Response('Error processing request', { status: 500 })
34
}
35
})