nginx-upload-module模块实现文件断点续传_nginx upload module 断点续传 进度(1)

最后的话

最近很多小伙伴找我要Linux学习资料,于是我翻箱倒柜,整理了一些优质资源,涵盖视频、电子书、PPT等共享给大家!

资料预览

给大家整理的视频资料:

给大家整理的电子书资料:

如果本文对你有帮助,欢迎点赞、收藏、转发给朋友,让我有持续创作的动力!

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以点击这里获取!

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

            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 u p l o a d f i e l d n a m e . p a t h " upload_field_name.path " uploadfieldname.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 上传片段的大小



**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():

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以点击这里获取!

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

需要这份系统化的资料的朋友,可以点击这里获取!

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值