change path of uploaded file for a specific endpoint?

Can I set FILE_UPLOAD_TEMP_DIR to a specific endpoint?
I need to receive files and save some to local and others to NFS.
Currently, I’m moving the files after upload finished but it’s slow due to they’re moved through network.
Therefore, I’d like to make the files to uploaded to different directories depending on the endpoint so that I don’t need to move them.

Kinda - it depends upon when exactly in the process you want to make this determination. Basically, you need to set the destination before accessing the request containing the file.

See File Uploads | Django documentation | Django

1 Like

Glad to hear that it might be possible!
In the case of below, how can I do that?

@api_view(['POST'])
@permission_classes([AllowAny])
def upload_to_nfs(request):
    file = request.FILES.get('file', None) 
    nfs_path = '/nfs-root/tmp'
    
    # To make the file uploaded to "nfs_path", not "FILE_UPLOAD_TEMP_DIR" 

It’s all described in the docs linked in my previous response.

Below worked. Thank you!

file = request.FILES.get('file', None)

with open(os.path.join(nfs_path, file.name), 'wb+') as destination:
    for chunk in file.chunks():
        destination.write(chunk)