Back

[SOLVED] Getting CORS error while uploading files to bucket

  • 0
  • Web
yatharth
27 May, 2023, 07:43

I am using this sample code in nextjs

const storeFile = async () => { try { const uploader = document.getElementById( "uploader" ) as HTMLInputElement | null; if (!uploader) { throw new Error("Uploader element not found"); }

TypeScript
const file = uploader.files?.[0];
console.log(file);
if (!file) {
  throw new Error("No file selected");
}

const promise = await storage.createFile(
  process.env.BUCKET_ID as string,
  ID.unique(),
  file
);

return promise;

} catch (err) { alert(err); } };

TL;DR
User is experiencing a CORS error while trying to upload files to a bucket using Appwrite. They suspect the issue may be related to their environment variables. Another user suggests checking the request headers in the network tab. The user resolves the issue by fixing their undefined environment variables. If anyone else is experiencing similar issues, they can try the solution provided in this GitHub thread: https://github.com/appwrite/appwrite/issues/2687. The user also shares a code snippet they are using to upload files in a Next.js project.
Bouahaza
27 May, 2023, 08:36
Drake
27 May, 2023, 15:45

What are the permissions on the bucket?

yatharth
27 May, 2023, 15:56

Role ANY Permissions CREATE READ UPDATE

Drake
27 May, 2023, 16:02

Would you please switch to the network tab, find the request, and share the request headers?

yatharth
27 May, 2023, 16:18

yes its working now some issue with env variables. My env variables were coming undefined

yatharth
27 May, 2023, 16:18

thats why cors was coming

Drake
27 May, 2023, 16:22

[SOLVED] Getting CORS error while uploading files to bucket

Emilson
28 May, 2023, 19:48

Hello @Steven I'm having the same issues. Can you please share how I can fix this? I'm new to Appwrite and still don't understand how some of the services work.

I keep having the COR Policy issue each time I try to submit the file. Could it be the problem is from my env variables ?

Drake
28 May, 2023, 20:11

Please create a new <#1072905050399191082> post

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more