Files
resty_functions/scripts/test_upyun_upload.lua
T

80 lines
2.4 KiB
Lua
Raw Normal View History

2022-02-08 13:57:15 +08:00
-- 指定模块引用目录,否则无法加载同目录下的其他文件
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';
2022-02-08 13:57:15 +08:00
2022-06-28 22:47:38 +08:00
local yun = require("lib.upyun")
2022-01-28 02:25:18 +08:00
local cjson = require "cjson"
2022-01-15 18:40:22 +08:00
2022-01-15 21:32:55 +08:00
local function upyun_upload_file()
2022-01-15 18:40:22 +08:00
2022-01-15 21:32:55 +08:00
local config = {
2024-01-03 22:14:25 +08:00
--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"
2022-01-15 21:32:55 +08:00
}
2022-01-15 18:40:22 +08:00
2022-01-15 21:32:55 +08:00
local upyun = yun:new(config)
2024-01-03 22:14:25 +08:00
--local bucket = ngx.var.upyun_bucket
local bucket = "moicen"
2022-01-28 21:24:53 +08:00
local directory = 'test'
2024-01-03 22:14:25 +08:00
local savePath = bucket .. "/" .. directory .. "/chunxiao.mp3"
2022-01-15 21:32:55 +08:00
2022-01-28 21:24:53 +08:00
local info, err = upyun:upload_file(savePath, nil, nil)
2022-01-15 21:32:55 +08:00
if not info then
2024-01-03 22:14:25 +08:00
ngx.say("failed to upload file : " .. err)
2022-01-15 21:32:55 +08:00
return
else
2022-01-28 02:25:18 +08:00
ngx.say(cjson.encode(info))
2022-01-15 21:32:55 +08:00
end
2022-01-15 18:40:22 +08:00
end
2022-01-15 21:32:55 +08:00
2024-01-03 22:14:25 +08:00
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()