用于Django REST框架的Nginx上传模块

时间:2019-05-27 07:32:23

标签: django docker nginx file-upload django-rest-framework

我想使用nginx上传模块将大文件上传到我的djangos MEDIA文件夹中。

单个文件上传的Django REST“ POST”端点位于

http://localhost:1337/api/record/measurements/#id/upload/

其中#id可以是整数(1,2,3,4,5 ...),适用于没有nginx的django。

我写了nginx配置

    #/etc/nginx/conf.d/default.conf
    location @backend {
        proxy_pass http://django-app:8000;
    }

    location  /api/record/measurements/1/upload/ {
        error_page 418 = @upload;
        if ($request_method = POST ) {
            return 418;
        }
        include /etc/nginx/mainloc.conf;
    }

    location @upload {
        upload_pass   @backend;
        upload_store /vol/web/media/_upload 1;
        upload_store_access user:r;
        upload_set_form_field $upload_field_name.name "$upload_file_name";
        upload_set_form_field $upload_field_name.content_type "$upload_content_type";
        upload_set_form_field $upload_field_name.path "$upload_tmp_path";
        upload_aggregate_form_field "$upload_field_name.md5" "$upload_file_md5";
        upload_aggregate_form_field "$upload_field_name.size" "$upload_file_size";
        upload_pass_form_field "^submit$|^description$";
        upload_cleanup 400 404 499 500-505;

    }


    location / {
        include /etc/nginx/mainloc.conf;
    }

#mainloc.conf
proxy_pass http://django-app:8000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;

编辑:

我设法将文件上传到/ vol / web / media / _upload,但是文件上传完成后,我得到了响应

{
    "id": 1,
    "data": null
}

并且上传的文件未移动到最终位置     MEDIA = / vol / web / media /,但保留在 upload_store

0 个答案:

没有答案