How to save a file from Supabase Storage using Node.js (converting blob to file)
Asked Answered
Q

1

5

I am downloading media files (mainly images) from Supabase Storage using from.download() in NodeJS.

How can I save the Blob that is returned as a file on my local hard drive?

Quotha answered 6/12, 2021 at 14:38 Comment(0)
Q
11

Create a buffer from the Blob and save that on your hard drive.

A working example:
(Note: If you are not working with NodeJS ESM modules, you must use require instead of import)

import fs from 'fs'
import { createClient } from '@supabase/supabase-js';

const supabase = createClient(yourSupaseUrl, yourSupabaseKey)

const fileName = 'some-file.png'
const filePath = 'some-folder/' + fileName
const { data, error } = await supabase.storage
        .from('storage-name')
        .download(filePath)

const blob = data;
const buffer = Buffer.from( await blob.arrayBuffer() );
await fs.promises.writeFile(fileName, buffer);

Links that helped me:

Quotha answered 6/12, 2021 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.