Back

[CLOSED] File upload to functions

  • 0
  • Self Hosted
  • Functions
  • Storage
Row0
30 Oct, 2023, 08:54

Hi, I am new to Appwrite and I am trying to upload files using a POST HTTP request to a function, which processes the file and store it in the storage system. Is it possible to do this? I don't see there's a files or form attribute of the context object, and posting through body doesn't seem to work. Is there any way I can upload files to a function? Or should I do this in the first place?

TL;DR
User is asking if it's possible to upload files to a function in Appwrite and process them before saving to the storage system. Several suggestions are made, including using separate buckets, converting the file to base64, and using the client SDK. No definitive solution is provided in the thread.
Guille
30 Oct, 2023, 10:40

Hi @Row0 why you want to use a function and not the client SDK? What is your use case?

Row0
30 Oct, 2023, 16:32

Hi! So basically what I want is to process the files my user uploads (compression, filtering, etc.) before saving them to the storage.

Guille
30 Oct, 2023, 16:34

What appwrite version are you using?

Row0
30 Oct, 2023, 19:11

I'm using the web version

Guille
31 Oct, 2023, 12:09

What I would do in your case, as function only accept a string as payload, convert the file in base64 and send it as JSON with the rest of your data.

You can do with something like this:

TypeScript
const convertBase64 = (file) => {
    return new Promise((resolve, reject) => {
      const fileReader = new FileReader();
      fileReader.readAsDataURL(file)
      fileReader.onload = () => {
        resolve(fileReader.result);
      }
      fileReader.onerror = (error) => {
        reject(error);
      }
    })
  }

const base64 = await convertBase64(file)
Drake
31 Oct, 2023, 17:36

honestly, i would just use 2 separate buckets, upload to 1 bucket, have a function that triggers on upload, process the file, and then store it in another bucket for serving.

Row0
1 Nov, 2023, 21:19

Thank you! I think I'll stick with this approach.

Drake
1 Nov, 2023, 21:46

[CLOSED] File upload to functions

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