Skip to content

Instantly share code, notes, and snippets.

@danalloway
Created April 2, 2021 02:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danalloway/d839a6ca18fd8878936c40f8470002cf to your computer and use it in GitHub Desktop.
Save danalloway/d839a6ca18fd8878936c40f8470002cf to your computer and use it in GitHub Desktop.
Supabase Storage - Upload Stream
const fetch = require("node-fetch");
const FormData = require("form-data");
const API_URL = process.env.SUPABASE_URL;
const AUTH_TOKEN = process.env.SUPABASE_KEY;
/**
* @async
* @param {string} bucketId
* @param {string} path
* @param {ReadableStream} stream
* @returns {void}
*/
async function upload(bucketId, path, stream) {
const form = new FormData();
form.append("", stream);
const result = await fetch(
`${API_URL}/storage/v1/object/${bucketId}/${path}`,
{
method: "POST",
body: form,
headers: {
Authorization: `Bearer ${AUTH_TOKEN}`,
apikey: AUTH_TOKEN,
},
}
);
return result
}
// ready, set, go!
upload('YOUR-BUCKET-ID', 'path/to/destination/file.jpg', STREAM);
@danalloway
Copy link
Author

Install dependancies npm install --save node-fetch form-data

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment