Merge pull request #50 from xiaolitongxue666/upyun_upload_file

Upyun upload file
This commit is contained in:
XiaoLi
2022-01-15 21:36:36 +08:00
committed by GitHub
3 changed files with 51 additions and 3 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

+27
View File
@@ -0,0 +1,27 @@
local yun = require("upyun")
local function upyun_upload_file()
local config = {
user = "moicen", --授权操作员名称
passwd = "NyJ51zRwFApY9Wo9EHJMrb8GI9YtvpVN", --操作员密码
localFilePath = "/Users/liyong/Code/Lua/resty_functions/test_files_dir/r2d2.jpeg"
}
local upyun = yun:new(config)
local savePath = "huiwing/test/r2d2.jpeg"
local gmkerl = nil
local options = nil
local info, err = upyun:upload_file(savePath, gmkerl, options)
if not info then
ngx.say("failed to upload image file : " .. err)
return
else
ngx.say(info)
end
end
upyun_upload_file()
+24 -3
View File
@@ -270,6 +270,8 @@ local function _request(sock, method, path, headers, body, extra)
local host = headers.Host
if not sock then
return nil, "not initialized yet"
else
print("Upyun module request connect host : ", host)
end
sock:settimeout(5000)
@@ -280,6 +282,11 @@ local function _request(sock, method, path, headers, body, extra)
end
-- Build and send request header
print("Debug upyun module request header method : ", method)
print("Debug upyun module request header path : ", path)
print("Debug upyun module request header headers : ", headers)
print("Debug upyun module request header extra : ", extra)
local header = _req_header(method, path, headers, extra)
local bytes, err = sock:send(header)
if not bytes then
@@ -513,6 +520,7 @@ function _M.new(self, config)
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"
@@ -536,9 +544,18 @@ function _M.new(self, config)
end
-- file to be uploaded is stored in the request body
read_body()
local content = get_body_data()
local file = ngx.req.get_body_file()
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
@@ -619,17 +636,21 @@ function _M.upload_file(self, path, gmkerl, option)
if not ret then
return nil, err
end
print("Upyun module upload file request ret : ", ret)
print("Upyun module upload file parse response headers : ", ret)
ret, err = _parse_upyun_headers(ret.headers, [[^x-upyun-([\w-]+)$]])
if not ret then
return nil, err
end
print("Upyun module upload file parse response headers ret : ", ret)
-- write the original author back as header.Authorization
-- may be changed in the _upyun_request()
headers.Authorization = author
headers["Content-Length"] = "0"
print("Upyun module upload file ret : ", ret)
return ret
end