Openresty使用lua-resty-upload模块上传并保存文件
🕖 by pyList at 2014-06-20 18:00
OpenResty 是一个基于 Nginx 与Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。这里将用到 lua-resty-upload 模块来处理上传超大的文件的请求。
lua-resty-upload
lua-resty-upload 模块实现基于 rfc1867
的 http协议文件上传、要求客户端提交的表单enctype=”multipart/form-data”,method=”POST”。
前端
<form action="upfile" method="post" enctype="multipart/form-data">
<label for="testFileName">select file: </label>
<input type="file" name="testFileName"/>
<input type="submit" name="upload" value="Upload" />
</form>
后端处理
local upload = require "resty.upload"
local cjson = require "cjson"
local chunk_size = 4096 -- should be set to 4096 or 8192
local filename
local file = nil
function get_filename(res)
local filename = ngx.re.match(res,'(.+)filename="(.+)"(.*)')
if filename then
return filename[2]
end
end
function existsFile(path)
x = io.open(path)
if x == nil then
io.close()
return false
else
x:close()
return true
end
end
local form, err = upload:new(chunk_size)
if not form then
ngx.log(ngx.ERR, "failed to new upload: ", err)
ngx.exit(500)
end
form:set_timeout(1000) -- 1 sec
local osfilepath = "/tmp/"
local i=0
while true do
local typ, res, err = form:read()
if not typ then
ngx.say("failed to read: ", err)
return
end
ngx.say("read: ", cjson.encode({typ, res}))
if typ == "header" then
if res[1] ~= "Content-Type" then
filename = get_filename(res[2])
if filename then
i=i+1
filepath = osfilepath .. filename
file = io.open(filepath,"w+")
if not file then
ngx.say("failed to open file: ", filepath)
return
end
end
end
elseif typ == "body" then
ngx.say("body begin")
if file then
file:write(res)
ngx.say("write ok: ", res)
end
elseif typ == "part_end" then
ngx.say("part_end")
if file then
file:close()
file = nil
ngx.say("file upload success")
end
elseif typ == "eof" then
break
end
end
local typ, res, err = form:read()
ngx.say("read: ", cjson.encode({typ, res}))
if i==0 then
ngx.say("please upload at least one file!")
return
end
python 测试代码
import requests #pip install requests if you don't have it already
url = 'http://www.test.com/lua/upload'
files = {'file':open('55e82a49e35383de7dcaa8b325da148f.jpg')}
#files = {'file':open('hello.txt')}
#'file' => name of html input field
r = requests.post(url, files=files)
#print r
#print dir(r)
print '=============== content ==================='
#print r.content
'''
print '=============== text ==================='
print r.text
print '=============== json ==================='
print r.json
print '=============== raw ==================='
print r.raw
'''
print '=============== url ==================='
print r.url
本文网址: https://pylist.com/t/1403258407 (转载注明出处)
如果你有任何建议或疑问可以在下面 留言
发表第一条评论!
相关推荐
小工具
标签
lua
sdk
ssdb
推荐
两个
cjson
module
错误
解决
webview
file
android
input
上传
python
form
data
post
wangeditor
tornado
例子
完整
nginx
服务器
中断
文件
mac
极速
简洁
安装
苦短
城铁
guido
我用
kvdb
sae
方便
使用
memcached
正式
支持
openwrt
usb
上网卡
共享
网络
golang
断言
类型
注意
问题
xor
解密
加密
方法
bbr
路由
开启
百度
google
pk
三大
必应
json
解析
测试
速度
ubuntu
gnu
debian
linux
修砖记
变砖
ubnt
er
tftp
轻量级
博客
一款
ulimit
永久
设置
系统
之坑
files
open
服务
js
全屏
浏览器
实现
caddy
https
部署
网站
多个
failed
load
devtools
sourcemap
chrome
自动更新
microsoft
关闭
app
store
登录
未知
静音
风扇
主机
笔记本
改造
低功耗
web
爬虫
组装
微信
尝鲜
视频
体验
cpu
查看
温度
server
浏览
身份验证
authenticator
迁移
手机
webdriver
微博
selenium
系统启动
固件
编译
宅家
坑记
屏幕
动手
硬件加速
quic
抢先
最近发表
- Chrome 控制台 DevTools failed to load SourceMap 警告的消除方法
- Mac 关闭 Microsoft 自动更新
- Mac 登录 App Store 出现“发生了未知错误”的解决方法
- 老笔记本改造为无风扇静音主机方案
- 自己组装21瓦低功耗家庭爬虫、文件、web服务器
- 微信视频号尝鲜体验
- Ubuntu/Debian 查看CPU温度的方法
- 在Ubuntu/debian Server 系统使用Chrome 无头浏览模式
- 换手机后 Google 身份验证器 Google Authenticator 数据迁移的简单方法
- 使用Golang selenium WebDriver 自动登录微博
- 在 Ubuntu 或其它 GNU/Linux 系统下安装 Debian
- Mac 下制作 USB ubuntu/debian 系统启动、安装盘的几种方法
- ubuntu/debian 下自行编译 OpenWRT 固件
- 宅家自己动手换手机屏幕掉坑记
- 路由 UBNT ER-X 官方固件升级及开启硬件加速的方法
- 在 Nginx 和 Golang web 上抢先体验 QUIC
最近浏览
- Python之父Guido穿着“人生苦短,我用Python”T恤等城铁
- SAE 上还是使用KVDB 方便
- SAE 正式支持 Memcached 了
- Openwrt 使用USB 4G 上网卡共享网络
- Golang里的类型断言及注意问题
- 用python 实现XOR 加密解密的方法
- Openwrt 路由上开启BBR
- 神奇:google、必应、百度三大翻译PK,百度出奇制胜!
- go json 解析速度测试
- 在 Ubuntu 或其它 GNU/Linux 系统下安装 Debian
- UBNT ER-X 变砖 tftp 修砖记
- 一款轻量级的基于SAE Python的博客诞生了
- Linux 系统永久设置ulimit
- Golang 服务之坑:too many open files
- js实现浏览器全屏方法
- 使用caddy 快速部署多个https 网站