Back

[SOLVED] My function runs twice on create file event when it's size is greater than 5mb

  • 0
  • Functions
  • Web
  • Storage
  • Cloud
Ajit Singh
6 Jun, 2023, 12:52

def main(req, res): """This function transcribes any audio file that is put into the bucket audio-files""" # Setup of various services client = Client() ( client.set_endpoint(req.variables.get("APPWRITE_FUNCTION_ENDPOINT", None)) .set_project(req.variables.get("APPWRITE_FUNCTION_PROJECT_ID", None)) .set_key(req.variables.get("APPWRITE_FUNCTION_API_KEY", None)) )

TypeScript
storage = Storage(client)


# Get the event data
event_data = json.loads(req.variables.get("APPWRITE_FUNCTION_EVENT_DATA", None))
event_name = req.variables.get("APPWRITE_FUNCTION_EVENT", None)


return res.json(
    {
        "event_name": event_name
    }
)

I have the following functin it runs twice each time I run it on trigger of an event when a file is created in the bucket. I am uploading via the appwrite cloud console.

Get following as event

in respnse of both {"event_name":"buckets.647b122c985261158060.files.647f2a9b73d3aca5f718.create"} {"event_name":"buckets.647b122c985261158060.files.647f2a9b73d3aca5f718.create"}

This happens when the file is more that 5mb

TL;DR
The user is experiencing an issue where their function runs twice on the event of creating a file that is more than 5mb in size. They are looking for a workaround to track the progress of the file upload. A suggested solution is to check if the number of uploaded chunks is equal to the total number of chunks in the event_data object. Additionally, they can tap into the onProgress method in the client SDK to track the upload progress. There is a discussion about whether this behavior is a bug or expected. The user is provided with example code and links to relevant documentation. No resolution to the duplicate function execution is provided in the thread
Binyamin
6 Jun, 2023, 13:28

Ohh

Binyamin
6 Jun, 2023, 13:28

This happens when the file is more that 5mb

Make sense now.

Binyamin
6 Jun, 2023, 13:29

Because when the file is more then 5MB the upload will dived to 5MB chunks

Ajit Singh
6 Jun, 2023, 13:30

But I uploaded 1 file the event should not be fired twice

Ajit Singh
6 Jun, 2023, 13:30

Is it a bug

Binyamin
6 Jun, 2023, 13:30

Mmm, could be But maybe pepole like to track the upload progress..

Binyamin
6 Jun, 2023, 13:31

For now what you can do is to check the chunkTotal and chunkUploaded in the event_data object to know if the upload has finished

Binyamin
6 Jun, 2023, 13:31
Ajit Singh
6 Jun, 2023, 13:35

Is there any other way to track progress of the upload

Binyamin
6 Jun, 2023, 13:37

Within functions? I don't think so

Ajit Singh
6 Jun, 2023, 13:58

In client sdk

Binyamin
6 Jun, 2023, 14:18

You can tap into the onProgress method https://github.com/appwrite/sdk-for-web/blob/master/src/services/storage.ts#L155

Like so:

TypeScript
storage.createFile('[BUCKET_ID]', '[FILE_ID]', document.getElementById('uploader').files[0], null, (prg)=>{
  console.log(prg);
});
Ajit Singh
6 Jun, 2023, 14:20

thanks for your effort

Ajit Singh
6 Jun, 2023, 14:21

I will write a workaround for the evebts problem.

Ajit Singh
6 Jun, 2023, 14:22

My function runs twice on create file event when it's size is greater than 5mb

Ajit Singh
6 Jun, 2023, 14:27

but just for future if there is separate way of progress tracking. Shouldn't create fire a single event on file creation of more than 5mb

Binyamin
6 Jun, 2023, 14:34

I don't think so, No.

Drake
6 Jun, 2023, 15:24

Here's the related issue. feel free to 👍🏼 https://github.com/appwrite/appwrite/issues/4507

For now, you can check if chunks uploaded == chunks total in your function

Ajit Singh
7 Jun, 2023, 02:45

Thanks steven

Bernard
22 Jun, 2023, 09:35

were you able to write a workaround for tracking the upload progress?

Bernard
22 Jun, 2023, 09:36

I am working on a project and i need to track the progress of the file upload

Drake
22 Jun, 2023, 16:55

Please create a new <#1072905050399191082> post rather than posting in an old thread

Drake
22 Jun, 2023, 16:55

[SOLVED] My function runs twice on create file event when it's size is greater than 5mb

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