Search code examples
javascriptreactjstypescriptsupabasereact-tsx

To download an Image from an URL using Javascript and then publish it to Supabase Storage


I am interested to download an image from an URL for instance "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQo2zi7aWGrKd8eTtROEbeG4noyYyEicLaqpMOmqXXN&s%22" and then pass it to something like a s3 bucket. Previously I have tried to fetch it, make a blob object, and then create a URL for it. But that dint seem to work. Here is the code of what I have done so far and it ends up creating an URL however it does not pass through the supabase client

      console.log(await req);
      const imgBlob = await req.blob();
      const imgUrl = URL.createObjectURL(imgBlob);
      const Imagefile = new File([imgUrl],"imageGenerator.png",{type:"image/png"});
      const d = new Date();
      const time = d.getTime();
      console.log(imgBlob);
      console.log(Imagefile);
      const {data,error} = await supabase.storage.from('ama').upload(`${time}`,Imagefile,{cacheControl:'3600',upsert: false})``` 
.

Solution

  • You should be just able to upload an blob like this:

    const imgBlob = await req.blob();
    const d = new Date();
    const time = d.getTime();
    const {data,error} = await supabase.storage.from('ama').upload(`${time}`,imgBlob,{cacheControl:'3600',upsert: false})