Published on

How to upload file using Supabase Storage in Node

Authors

You can use Supabase Storage to store your files as well and they have a flexible API/SDK as well which makes it easier to interact with it.

npm install @supabase/supabase-js

And get the values for SUPABASE_URL and SUPABASE_PRIVATE_KEY from the Supabase dashboard as well.

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

const SUPABASE_URL = process.env.SUPABASE_URL;
const SUPABASE_PRIVATE_KEY = process.env.SUPABASE_PRIVATE_KEY;

const supabaseAdmin = createClient(SUPABASE_URL, SUPABASE_PRIVATE_KEY);

export default supabaseAdmin;
import supabaseAdmin from './supabaseAdminClient';

const uploadFile = async (bucketName, filePath, fileData, options) => {
  const { error } = await supabaseAdmin.storage
    .from(bucketName)
    .upload(filePath, fileData, options);

  if (error) {
    throw error;
  }

  console.log(`File uploaded to ${filePath}`);
};

const bucketName = 'example';
const options = {
  // { contentType: 'text/csv' }
};
const filePath = 'path/to/file';
const fileData = null; // Assign your file data to this variable

uploadFile(bucketName, filePath, fileData, options);

Happy uploading files!