Skip to content

Can't figure out how to configure CORS to allow directly file uploads using presigned url.Β #1662

@sergiotapia

Description

@sergiotapia

Access to fetch at 'https://ki123123.supabase.co/object/upload/sign/my-bucket/coin123123123.png?token=123123123' from origin 'http://localhost:4000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

I'm generating a presigned url in my backend, and sending that to my frontend.

let Uploaders = {}

Uploaders.S3 = async function (entries, onViewError) {
  for (const entry of entries) {
    const { key, token } = entry.meta;
    const file = entry.file;

    const url = new URL(`https://ki123123.supabase.co/object/upload/sign/${key}`);
    url.searchParams.set('token', token);

    try {
      const body = new FormData();
      body.append('file', file);
      body.append('cacheControl', '3600');

      const headers = {
        'x-upsert': 'false',
      };

      const res = await fetch(url.toString(), {
        method: 'PUT',
        body: body,
        headers,
      });

      const data = await res.json();

      if (res.ok) {
        console.log('File uploaded successfully:', data);
      } else {
        console.error('Upload error:', data);
        if (onViewError) onViewError(data);
      }
    } catch (error) {
      console.error('Unexpected error:', error);
      if (onViewError) onViewError(error);
    }
  }
}

export default Uploaders;

In AWS S3 or Cloudflare R2, I would go into the bucket and tweak the CORS parameters there to allow direct browser uploads. I can't figure out how to do this in Supabase. Any tips?

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentationstorage-jsRelated to the storage-js library.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions