Created
April 2, 2021 02:57
-
-
Save danalloway/d839a6ca18fd8878936c40f8470002cf to your computer and use it in GitHub Desktop.
Supabase Storage - Upload Stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install dependancies
npm install --save node-fetch form-data