wget https://github.com/fdintino/nginx-upload-module/archive/2.3.0.tar.gz
tar zxvf 2.3.0.tar.gz
cd nginx-upload-module-2.3.0
./configure --add-module=/path/to/nginx-upload-module
make
make install
local upload = require "resty.upload"
local redis = require "resty.redis"
local red = redis:new()
red:set_timeout(1000)
local ok, err = red:connect("127.0.0.1", 6379)
if not ok then
ngx.say("failed to connect: ", err)
return
end
local chunk_size = 8192
local chunk_cnt = 0
local file_name = nil
local file_type = nil
local file_md5 = nil
local file_size = 0
function get_extension(file_name)
return file_name:match(".+%.(%w+)$")
end
function handle_file(meta, file)
while true do
local data, err, partial = file:read(chunk_size)
if err then
ngx.log(ngx.ERR, "failed to read file: ", err)
return err
end
if not data then
file:close()
break
end
chunk_cnt = chunk_cnt + 1
file_size = file_size + #data
local key = string.format("%s-%d", meta.name, chunk_cnt)
local ok, err = red:set(key, data)
if not ok then
ngx.log(ngx.ERR, "failed to set redis: ", err)
return err
end
end
local ext = get_extension(file_name)
file_md5 = ngx.md5(meta.md5)
local path = string.format("/upload/%s.%s", file_md5, ext)
path = ngx.escape_uri(path)
local ok, err = red:set(file_md5, path)
if not ok then
ngx.log(ngx.ERR, "failed to set redis: ", err)
return err
end
return path
end
local form, err = upload:new(chunk_size)
if not form then
ngx.log(ngx.ERR, "failed to new upload: ", err)
return ngx.exit(500)
end
form:set_timeout(1000)
while true do
local typ, res, err = form:read()
if not typ then
ngx.log(ngx.ERR, "failed to read form: ", err)
return ngx.exit(500)
end
if typ == "header" then
if res[1] ~= "Content-Disposition" then
file_type = res[2]
else
file_name = res[2]:match("filename=\"(.+)\"")
end
elseif typ == "body" then
handle_file(res, form)
elseif typ == "eof" then
break
end
end
local path = handle_file({name = file_name, md5 = file_md5}, nil)
ngx.say(path)
实现上传文件的下载功能,从redis中获取文件信息并返回文件内容。示例脚本如下:
code
local redis = require "resty.redis"
local cgi = require "cgi"
local red = redis:new()
red:set_timeout(1000)
local ok, err = red:connect("127.0.0.1", 6379)
if not ok then
ngx.say("failed to connect: ", err)
return
end
local args, err = cgi:get_all()
if not args or not args.path then
ngx.say("invalid request")
return
end
local path = ngx.unescape_uri(args.path)
local file_md5 = path:match("/upload/([%w-]+)%.")
if not file_md5 then
ngx.say("invalid file path")
return
end
local key_cnt, err = red:get(file_md5 .. "-count")
if not key_cnt then
ngx.log(ngx.ERR, "failed to get key count: ", err)
return
end
ngx.header["Content-Length"] = tonumber(key_cnt)
ngx.header["Content-Type"] = "application/octet-stream"
ngx.header["Content-Disposition"] = "attachment; filename=\""..path.."\""
local chunk_size = 8192
for i = 1, key_cnt do
local key = string.format("%s-%d", file_md5, i)
local data, err = red:get(key)
if not data then
ngx.writeHead(500)
ngx.say("failed to get data")
return ngx.exit()
end
ngx.say(data)
end