How to download a Supabase private file via REST API?

Hi there,

I’ve got a presigned file from Supabase, and I’m trying to download the file using the REST API, but having no luck

I have tested the presigned URL by copying and pasting in the browser and it works.

I have added the download parameter as can be seen in the attached image but no luck downloading.

I’ve set my Accept header to application/octet-stream.

Any ideas what I’m missing?

Well you actually got the image back, but it is in binary(I guess?), so what you’d need to do is to javascript - Convert octet-stream to image - Stack Overflow.

Why do you even need to download the image? Can’t you get the public URL to it from Supabase storage instead and then “embed” it where you need it?

Yeap, was binary indeed. I sell images/documents, so after purchase the buyer needs to be able to download the image/document. It’s a private object so no public URL, although one could technically use the presigned URL.

Ended up getting it to work with CoPilot. :slight_smile:

Here’s the code for anyone who think it might help

// Access the URL from the global variable
let url = >>enter your presigned URL here<<;

// Create a new URL object
let downloadUrl = new URL(url);

// Append the query parameter to the URL

downloadUrl.searchParams.append('download',>>enter your file name here<<);

// Create a new anchor element
let a = document.createElement('a');

// Set the href of the anchor element to the download URL
a.href = downloadUrl.toString();

// Set the download attribute of the anchor element to the filename
a.download = downloadUrl.pathname.split('/').pop();

// Append the anchor element to the body
document.body.appendChild(a);

// Simulate a click on the anchor element
a.click();

// Remove the anchor element from the body
document.body.removeChild(a);

// Return the download URL
return downloadUrl.toString();
1 Like