python upload_module_nginx-upload-module模块实现文件上传(multipart/form-data和断点续传)

17270ce95af85a4a2d33846cd5831d99.png

前言

有时候我们想简单实现文件上传的功能,又不想使用额外的语言(比如PHP、Java),或者想实现文件的断点续传。这个时候Nginx的一个模块nginx-upload-module就能满足我们的需求。

模块安装

下载模块:

cd /tmp

wget https://codeload.github.com/vkholodkov/nginx-upload-module/zip/2.2

unzip 2.2

安装模块:

.configure --add-module=/tmp/nginx-upload-module-2.2/

multipart/form-data表单上传示例

nginx.conf配置:

server {

[...]

location /upload {

upload_pass @uploadHandler;

upload_store /usr/local/nginx/upload_temp 1;

upload_set_form_field $upload_field_name.path "$upload_tmp_path";

}

location @uploadHandler {

proxy_pass http://backend-host;

}

[...]

}

这里在server里定义了upload location,这个location是上传的接口,还有@uploadHandler location,是当文件上传完成后,nginx模块会对这个location发送一些必要的信息,如文件上传的路径。

这里涉及了几个指令:

upload_pass @uploadHandler: 上传完成后会发送必要的数据到@uploadHandler;

upload_store /usr/local/nginx/upload_temp 1: 文件上传的临时目录;

upload_set_form_field $upload_field_name.path “$upload_tmp_path”: 设置文件上传完成后,把文件临时路径发送给upload_pass指定的location。

断点续传示例

nginx.conf配置

server {

[...]

location /resumable_upload {

upload_resumable on;

upload_state_store /usr/local/nginx/upload_temp ;

upload_pass @drivers_upload_handler;

upload_store /usr/local/nginx/upload_temp;

upload_set_form_field $upload_field_name.path "$upload_tmp_path";

}

location @resumable_upload_handler {

proxy_pass http://localhost:8002;

}

[...]

}

与上一步multipart/form-data表单上传示例配置不同的地方有:

upload_resumable on: 开启断点续传功能;

upload_state_store /usr/local/nginx/upload_temp: 设置断点续传状态文件存储的目录。

上传文件第一个片段

POST /upload HTTP/1.1

Host: example.com

Content-Length: 51201

Content-Type: application/octet-stream

Content-Disposition: attachment; filename="big.TXT"

X-Content-Range: bytes 0-51200/511920

Session-ID: 1111215056

<0-51200的字节文件数据>

上传文件第一个片段服务器响应

HTTP/1.1 201 Created

Date: Thu, 02 Sep 2010 12:54:40 GMT

Content-Length: 14

Connection: close

Range: 0-51200/511920

0-51200/511920

上传文件最后一个片段

POST /upload HTTP/1.1

Host: example.com

Content-Length: 51111

Content-Type: application/octet-stream

Content-Disposition: attachment; filename="big.TXT"

X-Content-Range: bytes 460809-511919/511920

Session-ID: 1111215056

<460809-511919字节文件数据>

上传文件最后一个片段服务器响应

HTTP/1.1 200 OK

Date: Thu, 02 Sep 2010 12:54:43 GMT

Content-Type: text/html

Connection: close

Content-Length: 2270

请求头说明

请求头

说明

Content-Disposition

attachment, filename=“上传的文件名”

Content-Type

待上传文件的mime type,如application/octet-stream(注:不能为multipart/form-data)

X-Content-Range

待上传文件字节范围,如第一片段bytes 0-51200/511920,最后一个片段bytes 460809-511919/511920(注:文件第一个字节标号为0,最后一个字节标号为n-1,其中n为文件字节大小)

上传文件的标识,由客户端随机指定.因为是断点续传,客户端必须确保同一个文件的所有片段上传标识一致

Content-Length

上传片段的大小

Python上传demo

#!/usr/bin/python

# -*- coding: utf-8 -*-

import os.path

import requests

import hashlib

# 待上传文件路径

FILE_UPLOAD = "/tmp/testfile"

# 上传接口地址

UPLOAD_URL = "http://host/drivers_upload"

# 单个片段上传的字节数

SEGMENT_SIZE = 1048576

def upload(fp, file_pos, size, file_size):

session_id = get_session_id()

fp.seek(file_pos)

payload = fp.read(size)

content_range = "bytes {file_pos}-{pos_end}/{file_size}".format(file_pos=file_pos,

pos_end=file_pos+size-1,file_size=file_size)

headers = {'Content-Disposition': 'attachment; filename="big.TXT"','Content-Type': 'application/octet-stream',

'X-Content-Range':content_range,'Session-ID': session_id,'Content-Length': size}

res = requests.post(UPLOAD_URL, data=payload, headers=headers)

print(res.text)

# 根据文件名hash获得session id

def get_session_id():

m = hashlib.md5()

file_name = os.path.basename(FILE_UPLOAD)

m.update(file_name)

return m.hexdigest()

def main():

file_pos = 0

file_size = os.path.getsize(FILE_UPLOAD)

fp = open(FILE_UPLOAD,"r")

while True:

if file_pos + SEGMENT_SIZE >= file_size:

upload(fp, file_pos, file_size - file_pos, file_size)

fp.close()

break

else:

upload(fp, file_pos, SEGMENT_SIZE, file_size)

file_pos = file_pos + SEGMENT_SIZE

if __name__ == "__main__":

main()

参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. --prefix=${PATH_INSTALL}/nginx: --prefix 指定安装目录,${PATH_INSTALL}/nginx 为安装目录的路径。可选值为任意路径,根据实际需要进行设置。此参数会影响到 nginx 的安装位置,例如配置文件的位置、日志文件的位置等。 2. --user=nginx: --user 指定 nginx 运行的用户,默认为 nobody。可选值为任意用户,根据实际需要进行设置。此参数会影响到 nginx 运行的权限。 3. --group=nginx: --group 指定 nginx 运行的用户组,默认为 nobody。可选值为任意用户组,根据实际需要进行设置。此参数会影响到 nginx 运行的权限。 4. --with-http_ssl_module: --with-http_ssl_module 开启 SSL/TLS 功能,支持 HTTPS 协议。可选值为 --with-http_ssl_module 或 --without-http_ssl_module。如果需要支持 HTTPS 协议,则必须开启此选项。 5. --with-http_realip_module: --with-http_realip_module 开启真实 IP 模块,用于获取客户端真实 IP 地址。可选值为 --with-http_realip_module 或 --without-http_realip_module。如果需要获取客户端真实 IP 地址,则必须开启此选项。 6. --with-http_addition_module: --with-http_addition_module 开启添加响应头模块,用于添加自定义的响应头信息。可选值为 --with-http_addition_module 或 --without-http_addition_module。 7. --with-http_sub_module: --with-http_sub_module 开启替换响应内容模块,用于替换响应内容中的关键字。可选值为 --with-http_sub_module 或 --without-http_sub_module。 8. --with-http_dav_module: --with-http_dav_module 开启 WebDAV 模块,用于支持 WebDAV 协议。可选值为 --with-http_dav_module 或 --without-http_dav_module。 9. --with-http_flv_module: --with-http_flv_module 开启 FLV 视频流模块,用于支持 FLV 格式的视频流。可选值为 --with-http_flv_module 或 --without-http_flv_module。 10. --with-http_mp4_module: --with-http_mp4_module 开启 MP4 视频流模块,用于支持 MP4 格式的视频流。可选值为 --with-http_mp4_module 或 --without-http_mp4_module。 11. --with-http_gunzip_module: --with-http_gunzip_module 开启 Gzip 解压缩模块,用于支持 Gzip 压缩格式。可选值为 --with-http_gunzip_module 或 --without-http_gunzip_module。 12. --with-http_gzip_static_module: --with-http_gzip_static_module 开启 Gzip 静态文件压缩模块,用于对静态文件进行 Gzip 压缩。可选值为 --with-http_gzip_static_module 或 --without-http_gzip_static_module。 13. --with-http_stub_status_module: --with-http_stub_status_module 开启状态页面模块,用于查看 nginx 的状态信息。可选值为 --with-http_stub_status_module 或 --without-http_stub_status_module。 14. --with-stream: --with-stream 开启 TCP/UDP 代理模块,用于支持 TCP/UDP 协议。可选值为 --with-stream 或 --without-stream。 15. --with-stream_ssl_module: --with-stream_ssl_module 开启 SSL/TLS 功能,支持 TCP/UDP 的 SSL/TLS 加密。可选值为 --with-stream_ssl_module 或 --without-stream_ssl_module。 16. --with-http_v2_module: --with-http_v2_module 开启 HTTP/2 模块,用于支持 HTTP/2 协议。可选值为 --with-http_v2_module 或 --without-http_v2_module。 17. --with-pcre: --with-pcre 指定使用 PCRE 库进行正则表达式匹配。可选值为 --with-pcre 或 --without-pcre。 18. --with-openssl=/www/server/nginx/src/openssl: --with-openssl 指定使用 OpenSSL 库进行 SSL/TLS 加密。可选值为 OpenSSL 库的路径。如果开启了 SSL/TLS 功能,则必须指定此选项。 19. --with-stream_ssl_preread_module: --with-stream_ssl_preread_module 开启 TCP/UDP SSL/TLS 握手前置模块,用于在握手前解析 SSL/TLS 协议。可选值为 --with-stream_ssl_preread_module 或 --without-stream_ssl_preread_module。 20. --with-http_image_filter_module: --with-http_image_filter_module 开启图片处理模块,用于对图片进行缩放、裁剪等操作。可选值为 --with-http_image_filter_module 或 --without-http_image_filter_module。 21. --with-ipv6: --with-ipv6 开启 IPv6 支持。可选值为 --with-ipv6 或 --without-ipv6。 22. --with-ld-opt=-Wl,-E: --with-ld-opt 指定链接器选项,-Wl,-E 表示启用链接器的 export-dynamic 选项。可选值为任意链接器选项,根据实际需要进行设置。此参数会影响到 nginx 的链接器选项。 23. --with-cc-opt=-Wno-error: --with-cc-opt 指定编译器选项,-Wno-error 表示忽略编译器的错误提示。可选值为任意编译器选项,根据实际需要进行设置。此参数会影响到 nginx 的编译器选项。 24. --with-ld-opt=-ljemalloc: --with-ld-opt 指定链接器选项,-ljemalloc 表示链接 jemalloc 库。可选值为任意链接器选项,根据实际需要进行设置。此参数会影响到 nginx 的链接器选项。 25. --add-module=/www/server/nginx/src/ngx_devel_kit: --add-module 指定添加第三方模块,/www/server/nginx/src/ngx_devel_kit 为第三方模块的路径。可选值为任意第三方模块的路径,根据实际需要进行设置。此参数会影响到 nginx模块加载顺序。 26. --add-module=/www/server/nginx/src/lua_nginx_module: --add-module 指定添加第三方模块,/www/server/nginx/src/lua_nginx_module 为第三方模块的路径。可选值为任意第三方模块的路径,根据实际需要进行设置。此参数会影响到 nginx模块加载顺序。 27. --add-module=/www/server/nginx/src/ngx_cache_purge: --add-module 指定添加第三方模块,/www/server/nginx/src/ngx_cache_purge 为第三方模块的路径。可选值为任意第三方模块的路径,根据实际需要进行设置。此参数会影响到 nginx模块加载顺序。 28. --add-module=/www/server/nginx/src/ngx_http_substitutions_filter_module-master: --add-module 指定添加第三方模块,/www/server/nginx/src/ngx_http_substitutions_filter_module-master 为第三方模块的路径。可选值为任意第三方模块的路径,根据实际需要进行设置。此参数会影响到 nginx模块加载顺序。 29. --add-module=/www/server/nginx/src/nginx-dav-ext-module: --add-module 指定添加第三方模块,/www/server/nginx/src/nginx-dav-ext-module 为第三方模块的路径。可选值为任意第三方模块的路径,根据实际需要进行设置。此参数会影响到 nginx模块加载顺序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值