DEV Community

Aaron K Saunders
Aaron K Saunders

Posted on

5 1

Supabase Storage Image Upload Tutorial Using Ionic React & Capacitor Camera

This is a post to support the video I did on the same topic. The video was split into two parts, one covering setting up Ionic Framework in ReactJS to work with the Capacitor Camera Plugin, and the other part was to upload the image captured from the camera to Supabase Storage.

The Video

Uploading the Camera Image

All of the magic happens here

 /**
   * upload to storage bucket, convert path to blob
   * get file name from path
   * 
   * @param path
   */
  const uploadImage = async (path: string) => {
    const response = await fetch(path);
    const blob = await response.blob();

    const filename = path.substr(path.lastIndexOf("/") + 1);

    const { data, error } = await supabase.storage
      .from("image-bucket")
      .upload(`${filename}`, blob, {
        cacheControl: "3600",
        upsert: false,
      });

    if (error) alert(error?.message);

    console.log(data);

    getAllImages();


    return true;
  };
Enter fullscreen mode Exit fullscreen mode

We take the webPath from the Capacitor Camera Plugin and use fetch to get a blob which we then upload to supabase.

Downloading the Image From Supabase

What I do here is create a seperate component RenderImage and in the initial useEffect hook, I make an API call to supabase to get the publicURL of the image and set it to a local state variable, when the variable is set, the image is drawn to the screen.

const RenderImage: React.FC<any> = ({ path }) => {
  const [publicUrl, setPublicUrl] = useState<any>("");
  useEffect(() => {
    (async () => {
      const { publicURL } = supabase.storage
        .from("image-bucket")
        .getPublicUrl(path);

      setPublicUrl(publicURL);
    })();
  },[path]);

  return <IonImg src={publicUrl} />;
};
Enter fullscreen mode Exit fullscreen mode

All The Rest...

the rest of the video is a more detailed explanation of the supabase api and how you need to setup the buckets for use.

Source Code

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay