Files
resty_functions/scripts/lib/upyun_remove.lua
T

81 lines
2.5 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
2024-01-21 02:21:21 +08:00
ngx.status = 404
2024-01-21 02:15:31 +08:00
local msg = "failed to remove file " .. filepath .. " reach max retries"
ngx.log(ngx.ERR, msg)
ngx.say(msg)
2024-01-03 22:14:25 +08:00
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
2024-01-07 21:17:04 +08:00
--filename = strip_path.strip_path(filepath)
filename = filepath
2024-01-03 22:14:25 +08:00
end
ngx.log(ngx.INFO, 'bucket -> ', bucket)
ngx.log(ngx.INFO, 'directory -> ', directory)
2024-01-21 03:02:40 +08:00
local removePath = bucket .. "/" .. directory .. "/" .. filename
2024-01-03 22:14:25 +08:00
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)
2024-01-21 02:15:31 +08:00
ngx.log(ngx.ERR, "Upyun Remove File Error: " .. filepath .. " e: " .. err .. " " .. '[' .. error_table["code"] .. ']')
2024-01-03 22:14:25 +08:00
if (error_table["code"] == 40000006)
2024-01-21 02:17:08 +08:00
then
2024-01-03 22:14:25 +08:00
ngx.log(ngx.ERR, "Retry remove file : " .. '[' .. err .. ']')
upyun_remove.remove(filepath, filename, retry+1)
else
2024-01-21 02:21:21 +08:00
ngx.status = 404
2024-01-21 02:15:31 +08:00
local msg = "failed to remove file : " .. filepath .. ' [' .. err .. ']'
ngx.log(ngx.ERR, msg)
ngx.say(msg)
2024-01-03 22:14:25 +08:00
end
else
ngx.status = 200
2024-01-21 03:02:40 +08:00
local fullpath = ngx.var.upyun_domain .. "/" .. directory .. "/" .. filename
2024-01-03 22:14:25 +08:00
ngx.log(ngx.INFO, "SUCCESS REMOVE -> UPYUN URL -> ", fullpath)
ngx.say(fullpath)
end
end
end
return upyun_remove