From 99a35fc635a2d29981f93a4280a72fb3f881aba8 Mon Sep 17 00:00:00 2001 From: Lakshan Perera Date: Tue, 25 Oct 2022 11:39:32 +1100 Subject: [PATCH 1/3] fix: add an example for reading a file from storage --- .../supabase/functions/read-storage/index.ts | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 examples/edge-functions/supabase/functions/read-storage/index.ts diff --git a/examples/edge-functions/supabase/functions/read-storage/index.ts b/examples/edge-functions/supabase/functions/read-storage/index.ts new file mode 100644 index 00000000000..c2387593550 --- /dev/null +++ b/examples/edge-functions/supabase/functions/read-storage/index.ts @@ -0,0 +1,64 @@ +// Follow this setup guide to integrate the Deno language server with your editor: +// https://deno.land/manual/getting_started/setup_your_environment +// This enables autocomplete, go to definition, etc. + +import { serve } from "https://deno.land/std@0.131.0/http/server.ts" +import { createClient } from 'https://esm.sh/@supabase/supabase-js@2.0.0' + +const corsHeaders = { + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey', +} + +serve(async (req) => { + // read a text file from storage and print its contents + // This is needed if you're planning to invoke your function from a browser. + if (req.method === 'OPTIONS') { + return new Response('ok', { headers: corsHeaders }) + } + + try { + // Create a Supabase client with the Auth context of the logged in user. + const supabaseClient = createClient( + // Supabase API URL - env var exported by default. + Deno.env.get('SUPABASE_URL') ?? '', + // Supabase API ANON KEY - env var exported by default. + Deno.env.get('SUPABASE_ANON_KEY') ?? '', + // Create client with Auth context of the user that called the function. + // This way your row-level-security (RLS) policies are applied. + { global: { headers: { Authorization: req.headers.get('Authorization')! } } } + ) + + const { data, error } = await supabaseClient + .storage + .from('my-bucket') + .download('sample.txt') + if (error) throw error + + // file contents are returned as a blob, we can convert it to utf-8 text by calling text() method. + const contents = await data.text(); + + // prints out the contents of the file + console.log(contents) + + return new Response(JSON.stringify({ contents }), { + headers: { ...corsHeaders, 'Content-Type': 'application/json' }, + status: 200, + }) + + } catch (error) { + console.error(error); + + return new Response(JSON.stringify({ error: error.message }), { + headers: { ...corsHeaders, 'Content-Type': 'application/json' }, + status: 400, + }) + } + +}) + +// To invoke: +// curl -i --location --request POST 'http://localhost:54321/functions/v1/' \ +// --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24ifQ.625_WdcF3KHqz5amU0x2X5WWHP-OEs_4qj0ssLNHzTs' \ +// --header 'Content-Type: application/json' \ +// --data '{"name":"Functions"}' From 7f59460573dd36695545c357f2f13ccd33a06e44 Mon Sep 17 00:00:00 2001 From: Lakshan Perera Date: Tue, 25 Oct 2022 15:11:55 +1100 Subject: [PATCH 2/3] fix: function name --- .../edge-functions/supabase/functions/read-storage/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/edge-functions/supabase/functions/read-storage/index.ts b/examples/edge-functions/supabase/functions/read-storage/index.ts index c2387593550..c5eb049679c 100644 --- a/examples/edge-functions/supabase/functions/read-storage/index.ts +++ b/examples/edge-functions/supabase/functions/read-storage/index.ts @@ -58,7 +58,7 @@ serve(async (req) => { }) // To invoke: -// curl -i --location --request POST 'http://localhost:54321/functions/v1/' \ +// curl -i --location --request POST 'http://localhost:54321/functions/v1/read-storage' \ // --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24ifQ.625_WdcF3KHqz5amU0x2X5WWHP-OEs_4qj0ssLNHzTs' \ // --header 'Content-Type: application/json' \ // --data '{"name":"Functions"}' From bb1d135a78e0a96e4b2d93f0e306be42a9bcc9ce Mon Sep 17 00:00:00 2001 From: Lakshan Perera Date: Tue, 25 Oct 2022 15:23:35 +1100 Subject: [PATCH 3/3] fix: remove invoke comment --- .../edge-functions/supabase/functions/read-storage/index.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/examples/edge-functions/supabase/functions/read-storage/index.ts b/examples/edge-functions/supabase/functions/read-storage/index.ts index c5eb049679c..b1fbbf96ba3 100644 --- a/examples/edge-functions/supabase/functions/read-storage/index.ts +++ b/examples/edge-functions/supabase/functions/read-storage/index.ts @@ -56,9 +56,3 @@ serve(async (req) => { } }) - -// To invoke: -// curl -i --location --request POST 'http://localhost:54321/functions/v1/read-storage' \ -// --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24ifQ.625_WdcF3KHqz5amU0x2X5WWHP-OEs_4qj0ssLNHzTs' \ -// --header 'Content-Type: application/json' \ -// --data '{"name":"Functions"}'