Add upyun remove
This commit is contained in:
+4
-1
@@ -43,4 +43,7 @@ luac.out
|
||||
.DS_Store
|
||||
|
||||
# IDEA dir
|
||||
.idea/
|
||||
.idea/
|
||||
|
||||
# Test medie files
|
||||
*.mp3
|
||||
@@ -55,6 +55,11 @@ server {
|
||||
location /api/ngx/image/upload_combined {
|
||||
content_by_lua_file $resty_loc/nginx/scripts/upload_combined_image.lua;
|
||||
}
|
||||
|
||||
location /api/ngx/image/upyun_remove {
|
||||
content_by_lua_file $resty_loc/nginx/scripts/upyun_remove.lua;
|
||||
}
|
||||
|
||||
#Upload image files
|
||||
location /api/ngx/image/upload {
|
||||
content_by_lua_file $resty_loc/nginx/scripts/old_upload.lua;
|
||||
|
||||
@@ -50,6 +50,10 @@ server {
|
||||
content_by_lua_file $resty_loc/nginx/scripts/upload_combined_image.lua;
|
||||
}
|
||||
|
||||
location /api/ngx/image/upyun_remove {
|
||||
content_by_lua_file $resty_loc/nginx/scripts/upyun_remove.lua;
|
||||
}
|
||||
|
||||
#Upload image files
|
||||
location /api/ngx/image/upload {
|
||||
content_by_lua_file $resty_loc/nginx/scripts/old_upload.lua;
|
||||
|
||||
@@ -7,8 +7,8 @@ server {
|
||||
|
||||
set $tmp_file_dir "/usr/local/file_upload"; # 文件存储路径
|
||||
set $task_server "http://127.0.0.1:8080"; # task server host
|
||||
set $htyuc "http://127.0.0.1:3000"; #htyuc host
|
||||
# set $htyuc "https://admin.alchemy-studio.cn"; # Verified by admin.alchemy-studio.cn
|
||||
# set $htyuc "http://127.0.0.1:3000"; #htyuc host
|
||||
set $htyuc "https://admin.moicen.com"; #Verify jwt token
|
||||
set $resty_loc "/usr/local/opt/openresty";
|
||||
set $convert "/usr/local/bin/convert";
|
||||
set $upyun_operator "moicen";
|
||||
@@ -49,6 +49,11 @@ server {
|
||||
location /api/ngx/image/upload_combined {
|
||||
content_by_lua_file $resty_loc/nginx/scripts/upload_combined_image.lua;
|
||||
}
|
||||
|
||||
location /api/ngx/image/upyun_remove {
|
||||
content_by_lua_file $resty_loc/nginx/scripts/upyun_remove.lua;
|
||||
}
|
||||
|
||||
#Upload image files
|
||||
location /api/ngx/image/upload {
|
||||
content_by_lua_file $resty_loc/nginx/scripts/old_upload.lua;
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
#user nobody;
|
||||
worker_processes 1;
|
||||
|
||||
error_log /usr/local/etc/openresty/logs/error.log debug;
|
||||
# error_log /usr/local/etc/openresty/logs/error.log debug;
|
||||
#error_log logs/error.log notice;
|
||||
#error_log logs/error.log info;
|
||||
error_log logs/error.log info;
|
||||
|
||||
#pid logs/nginx.pid;
|
||||
|
||||
|
||||
@@ -103,6 +103,10 @@ server {
|
||||
content_by_lua_file $resty_loc/nginx/scripts/upload_combined_image.lua;
|
||||
}
|
||||
|
||||
location /api/ngx/image/upyun_remove {
|
||||
content_by_lua_file $resty_loc/nginx/scripts/upyun_remove.lua;
|
||||
}
|
||||
|
||||
#Audio file download
|
||||
location /api/ngx/audio/upload {
|
||||
content_by_lua_file $resty_loc/nginx/scripts/upload_audio.lua;
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
-- 指定模块引用目录,否则无法加载同目录下的其他文件
|
||||
--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
|
||||
|
||||
-- 使用 os.tmpname 获取临时文件名
|
||||
local tempFileName = "upyun_remove_temp_file"
|
||||
-- 拼接路径
|
||||
local tempFilePath = "/tmp/" .. tempFileName
|
||||
-- 使用 io.open 创建临时文件
|
||||
local tempFile = io.open(tempFilePath, "w")
|
||||
-- 写入内容到临时文件
|
||||
tempFile:write("upyun new config local temp file")
|
||||
-- 关闭文件
|
||||
tempFile:close()
|
||||
-- 使用 os.execute 执行 chmod 命令
|
||||
os.execute("chmod 777 " .. tempFilePath)
|
||||
--print("Upyun new config local temp file path : ", tempFilePath)
|
||||
|
||||
|
||||
local upyun, err = Upyun:new({
|
||||
user = ngx.var.upyun_operator,
|
||||
passwd = ngx.var.upyun_password,
|
||||
--localFilePath = filepath -- TODO : Add a fake file for create new upyun entry
|
||||
--localFilePath = "/Users/liyong/Code/AlchemyStudio/resty_functions/test_files_dir"
|
||||
localFilePath = tempFilePath
|
||||
})
|
||||
|
||||
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
|
||||
@@ -1,6 +1,6 @@
|
||||
-- 指定模块引用目录,否则无法加载同目录下的其他文件
|
||||
--package.path = package.path .. ';/usr/local/opt/openresty/nginx/scripts/?.lua';
|
||||
package.path = package.path .. ';<SCRIPT_PATH>/?.lua';
|
||||
package.path = package.path .. ';/usr/local/opt/openresty/nginx/scripts/?.lua';
|
||||
--package.path = package.path .. ';<SCRIPT_PATH>/?.lua';
|
||||
|
||||
local yun = require("lib.upyun")
|
||||
local cjson = require "cjson"
|
||||
@@ -8,19 +8,23 @@ local cjson = require "cjson"
|
||||
local function upyun_upload_file()
|
||||
|
||||
local config = {
|
||||
user = ngx.var.upyun_operator,
|
||||
passwd = ngx.var.upyun_password,
|
||||
localFilePath = "/file_upload/abc.mp3"
|
||||
--user = ngx.var.upyun_operator,
|
||||
--passwd = ngx.var.upyun_password,
|
||||
user = "moicen",
|
||||
passwd = "NyJ51zRwFApY9Wo9EHJMrb8GI9YtvpVN",
|
||||
--localFilePath = "/file_upload/abc.mp3"
|
||||
localFilePath = "/Users/liyong/Code/AlchemyStudio/resty_functions/test_files_dir/chunxiao.mp3"
|
||||
}
|
||||
|
||||
local upyun = yun:new(config)
|
||||
local bucket = ngx.var.upyun_bucket
|
||||
--local bucket = ngx.var.upyun_bucket
|
||||
local bucket = "moicen"
|
||||
local directory = 'test'
|
||||
local savePath = bucket .. "/" .. directory .. "/abc.mp3"
|
||||
local savePath = bucket .. "/" .. directory .. "/chunxiao.mp3"
|
||||
|
||||
local info, err = upyun:upload_file(savePath, nil, nil)
|
||||
if not info then
|
||||
ngx.say("failed to upload image file : " .. err)
|
||||
ngx.say("failed to upload file : " .. err)
|
||||
return
|
||||
else
|
||||
ngx.say(cjson.encode(info))
|
||||
@@ -28,4 +32,49 @@ local function upyun_upload_file()
|
||||
|
||||
end
|
||||
|
||||
upyun_upload_file()
|
||||
local function upyun_remove_file()
|
||||
|
||||
-- 使用 os.tmpname 获取临时文件名
|
||||
local tempFileName = "upyun_remove_temp_file"
|
||||
-- 拼接路径
|
||||
local tempFilePath = "/tmp/" .. tempFileName
|
||||
-- 使用 io.open 创建临时文件
|
||||
local tempFile = io.open(tempFilePath, "w")
|
||||
-- 写入内容到临时文件
|
||||
tempFile:write("upyun new config local temp file")
|
||||
-- 关闭文件
|
||||
tempFile:close()
|
||||
-- 使用 os.execute 执行 chmod 命令
|
||||
os.execute("chmod 777 " .. tempFilePath)
|
||||
--print("Upyun new config local temp file path : ", tempFilePath)
|
||||
|
||||
local config = {
|
||||
--user = ngx.var.upyun_operator,
|
||||
--passwd = ngx.var.upyun_password,
|
||||
user = "moicen",
|
||||
passwd = "NyJ51zRwFApY9Wo9EHJMrb8GI9YtvpVN",
|
||||
--localFilePath = "/file_upload/abc.mp3"
|
||||
--localFilePath = "/Users/liyong/Code/AlchemyStudio/resty_functions/test_files_dir/r2d2.jpeg"
|
||||
localFilePath = tempFilePath
|
||||
--localFilePath = nil
|
||||
}
|
||||
|
||||
local upyun = yun:new(config)
|
||||
--local bucket = ngx.var.upyun_bucket
|
||||
local bucket = "moicen"
|
||||
local directory = 'test'
|
||||
local removePath = bucket .. "/" .. directory .. "/chunxiao.mp3"
|
||||
|
||||
local info, err = upyun:remove_file(removePath)
|
||||
if not info then
|
||||
ngx.say("failed to remove file : " .. err)
|
||||
return
|
||||
else
|
||||
ngx.say(cjson.encode(info))
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
upyun_upload_file()
|
||||
|
||||
--upyun_remove_file()
|
||||
@@ -0,0 +1,33 @@
|
||||
package.path = package.path .. ';<SCRIPT_PATH>/?.lua';
|
||||
|
||||
local http = require "resty.http"
|
||||
local verify = require('lib.jwt_verify')
|
||||
local cjson = require "cjson"
|
||||
|
||||
local httpc = http:new()
|
||||
local authHeader = ngx.req.get_headers().Authorization
|
||||
local sudoerToken = ngx.req.get_headers().HtySudoerToken
|
||||
|
||||
ngx.req.read_body()
|
||||
local request_body = ngx.req.get_body_data()
|
||||
local request_body_json = cjson.decode(request_body)
|
||||
local upyun_remove_url = request_body_json["request_url"]
|
||||
ngx.log(ngx.INFO, 'upyun_remove_url -> ', upyun_remove_url)
|
||||
|
||||
verify(httpc, authHeader, sudoerToken)
|
||||
|
||||
ngx.log(ngx.INFO, 'REMOVE UPYUN FILE -> start')
|
||||
local upyun_upload = require("lib.upyun_remove")
|
||||
ngx.log(ngx.INFO, 'REMOVE UPYUN FILE -> upyun_remove loaded')
|
||||
|
||||
local file_to_remove = string.match(upyun_remove_url, ".*/(.*)")
|
||||
ngx.log(ngx.INFO, 'REMOVE UPYUN FILE -> file to remove -> ', file_to_remove)
|
||||
|
||||
--local file_dir = ngx.var.tmp_file_dir
|
||||
--local fullpath = file_dir .. "/" .. file_to_remove
|
||||
--
|
||||
--ngx.log(ngx.INFO, 'REMOVE UPYUN FILE -> fullpath -> ', fullpath)
|
||||
--
|
||||
--upyun_upload.remove(fullpath, nil, 0)
|
||||
|
||||
upyun_upload.remove(file_to_remove, file_to_remove, 0)
|
||||
Reference in New Issue
Block a user