53 lines
1.6 KiB
Lua
53 lines
1.6 KiB
Lua
-- 指定模块引用目录,否则无法加载同目录下的其他文件
|
|
--package.path = package.path .. ';/usr/local/opt/openresty/nginx/scripts/?.lua';
|
|
--package.path = package.path .. ';/Users/liyong/Code/AlchemyStudio/resty_functions/scripts/?.lua';
|
|
package.path = package.path .. ';<SCRIPT_PATH>/?.lua';
|
|
|
|
local upyun_upload = {}
|
|
|
|
local strip_path = require("lib.strip_path")
|
|
local Upyun = require('lib.upyun')
|
|
|
|
function upyun_upload.upload(filepath, filename)
|
|
local upyun, err = Upyun:new({
|
|
user = ngx.var.upyun_operator,
|
|
passwd = ngx.var.upyun_password,
|
|
localFilePath = filepath
|
|
})
|
|
|
|
if not upyun then
|
|
ngx.status = 500
|
|
ngx.log(ngx.ERR, "failed to initialize upyun: " .. err)
|
|
return
|
|
end
|
|
|
|
local bucket = ngx.var.upyun_bucket
|
|
local directory = ngx.var.upyun_directory
|
|
if not filename then
|
|
filename = strip_path.strip_path(filepath)
|
|
end
|
|
|
|
ngx.log(ngx.INFO, 'bucket -> ', bucket)
|
|
ngx.log(ngx.INFO, 'directory -> ', directory)
|
|
|
|
local savePath = bucket .. "/" .. directory .. "/" .. filename
|
|
ngx.log(ngx.INFO, " savePath " , savePath)
|
|
|
|
local options = {
|
|
md5 = true
|
|
}
|
|
|
|
local info, err = upyun:upload_file(savePath, nil, options)
|
|
if not info then
|
|
ngx.status = 400
|
|
ngx.log(ngx.ERR, "failed to upload file : " .. err)
|
|
return
|
|
else
|
|
ngx.status = 200
|
|
local fullpath = ngx.var.upyun_domain .. "/" .. directory .. "/" .. filename
|
|
ngx.log(ngx.INFO, "SUCCESS UPLOAD -> UPYUN URL -> ", fullpath)
|
|
ngx.say(fullpath)
|
|
end
|
|
end
|
|
|
|
return upyun_upload |