nginx-upload-module模块实现文件断点续传
| 导读 | 每当我们想简单的实现文件上传功能,而又不使用其他的语言(比如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/
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为文件字节大小)
X-Session-ID 上传文件的标识,由客户端随机指定.因为是断点续传,客户端必须确保同一个文件的所有片段上传标识一致
Content-Length 上传片段的大小
#!/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()
nginx-upload-module模块实现文件断点续传的更多相关文章
- nginx upload module的使用
现在的网站,总会有一点与用户交互的功能,例如允许用户上传头像,上传照片,上传附件这类的.PHP写的程序,对于上传文件效率不是很高.幸好,nginx有一个名为upload的module可以解决这个问题. ...
- nginx上传模块—nginx upload module-
一. nginx upload module原理 官方文档: http://www.grid.net.ru/nginx/upload.en.html Nginx upload module通过ngin ...
- Nginx Upload Module 上传模块
传统站点在处理文件上传请求时,普遍使用后端编程语言处理,如:Java.PHP.Python.Ruby等.今天给大家介绍Nginx的一个模块,Upload Module上传模块,此模块的原理是先把用户上 ...
- 转:使用 Nginx Upload Module 实现上传文件功能
普通网站在实现文件上传功能的时候,一般是使用Python,Java等后端程序实现,比较麻烦.Nginx有一个Upload模块,可以非常简单的实现文件上传功能.此模块的原理是先把用户上传的文件保存到临时 ...
- 11.nginx upload module + python django 后台 实现视频上传与切片
1.需求:支持视频上传并切片,支持通过m3u8文件播放 2.视频切片的上一节已经谈过,这一节主要是视频上传的处理 第一步:upload-module模块安装 -----------首先下载upload ...
- ZendFramework-2.4 源代码 - 关于Module - 模块入口文件
<?php // /data/www/www.domain.com/www/module/Album/Module.php namespace Album; use Zend\ModuleMan ...
- 实现Nginx Upload 模块 功能上传文件。
分析(也许我表达的让人难以理解,但是我想说一句,直接实践是最好的.....): 一.Ningx 上传( 1.安装Nginx 的模块文件(upload):https://www.nginx.com/re ...
- nginx上传模块nginx_upload_module和nginx_uploadprogress_module模块进度显示,如何传递GET参数等。
ownload:http://www.grid.net.ru/nginx/download/nginx_upload_module-2.2.0.tar.gzconfigure and make : . ...
- Nginx Image Module图片缩略图 水印处理模块
Nginx Image Module图片缩略图 水印处理模块 下载Tengine tar -zxvf tengine-1.4.5.tar.gz cd tengine-1.4.5 下载Nginx tar ...
随机推荐
- css的6中居中的方式
请先看blog:http://blog.csdn.net/wolinxuebin/article/details/7615098
- No plugin found for prefix 'jetty' in the current project and in the plugin groups 【转】
在maven进行jetty的调试中出现错误: [plain] view plaincopyprint? [ERROR] No plugin found for prefix 'jetty' in th ...
- javamail技术
package com.zh.javaEmail; import java.util.*; import javax.mail.*; import javax.mail.internet.*; imp ...
- 什么是POJO?
本文转载自百度文库,详细出处请参考: http://wenku.baidu.com/view/4a9ad533ee06eff9aef80765.html 我认为写的很准确,很抱歉没有找到作者的名字! ...
- 【Gym 100685J】Just Another Disney Problem(交互/排序)
第一次做交互题. 题意是有n个数(n<1000),你通过问1 a b,后台返回你YES代表a<b,NO代表a>b.要你在10000次询问内给出一个符合的排列.n=1000来说,100 ...
- BZOJ-2748 音量调节 DP+背包(脑残)
水DP,一开始竟然想错了...水了半天....真可怕 2748: [HAOI2012]音量调节 Time Limit: 3 Sec Memory Limit: 128 MB Submit: 1156 ...
- jquery无限级下拉框
非原创,已修改成通用版,效果如下: 下载:http://files.cnblogs.com/files/EasonJim/jquery.menu.rar 项目相关地址 源码:https://githu ...
- 记录一次MVC 3.0错误 HTTP 404您正在查找的资源(或者它的一个依赖项)可能已被移除,或其名称已更改,或暂时不可用。请检查以下 URL 并确保其拼写正确。
在部署到IIS7时,MVC3报了一个找不到资源的错误,文件肯定是有的,而且页面是肯定报错的,也就说内部运行错误了,而MVC把错误没有抛出来而已: 所以对症下药,发觉我的项目里面用了rexs进行多语言, ...
- poj 3233 矩阵快速幂+YY
题意:给你矩阵A,求S=A+A^1+A^2+...+A^n sol:直接把每一项解出来显然是不行的,也没必要. 我们可以YY一个矩阵: 其中1表示单位矩阵 然后容易得到: 可以看出这个分块矩阵的左下角 ...
- C# WPF 显示图片和视频显示 EmuguCv、AForge.Net测试(续)
介绍 本文是接着上文<C# WPF 显示图片和视频显示 EmuguCv.AForge.Net测试>写的,建议先看下上文,因为有些代码还需要了解. 增添 接着上文的代码,我们可以在事件处理方 ...