question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Uploading image from online link to supabase always returns application/octet-stream

See original GitHub issue

I tried to upload a file from a URL to Supabase storage. I always get an object with application/octet-stream mime type. It only happens on Cloudflare workers. I tried it on a normal server and it works well. Here is my code:

const { createClient } = require('@supabase/supabase-js')

const supabase = createClient('SUPABASE_URL', 'SUPABASE_ANON_KEY')

async function upload (url) {
    const blob = await fetch(url)
        .then(response => response.blob())
    const fileName = 'test.' +blob.type.split('/')[1]
    const result = await supabase
      .storage
      .from('avatars')
      .upload(fileName, blob, {
        cacheControl: '3600',
        contentType: blob.type,
        upsert: true,
      })
      console.log(result)
}

upload('https://tinypng.com/images/panda-happy-2x.png')
image

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:14 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
mrbbotcommented, May 24, 2022

Did some digging, this is the code in @supabase/supabase-js that does the uploading: https://github.com/supabase/storage-js/blob/1a7d98c3e8f90c43ab7c7c2b52a34f99bb0736b7/src/lib/StorageFileApi.ts#L74-L76. Looks like undici’s FormData class discards Blob types when appending to FormData:

// Local
import { FormData } from "undici";

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

const formData = new FormData();
formData.append("file", blob);

const formDataEdBlob = formData.get("file");
console.log(formDataEdBlob.type); // ""
// Workers Runtime (and also Firefox)
const blob = new Blob(["test"], { type: "text/plain" });

const formData = new FormData();
formData.append("file", blob);

const formDataEdBlob = formData.get("file");
console.log(formDataEdBlob.type); // "text/plain"

See the corresponding undici code here: https://github.com/nodejs/undici/blob/4684a1543d87e98b441959d731a3a13d20eaa17d/lib/fetch/formdata.js#L244-L248. Probably worth an issue in undici?


A potential temporary solution though is to pass a non-Blob/File type to upload, triggering this code path instead: https://github.com/supabase/storage-js/blob/1a7d98c3e8f90c43ab7c7c2b52a34f99bb0736b7/src/lib/StorageFileApi.ts#L81-L83:

const buffer = await blob.arrayBuffer();
const result = await supabase.storage
  .from("avatars")
  .upload(fileName, buffer, {
    cacheControl: "3600",
    contentType: blob.type,
    upsert: true,
  });
0reactions
petebacondarwincommented, May 26, 2022

Cool. In that case I am going to close this, since there is nothing to track here and you have provided a workaround.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Storage: What is the public URL of the image? #1146 - GitHub
I don't know the public URL after uploading the image. ... base64.replace('data:application/octet-stream;base64' , "data:image/png;base64").
Read more >
Why is my file type "application/octet-stream" when I upload ...
I am doing this through the web. When I download an image and upload it, the image type is set as "application/octet-stream" and...
Read more >
How To Upload Files To Supabase Storage Buckets and Write ...
Overview. Simple application showing file upload and writing database records using Remix and Supabase. We show how Actions and Loaders work ...
Read more >
Images put to storage are saved as 'octet-stream' rather than ...
Results: In Firebase (storage) The image is being saved as application/octet-stream instead of image/jpeg. The image is not shown, it says ...
Read more >
Storage Image Transformations - Supabase
Transform images with Storage. ... This returns the public URL that serves the resized image. supabase.storage.from('bucket').getPublicUrl('image.jpg' ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found