Chrysolites Tech Academy

Photo Social App with FlutterFlow and Supabase · Course book

Chapter 9: Storing Images in Buckets

Pairs with Lesson 9: Supabase Storage Buckets

Tables are perfect for structured data such as captions, ids and timestamps, but they are the wrong place for photos, videos and audio. Those are unstructured files, large and opaque to the database, and they belong in a file store. Supabase provides one called Storage, organised into buckets. This chapter explains what buckets are, how they relate to tables, how to create one for post images, how storage policies control access, why an "allow all" policy is fine for a first test but not for a real app, how files are named and addressed, and how much storage and bandwidth image sizes really cost, worked out by hand.

What a bucket is

A bucket is a named container for files, similar to a top-level folder on a cloud drive. Inside it, files can be organised into folders by including slashes in their names, such as posts/7c1e.../1700000000.jpg. Each bucket has its own settings and access rules, so an app typically uses a few buckets for different purposes: one for post images, one for profile photos, perhaps one for chat attachments.

Storage and tables work together. The file lives in the bucket; the table row stores its path or its address. When the feed displays a post, it reads the row, finds the image address and loads the image from storage. This split keeps the database small and fast, lets the storage service deliver files efficiently, often through a content delivery network close to the user, and lets each part be secured with rules suited to it. Storing the path, rather than a complete address, is often the more flexible choice, because the full address can always be rebuilt from the path, whereas an address saved in full breaks if the project or bucket is ever moved.

Bucket settingWhat it controlsSensible choice for post images
Public or privateWhether files can be read without a signed addressPublic, for a public feed
File size limitThe largest file that may be uploadedA few megabytes, enough for a compressed photo
Allowed file typesWhich MIME types are acceptedimage/jpeg, image/png, image/webp
PoliciesWho may upload, update, delete or list filesSigned-in users may upload to their own folder

Creating a bucket

In the Supabase dashboard, the Storage section lists buckets and has a button to create one. You give it a name, such as posts, choose whether it is public, and optionally set a size limit and allowed file types. Setting limits at the bucket level is a cheap safeguard: even if the app's checks are bypassed, the storage service refuses a 500 MB video uploaded to an image bucket.

A public bucket means anyone with a file's address can view it, and every file has a permanent address built from the project's address, the bucket name and the file path. That suits images in a public social feed, and permanent addresses can be cached by phones and networks, so images load instantly the second time. A private bucket requires a signed URL, an address with a signature and an expiry time, generated on request; that suits content only certain users should see. Being public applies only to reading: uploading, replacing and deleting are still governed by policies.

Policies and the "allow all" shortcut

Storage access is controlled by policies, rules written in the same style as the row level security policies on tables. A policy names an operation, such as select for reading or listing, insert for uploading, update or delete, and a condition that must be true. To get started quickly, the series first adds an allow all policy to the bucket, which permits every operation for everyone. That makes the first upload test work without any configuration problems.

An allow-all policy is a development convenience only. Left in place, it would let anyone upload files of any kind into your bucket, overwrite other people's images or delete them. Before the app is used by real people, it should be replaced with policies that express the actual rules:

  • Anyone may read files in the posts bucket, since the feed is public, which a public bucket already provides.
  • Signed-in users may upload only into a folder named with their own user id, checked by comparing the first folder in the path with auth.uid().
  • Users may update or delete only files in their own folder.

Organising files by user id therefore does double duty: it keeps each user's files together and makes these rules simple to write. A file name made from a timestamp or a random id ensures that two uploads never collide, even from the same user in the same second.

What images really cost

Storage and bandwidth are the main running costs of a photo app, and both depend directly on image size. A modern phone photo straight from the camera is often 3 MB or more. The feed displays images at about 1,080 pixels wide, so storing and sending the full original wastes most of those bytes.

Worked example. An original photo of 3.2 MB is resized to 1,080 × 1,350 pixels and compressed, giving a file of about 300 KB, a saving of (3,200 − 300) / 3,200 = 90.6 percent. Suppose 1,000 posts are each viewed 50 times in a month, 50,000 image loads in total. With originals, that is 50,000 × 3.2 MB = 160,000 MB, about 156 GB of bandwidth; with compressed images it is 50,000 × 0.3 MB = 15,000 MB, about 14.6 GB. Storage for the 1,000 images falls from 3.2 GB to 0.3 GB. The ratio is the same at any scale: compression cuts both costs by about ten times.
156 GB14.6 GB original 3.2 MB photoscompressed 300 KB 080160 GB per month
Figure 9.1 — Bandwidth for 50,000 image views in a month. Resizing and compressing before upload cuts it by roughly ten times.

Compression is best done on the phone, before upload, so that the large original never leaves the device. FlutterFlow's image picker can limit the maximum width and height and the quality of the result, which achieves most of the saving with no extra code. Modern formats such as WebP are typically smaller than JPEG at similar quality, though JPEG remains the most universally supported.

Two further techniques reduce costs once an app grows. Caching headers, set when a file is uploaded, tell phones and networks how long they may keep a copy; for an image that never changes, a long cache lifetime means a returning user loads it from their own device rather than downloading it again. And image transformations, which Supabase offers on some plans, resize images on request: the grid can ask for a 300-pixel version and the detail page for a 1,080-pixel version of the same stored file, so each screen downloads only what it needs. Both work best when file names change whenever content changes, so that caches never serve an outdated picture.

Uploading itself has a few details worth getting right. Each upload should state the file's content type, such as image/jpeg, so that browsers and apps display it correctly. The upload call can be told whether to replace an existing file with the same name, an option often called upsert; for post images, refusing to overwrite is safer, since each post should have its own file. And an upload that succeeds but whose database insert then fails leaves an orphaned file nobody can see, so the app should either retry the insert or delete the file, keeping the bucket tidy.

Bucket design for the whole app

Looking ahead, the app's buckets might be arranged as follows. A posts bucket, public, holds post images in folders by user id. An avatars bucket, public, holds profile photos, each saved under a new name when changed so that cached old pictures do not linger. Any private content, such as images sent in chat, would go into a private bucket with signed addresses and policies that let only the chat's members read it. Writing these decisions down, with each bucket's policies, makes the system easy to understand and to audit before release, and easy to explain to anyone who joins the project later.

With a bucket in place, the next part connects the add-post pages to storage and the posts table: the user picks an image, the app uploads it to the bucket, receives its address and inserts a row into the posts table with the address and caption, and the new post appears in the feed for everyone to see.

Practice tasks

  1. A 4.5 MB photo is compressed to 350 KB. What percentage is saved?
  2. Compute monthly bandwidth for 2,000 posts viewed 30 times each, at 3.2 MB and at 0.3 MB per image.
  3. Write, in words, the upload policy that lets users upload only into their own folder.
  4. Explain why public buckets suit post images in a public feed, and what remains controlled by policies.
  5. Why should the bucket itself have a file size limit and allowed file types, even if the app checks them?
  6. List the buckets you would create for this app, and whether each should be public or private.

Install Chrysolites Tech Academy

Android / Chrome / Edge: open the browser menu and choose Install app or Add to Home screen.

iPhone / iPad (Safari): tap the Share button, then Add to Home Screen.