Add upyun new2

This commit is contained in:
XiaoLi
2024-01-03 23:18:02 +08:00
committed by XiaoLi
parent ffab75c78a
commit dd27afb222
3 changed files with 115 additions and 33 deletions
+81
View File
@@ -574,6 +574,87 @@ function _M.new(self, config)
}, mt) }, mt)
end end
function _M.new2(self, config)
local user = config.user
local passwd = config.passwd
local endpoint = config.endpoint and tonumber(config.endpoint) + 1 or 1
local author = config.author and lower(config.author) or nil
local localFilePath = config.localFilePath or nil -- add config para for upload file directly
if not user or type(user) ~= "string" or user == "" then
return nil, "invalid user"
end
if not passwd or type(passwd) ~= "string" or passwd == "" then
return nil, "invalid passwd"
end
if endpoint > 4 then
return nil, "invalid endpoint"
end
-- explicit "basic" sets author_mode to Basic
local author_mode = "U"
if author == "basic" then
author_mode = "B"
author = "Basic " .. base64(user .. ":" .. passwd)
else
author = "UpYun " .. user .. ":"
end
---- file to be uploaded is stored in the request body
--print("Upyun module new config para local file path : ", localFilePath)
--local file = nil
--local content = nil
--if localFilePath == nil then
-- read_body()
-- content = get_body_data()
-- file = ngx.req.get_body_file()
--else
-- file = localFilePath;
--end
--
--print("Upyun module new config file : ", file)
--if file then
-- local f, err = io.open(file, "r")
-- if not f then
-- return nil, err
-- end
--
-- content = f:read("*a")
-- f:close()
--end
--if not content then
-- return nil, "request body is expected"
--end
--TODO ngx.updatetime?
local date = http_time(time_sec())
if not date then
return nil, "failed to get current time"
end
local sock = tcp()
if not sock then
return nil, "failed to create a TCP socket"
end
return setmetatable({
sock = sock,
user = user,
passwd = passwd,
author_mode = author_mode,
headers = {
Authorization = author,
Host = host_list[endpoint],
Date = date,
["Content-Length"] = "0"
},
body = { content = content },
}, mt)
end
function _M.upload_file(self, path, gmkerl, option) function _M.upload_file(self, path, gmkerl, option)
-- return info, err -- return info, err
local headers = self.headers local headers = self.headers
+16 -15
View File
@@ -19,27 +19,28 @@ function upyun_remove.remove(filepath, filename, retry)
ngx.log(ngx.ERR, "failed to remove file : reach max retries") ngx.log(ngx.ERR, "failed to remove file : reach max retries")
else else
-- 使用 os.tmpname 获取临时文件名 ---- 使用 os.tmpname 获取临时文件名
local tempFileName = "upyun_remove_temp_file" --local tempFileName = "upyun_remove_temp_file"
-- 拼接路径 ---- 拼接路径
local tempFilePath = "/tmp/" .. tempFileName --local tempFilePath = "/tmp/" .. tempFileName
-- 使用 io.open 创建临时文件 ---- 使用 io.open 创建临时文件
local tempFile = io.open(tempFilePath, "w") --local tempFile = io.open(tempFilePath, "w")
-- 写入内容到临时文件 ---- 写入内容到临时文件
tempFile:write("upyun new config local temp file") --tempFile:write("upyun new config local temp file")
-- 关闭文件 ---- 关闭文件
tempFile:close() --tempFile:close()
-- 使用 os.execute 执行 chmod 命令 ---- 使用 os.execute 执行 chmod 命令
os.execute("chmod 777 " .. tempFilePath) --os.execute("chmod 777 " .. tempFilePath)
--print("Upyun new config local temp file path : ", tempFilePath) ----print("Upyun new config local temp file path : ", tempFilePath)
local upyun, err = Upyun:new({ local upyun, err = Upyun:new2({
user = ngx.var.upyun_operator, user = ngx.var.upyun_operator,
passwd = ngx.var.upyun_password, passwd = ngx.var.upyun_password,
--localFilePath = filepath -- TODO : Add a fake file for create new upyun entry --localFilePath = filepath -- TODO : Add a fake file for create new upyun entry
--localFilePath = "/Users/liyong/Code/AlchemyStudio/resty_functions/test_files_dir" --localFilePath = "/Users/liyong/Code/AlchemyStudio/resty_functions/test_files_dir"
localFilePath = tempFilePath --localFilePath = tempFilePath
localFilePath = nil
}) })
if not upyun then if not upyun then
+18 -18
View File
@@ -34,19 +34,19 @@ end
local function upyun_remove_file() local function upyun_remove_file()
-- 使用 os.tmpname 获取临时文件名 ---- 使用 os.tmpname 获取临时文件名
local tempFileName = "upyun_remove_temp_file" --local tempFileName = "upyun_remove_temp_file"
-- 拼接路径 ---- 拼接路径
local tempFilePath = "/tmp/" .. tempFileName --local tempFilePath = "/tmp/" .. tempFileName
-- 使用 io.open 创建临时文件 ---- 使用 io.open 创建临时文件
local tempFile = io.open(tempFilePath, "w") --local tempFile = io.open(tempFilePath, "w")
-- 写入内容到临时文件 ---- 写入内容到临时文件
tempFile:write("upyun new config local temp file") --tempFile:write("upyun new config local temp file")
-- 关闭文件 ---- 关闭文件
tempFile:close() --tempFile:close()
-- 使用 os.execute 执行 chmod 命令 ---- 使用 os.execute 执行 chmod 命令
os.execute("chmod 777 " .. tempFilePath) --os.execute("chmod 777 " .. tempFilePath)
--print("Upyun new config local temp file path : ", tempFilePath) ----print("Upyun new config local temp file path : ", tempFilePath)
local config = { local config = {
--user = ngx.var.upyun_operator, --user = ngx.var.upyun_operator,
@@ -55,11 +55,11 @@ local function upyun_remove_file()
passwd = "NyJ51zRwFApY9Wo9EHJMrb8GI9YtvpVN", passwd = "NyJ51zRwFApY9Wo9EHJMrb8GI9YtvpVN",
--localFilePath = "/file_upload/abc.mp3" --localFilePath = "/file_upload/abc.mp3"
--localFilePath = "/Users/liyong/Code/AlchemyStudio/resty_functions/test_files_dir/r2d2.jpeg" --localFilePath = "/Users/liyong/Code/AlchemyStudio/resty_functions/test_files_dir/r2d2.jpeg"
localFilePath = tempFilePath --localFilePath = tempFilePath
--localFilePath = nil localFilePath = nil
} }
local upyun = yun:new(config) local upyun = yun:new2(config)
--local bucket = ngx.var.upyun_bucket --local bucket = ngx.var.upyun_bucket
local bucket = "moicen" local bucket = "moicen"
local directory = 'test' local directory = 'test'
@@ -75,6 +75,6 @@ local function upyun_remove_file()
end end
upyun_upload_file() --upyun_upload_file()
--upyun_remove_file() upyun_remove_file()