Search code examples
pythondjangodjango-rest-frameworkcelery

DRF and Celery: FileNotFoundError: [Errno 2] No such file or directory


This works fine on my local deployment but on the cloud deployement it does not.

with open(file_path, "wb+") as fp:
    for chunk in file:
        fp.write(chunk)

result = upload.delay(name, file_path)

In a different file:

@shared_task
def upload(name, file_path):

    path = Path(path_tmp)
    if os.path.isfile(path):
        do something

The error is

Not a path /mediafiles/rawfiles/file.png", FileNotFoundError: [Errno 2] No such file or directory

When I navigate in the docker to -> /mediafiles/rawfiles, the file is there and has a size.

I am using DRF -> Celery -> Django.

Can someone help why the cloud deployment is not able to find the file?


Solution

  • When you delegate task to celery worker with @shared_task and try to get access to the filesystem, it try to get this file on the worker machine. So you need to execute this task on machine with a file or copy target file to worker machine.