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 (转载注明出处)
如果你有任何建议或疑问可以在下面 留言
发表第一条评论!
相关推荐
小工具
标签
python
form
data
post
上传
wangeditor
tornado
例子
完整
nginx
服务器
中断
文件
mac
lua
极速
简洁
安装
sdk
ssdb
推荐
两个
cjson
module
错误
解决
webview
file
android
input
低功耗
web
爬虫
组装
google
身份验证
authenticator
迁移
手机
bbr
openwrt
路由
开启
sqlite3
连接池
遍历
数据库
方法
golang
quic
抢先
国内
pkcs7
js
crypto
aes
ecb
caddy
https
部署
网站
多个
硬件加速
ubnt
er
固件
app
store
登录
未知
bottle
uwsgi
搭建
环境
搜索引擎
网址
提交
主动
经验
api
自动
字符串
中文
分割
英文
usb
上网卡
共享
网络
自动更新
microsoft
关闭
静音
风扇
主机
笔记本
改造
微信
尝鲜
视频
体验
cpu
debian
ubuntu
查看
温度
chrome
server
浏览
webdriver
微博
selenium
gnu
linux
系统启动
编译
宅家
坑记
屏幕
动手
useragent
蜘蛛
真假
识别
最近发表
- 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
- 从UserAgent识别搜索引擎并判断真假蜘蛛
最近浏览
- 自己组装21瓦低功耗家庭爬虫、文件、web服务器
- 换手机后 Google 身份验证器 Google Authenticator 数据迁移的简单方法
- Openwrt 路由上开启BBR
- python SQLite3 连接池
- SSDB 数据库遍历的方法
- 在 Nginx 和 Golang web 上抢先体验 QUIC
- OpenWrt 国内源
- python & js Crypto AES ECB Pkcs7 加密解密实现
- 使用caddy 快速部署多个https 网站
- 路由 UBNT ER-X 官方固件升级及开启硬件加速的方法
- Mac 登录 App Store 出现“发生了未知错误”的解决方法
- Bottle + UWSGI + Nginx 环境搭建过程
- 向各搜索引擎主动提交网址的经验
- 利用 API 自动向搜索引擎提交网址
- python 分割中文英文混合字符串的正确方法
- Openwrt 使用USB 4G 上网卡共享网络