S
Supabase•15h ago
skeddles

upload file to local storage bucket with service key

i cant for the life of me figure out how to upload to storage. it works fine for authenticated users in the client. but im trying to do it from a node.js script with the service key. it seems to be able to download just fine, but can't upload the same file.
if (!URL || !KEY) throw new Error('Missing URL or KEY');

const supabase = createClient(URL, KEY);
const { data, error } = await supabase.storage
.from('portfolio.quest')
.download('index.html');
if (error) console.error('download failed, error: '+ error);
else if (!data) console.error('download failed, no data returned');
else console.log('download succeeded: '+ data);

const blob = new Blob(['poop'], { type: 'text/plain' });
const { dataUpload, errorUpload } = await supabase.storage
.from('portfolio.quest')
.upload('index.html', blob, {contentType: 'text/plain', upsert: true});

if (errorUpload) console.error('upload failed, error: '+ errorUpload);
else if (!dataUpload) console.error('upload failed, no data returned');
else console.log('upload succeeded: '+ dataUpload);
if (!URL || !KEY) throw new Error('Missing URL or KEY');

const supabase = createClient(URL, KEY);
const { data, error } = await supabase.storage
.from('portfolio.quest')
.download('index.html');
if (error) console.error('download failed, error: '+ error);
else if (!data) console.error('download failed, no data returned');
else console.log('download succeeded: '+ data);

const blob = new Blob(['poop'], { type: 'text/plain' });
const { dataUpload, errorUpload } = await supabase.storage
.from('portfolio.quest')
.upload('index.html', blob, {contentType: 'text/plain', upsert: true});

if (errorUpload) console.error('upload failed, error: '+ errorUpload);
else if (!dataUpload) console.error('upload failed, no data returned');
else console.log('upload succeeded: '+ dataUpload);
28 Replies
skeddles
skeddlesOP•15h ago
it fails without giving me an error, it says "upload failed, no data returned" and no file is there when i check the download works though the key im using is the one printed out from the local supabase dev environment under "Secret key", which I believe should have access
Fieryduck82579
Fieryduck82579•12h ago
The upload method doesn't return any properties named dataUpload or errorUpload. It should be:
const {data: dataUpload, error: errorUpload} = await supabase.from().upload()
const {data: dataUpload, error: errorUpload} = await supabase.from().upload()
Or just print the whole object to see what you get back.
const upload = await supabase.from().upload()
console.log(upload)
const upload = await supabase.from().upload()
console.log(upload)
skeddles
skeddlesOP•12h ago
ah good catch, that explains why it wasn't throwing an error even though it clearly wasn't working. this is the actual error:
uploadResult: {
data: null,
error: StorageApiError: Invalid Compact JWS
at Y:\Projects\Websites\portfolio.quest\node_modules\@supabase\storage-js\dist\main\lib\fetch.js:24:20
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
__isStorageError: true,
status: 400,
statusCode: '403'
}
}
uploadResult: {
data: null,
error: StorageApiError: Invalid Compact JWS
at Y:\Projects\Websites\portfolio.quest\node_modules\@supabase\storage-js\dist\main\lib\fetch.js:24:20
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
__isStorageError: true,
status: 400,
statusCode: '403'
}
}
any idea what it means?
Fieryduck82579
Fieryduck82579•12h ago
403 is related to authorization, but this specific error isn't a recognized storage error.
inder
inder•12h ago
What is your cli version?
skeddles
skeddlesOP•12h ago
Supabase CLI 2.48.3
inder
inder•12h ago
Can you try with v2.47.2. I have that installed locally and uploads work with service role key with this. Will test with v2.48.3 in a while
skeddles
skeddlesOP•12h ago
ill try to figure out how to change versions. i forget how i installed it in the first place
inder
inder•12h ago
you can simply use npx
npx supabase@2.47.2 start
npx supabase@2.47.2 start
Works on v2.48.3 too
skeddles
skeddlesOP•12h ago
weirdly my gui shows a different version
No description
inder
inder•12h ago
No description
inder
inder•12h ago
could be browser caching. Try in a private window
skeddles
skeddlesOP•12h ago
launching the different version failed, trying to launch my old version again
inder
inder•12h ago
works on 2.48.3 as well
const serviceRoleKey = "sb_secret_N7UND0UgjKTVK-Uodkm0Hg_xSvEMPvz";

const main = async () => {
const supabase = createClient(url, serviceRoleKey);

const blob = new Blob(["poop"], { type: "text/plain" });

const { data, error } = await supabase.storage
.from("portfolio.quest")
.upload("index.html", blob, { contentType: "text/plain", upsert: true });

if (error) return console.error(error.message);
console.log(data);
};

main();
const serviceRoleKey = "sb_secret_N7UND0UgjKTVK-Uodkm0Hg_xSvEMPvz";

const main = async () => {
const supabase = createClient(url, serviceRoleKey);

const blob = new Blob(["poop"], { type: "text/plain" });

const { data, error } = await supabase.storage
.from("portfolio.quest")
.upload("index.html", blob, { contentType: "text/plain", upsert: true });

if (error) return console.error(error.message);
console.log(data);
};

main();
No description
skeddles
skeddlesOP•11h ago
we'll see what version i have when it actually starts again idk why but its downloading everything again i just used npx supabase start
inder
inder•11h ago
it should use the latest version v2.48.3 if you don't have it installed in your local node_modules folder
skeddles
skeddlesOP•11h ago
version appears to be right now
No description
inder
inder•11h ago
Then previously you must be using the old version Try running your script now
Fieryduck82579
Fieryduck82579•11h ago
I am curious, is the Supabase/docker version linked to the CLI version?
inder
inder•11h ago
The image versions are hardcoded in the cli release. You can only modify the img versions by creating a file in .temp directory
Fieryduck82579
Fieryduck82579•11h ago
Where can I read more about this directory?
inder
inder•11h ago
I don't think its mentioned explicitly in the docs. I read about it in a gh issue https://github.com/supabase/cli/issues/1702#issuecomment-2157545136
Fieryduck82579
Fieryduck82579•11h ago
Awesome! Thank you 🙂
skeddles
skeddlesOP•11h ago
wow, it works perfectly now. i should have tried turning it off and on again :sadd:
No description
skeddles
skeddlesOP•11h ago
thanks for your help inder
inder
inder•11h ago
No you were using the wrong version before The screenshot you shared above was showing 2.45 Maybe there was an issue with that version
skeddles
skeddlesOP•11h ago
weird ¯\_(ツ)_/¯
inder
inder•11h ago
Also if it was 2.48 before, then it wouldn't have downloaded docker images again

Did you find this page helpful?