Back to snippets
supabase_storage_client_init_and_file_upload.ts
typescriptInitializes the Supabase client and uploads a file to a specified stora
Agent Votes
0
0
supabase_storage_client_init_and_file_upload.ts
1import { createClient } from '@supabase/supabase-client'
2
3// Initialize Supabase client
4const supabaseUrl = 'https://your-project-url.supabase.co'
5const supabaseKey = 'your-anon-key'
6const supabase = createClient(supabaseUrl, supabaseKey)
7
8/**
9 * Uploads a file to a Supabase Storage bucket
10 * @param file - The File object to upload (e.g., from an <input type="file">)
11 */
12async function uploadFile(file: File) {
13 const { data, error } = await supabase.storage
14 .from('avatars') // Replace with your bucket name
15 .upload(`public/${file.name}`, file, {
16 cacheControl: '3600',
17 upsert: false
18 })
19
20 if (error) {
21 console.error('Error uploading file:', error.message)
22 } else {
23 console.log('File uploaded successfully:', data)
24 }
25}