Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Zip Supabase Storage Downloads: Best Way

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide covers the main approaches for zipping multiple files from Supabase Storage into a single downloadable archive, helping you choose the best method based on file size, number of files, and your serverless environment. You'll learn the trade-offs between client-side and server-side zipping, and get step-by-step instructions for the most practical approaches.

What You'll Need

  • A Supabase project with Storage buckets and files you want to zip.
  • Access to your Supabase project keys (URL and anon/public key, or service role key for server-side operations).
  • For server-side zipping: a Node.js environment (e.g., Supabase Edge Functions, Vercel, Netlify) with npm installed.
  • For client-side zipping: a modern browser (Chrome, Firefox, Safari, Edge) and JavaScript knowledge.

Step-by-Step

Option 1: Client-Side Zipping (Best for Small Sets of Small Files)

  1. Identify the files. List the file paths in your Supabase Storage bucket that you want to include. You can fetch this via the Supabase JS client: supabase.storage.from('bucket').list('folder').
  2. Install a zip library. In your frontend project, install jszip (or similar) via npm/yarn: npm install jszip.
  3. Download each file as a Blob. Use the Supabase client to download each file: const { data, error } = await supabase.storage.from('bucket').download(path). The data is a Blob.
  4. Add files to the ZIP. Create a new JSZip instance, then for each downloaded Blob, add it: zip.file(path, blob).
  5. Generate the ZIP. Call const blob = await zip.generateAsync({ type: 'blob' }) to produce the final ZIP file as a Blob.
  6. Trigger download. Create an object URL and click a temporary link: const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = 'files.zip'; a.click(); URL.revokeObjectURL(url);.

Option 2: Server-Side Zipping with Edge Functions (Best for Large Files or Many Files)

  1. Set up a Supabase Edge Function. In your project, create a new Edge Function (Deno-based) using the Supabase CLI: supabase functions new zip-files.
  2. Install required libraries. In the function's deno.json or via import maps, add jszip and the Supabase JS client. For Deno, you can import from esm.sh: import JSZip from 'https://esm.sh/jszip'.
  3. Create the function handler. The function receives a request with the list of file paths (e.g., as JSON). Use the Supabase client with your service role key to download each file as an ArrayBuffer: const { data, error } = await supabase.storage.from('bucket').download(path).
  4. Add to ZIP and generate. Add each ArrayBuffer to the JSZip instance, then generate the ZIP as a Node.js Buffer: const zipBlob = await zip.generateAsync({ type: 'nodebuffer' }).
  5. Return the ZIP as a response. Set the appropriate headers (Content-Type: application/zip, Content-Disposition: attachment; filename="files.zip") and return the buffer as the response body.
  6. Call the function from your client. Use supabase.functions.invoke('zip-files', { body: { paths } }) to get the ZIP, then trigger a download as in the client-side option.

Option 3: Pre-Zipped Storage (Best for Static Sets of Files)

  1. Determine if your file set is static. If the files rarely change (e.g., a set of PDFs for a report), you can pre-zip them once.
  2. Zip locally. Use any desktop tool (like WinZip, 7-Zip, or macOS Archive Utility) to create a ZIP file containing your files.
  3. Upload the ZIP to Supabase Storage. Upload the ZIP file to a bucket, just like any other file.
  4. Provide a direct download link. Use the Supabase client to get a public URL or a signed URL for the ZIP, and let users download that single file.

Common Mistakes

  • Ignoring memory limits. Client-side zipping loads all files into browser memory. For large files (e.g., >100MB total), this can crash the tab.
  • Not handling CORS. When using Edge Functions, ensure your Supabase project's CORS settings allow your domain to call the function.
  • Forgetting to set the correct Content-Type. If you don't set the ZIP response's Content-Type to application/zip, browsers may treat it as a text file.
  • Using the anon key for server-side downloads. If your bucket is private, you must use the service role key in the Edge Function, not the anon key, to bypass RLS.
  • Not revoking object URLs. In client-side zipping, failing to call URL.revokeObjectURL() can cause memory leaks.

Tips

  • Choose based on file size and count. For a few small files (<10MB total), client-side is simplest. For larger sets, use server-side to avoid browser memory issues.
  • Use streams for very large files. In Edge Functions, you can stream files from Supabase to the ZIP to avoid loading entire files into memory, but this is more complex.
  • Cache the ZIP. If the file set changes rarely, consider generating the ZIP once and storing it in Supabase Storage, then serving that.
  • Add progress feedback. For client-side zipping, show a progress bar using the onUpdate callback in JSZip's generateAsync.
  • Consider file naming. Use relative paths in the ZIP to preserve folder structure, but ensure you don't include absolute paths that could confuse users.

Troubleshooting

  • ZIP file is empty or corrupt. Check that each file download succeeded. In the client, log the error from download(). In Edge Functions, ensure you're awaiting each download correctly.
  • Browser shows "Failed - Network Error" when downloading. This often happens when the ZIP is too large for the client to generate. Switch to server-side zipping.
  • Edge Function times out. Supabase Edge Functions have a timeout (default 60s). If your ZIP takes longer, you need to split the work or use a different approach (e.g., pre-zip).
  • CORS errors when calling the function. Go to Supabase Dashboard → Edge Functions → your function → CORS settings, and add your domain.
  • Files missing from ZIP. Verify that the file paths are correct and that the files exist. Use the Supabase dashboard to confirm.
  • ZIP downloads but is not recognized. Ensure the response header Content-Type is set to application/zip and the filename ends with .zip.

Frequently Asked Questions

Can I zip files directly from Supabase without using a third-party service?

Yes, you can use Supabase Edge Functions to zip files server-side, or use client-side JavaScript with a library like JSZip. There's no built-in Supabase feature for zipping.

What is the maximum file size I can zip client-side?

It depends on the user's device memory. Generally, stay under 100MB total, but even that can be risky on mobile devices. For larger files, use server-side zipping.

Do I need a service role key for server-side zipping?

Yes, if your bucket is private or you need to bypass Row Level Security. The service role key can access all files, but keep it secret and only use it in server-side code.

Can I zip files from multiple buckets?

Yes, you can download files from any bucket and add them to the same ZIP. Just ensure the Supabase client has access to those buckets.

Is there a way to zip files without downloading them all at once?

Yes, you can use streaming in Edge Functions to process files one by one, but it's more complex. For most use cases, downloading all files into memory is fine.

How do I handle zip downloads for authenticated users only?

You can use Supabase Auth to check the user's session in the Edge Function, or use signed URLs for the ZIP file if you pre-zip and store it.