2022-02-08 00:25:20 +08:00
|
|
|
-- 指定模块引用目录,否则无法加载同目录下的其他文件
|
2022-02-08 13:57:15 +08:00
|
|
|
--package.path = package.path .. ';/usr/local/opt/openresty/nginx/scripts/?.lua';
|
2023-07-23 21:38:56 +08:00
|
|
|
--package.path = package.path .. ';/Users/liyong/Code/AlchemyStudio/resty_functions/scripts/?.lua';
|
2022-02-08 14:14:57 +08:00
|
|
|
package.path = package.path .. ';<SCRIPT_PATH>/?.lua';
|
2022-02-08 00:25:20 +08:00
|
|
|
|
|
|
|
|
local upyun_upload = {}
|
|
|
|
|
|
2023-05-08 00:45:53 +08:00
|
|
|
local strip_path = require("lib.strip_path")
|
|
|
|
|
local Upyun = require('lib.upyun')
|
2022-02-08 00:25:20 +08:00
|
|
|
|
2022-06-26 17:49:50 +08:00
|
|
|
function upyun_upload.upload(filepath, filename)
|
2022-02-08 00:25:20 +08:00
|
|
|
local upyun, err = Upyun:new({
|
|
|
|
|
user = ngx.var.upyun_operator,
|
|
|
|
|
passwd = ngx.var.upyun_password,
|
2023-07-23 21:38:56 +08:00
|
|
|
--user = "moicen",
|
|
|
|
|
--passwd = "NyJ51zRwFApY9Wo9EHJMrb8GI9YtvpVN",
|
2022-02-08 00:25:20 +08:00
|
|
|
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
|
2023-07-23 21:38:56 +08:00
|
|
|
--local bucket = "huiwing"
|
|
|
|
|
--local directory = "music-room"
|
2022-06-26 17:49:50 +08:00
|
|
|
if not filename then
|
|
|
|
|
filename = strip_path.strip_path(filepath)
|
|
|
|
|
end
|
2022-02-08 00:25:20 +08:00
|
|
|
|
2023-01-17 19:39:08 +08:00
|
|
|
ngx.log(ngx.INFO, 'bucket -> ', bucket)
|
|
|
|
|
ngx.log(ngx.INFO, 'directory -> ', directory)
|
|
|
|
|
|
2022-02-08 00:25:20 +08:00
|
|
|
local savePath = bucket .. "/" .. directory .. "/" .. filename
|
2023-01-17 19:39:08 +08:00
|
|
|
ngx.log(ngx.INFO, " savePath " , savePath)
|
2022-02-08 00:25:20 +08:00
|
|
|
|
2023-07-23 21:38:56 +08:00
|
|
|
local options = {
|
|
|
|
|
md5 = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
local info, err = upyun:upload_file(savePath, nil, options)
|
2022-02-08 00:25:20 +08:00
|
|
|
if not info then
|
2023-07-23 21:38:56 +08:00
|
|
|
ngx.status = 400
|
2022-02-08 00:25:20 +08:00
|
|
|
ngx.log(ngx.ERR, "failed to upload file : " .. err)
|
|
|
|
|
return
|
|
|
|
|
else
|
|
|
|
|
ngx.status = 200
|
2022-08-31 23:34:54 +08:00
|
|
|
local fullpath = ngx.var.upyun_domain .. "/" .. directory .. "/" .. filename
|
2023-07-23 21:38:56 +08:00
|
|
|
--local fullpath = "https://upyun.huiwings.cn" .. "/" .. directory .. "/" .. filename
|
2023-07-23 00:15:17 +08:00
|
|
|
ngx.log(ngx.INFO, "SUCCESS UPLOAD -> UPYUN URL -> ", fullpath)
|
2023-07-23 21:38:56 +08:00
|
|
|
--ngx.say("SUCCESS UPLOAD -> UPYUN URL -> ", fullpath)
|
2022-03-09 03:09:11 +08:00
|
|
|
ngx.say(fullpath)
|
2022-02-08 00:25:20 +08:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return upyun_upload
|