Files
resty_functions/scripts/lib/upyun_remove.lua
T

77 lines
2.3 KiB
Lua
Raw Normal View History

2024-01-03 22:14:25 +08:00
-- 指定模块引用目录,否则无法加载同目录下的其他文件
--package.path = package.path .. ';/usr/local/opt/openresty/nginx/scripts/?.lua';
package.path = package.path .. ';<SCRIPT_PATH>/?.lua';
local upyun_remove = {}
local json = require("cjson")
local strip_path = require("lib.strip_path")
local Upyun = require('lib.upyun')
function upyun_remove.remove(filepath, filename, retry)
if retry == nil then
retry = 1
end
if retry > 3 then
ngx.log(ngx.ERR, "failed to remove file : reach max retries")
else
2024-01-03 23:18:02 +08:00
local upyun, err = Upyun:new2({
2024-01-03 22:14:25 +08:00
user = ngx.var.upyun_operator,
passwd = ngx.var.upyun_password,
2024-01-03 23:18:02 +08:00
localFilePath = nil
2024-01-03 22:14:25 +08:00
})
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 removePath = bucket .. "/" .. directory .. "/" .. filename
ngx.log(ngx.INFO, " removePath " , removePath)
--local options = {
-- md5 = true
--}
ngx.log(ngx.INFO, ' ========>>>>> ', removePath)
local info, err = upyun:remove_file(removePath)
if not info then
local error_table = json.decode(err)
ngx.log(ngx.INFO, "Upyun Remove File Error: " .. err)
ngx.log(ngx.ERR, '[' .. error_table["code"] .. ']')
if (error_table["code"] == 40000006)
then
ngx.status = 400
ngx.log(ngx.ERR, "Retry remove file : " .. '[' .. err .. ']')
upyun_remove.remove(filepath, filename, retry+1)
else
ngx.status = 400
ngx.log(ngx.ERR, "failed to remove file : " .. '[' .. err .. ']')
end
else
ngx.status = 200
local fullpath = ngx.var.upyun_domain .. "/" .. directory .. "/" .. filename
ngx.log(ngx.INFO, "SUCCESS REMOVE -> UPYUN URL -> ", fullpath)
ngx.say(fullpath)
end
end
end
return upyun_remove