local http = require("socket.http") local ltn12 = require("ltn12") local lfs = require "lfs" http.TIMEOUT = 5 local function upload_file ( url, filename ) local fileHandle = io.open( filename,"rb") if (fileHandle) then local fileContent = fileHandle:read( "*a" ) fileHandle:close() local boundary = 'abcd' local header_b = 'Content-Disposition: form-data; name="file"; filename="' .. filename .. '"\r\nContent-Type: text/plain\r\n' local fileContent = '--' ..boundary .. '\r\n' ..header_b ..'\r\n'.. fileContent .. '\r\n--' .. boundary ..'--\r\n' local response_body = { } local _, code = http.request { url = url , method = "POST", headers = { ["Content-Length"] = fileContent:len(), ['Content-Type'] = 'multipart/form-data; boundary=' .. boundary }, source = ltn12.source.string(fileContent) , sink = ltn12.sink.table(response_body), } return code, table.concat(response_body) else return false, "File Not Found" end end local rc,content = upload_file ('http://127.0.0.1:826/api/upload', 'test.png' ) print(rc,content) local function calc_token() -- Upyun upload parameters local operator = "operator123" local password = "password123" local bucket = "upyun-temp" local method = "POST" local save_key = "/demo.jpg" -- Get RFC1123 data local date = "Wed, 09 Nov 2016 14:26:58 GMT" -- RFC1123 date -- Generate upyun UCT time local utc_time = os.time(os.date("!*t")) print("utc_time...", utc_time, ngx.http_time(ngx.time())) -- Calculate upyun file expiration local expiration = "1478674618" local content_md5 = "7ac66c0f148de9519b8bd264312c4d64" local policy = string.format("{\"bucket\":\"%s\",\"save-key\":\"%s\",\"expiration\":\"%s\",\"date\":\"%s\",\"content-md5\":\"%s\"}", bucket, save_key, expiration, date, content_md5) print("policy....", policy, ngx.encode_base64(policy)) local to_be_signed = method .. "&" .. "/" .. bucket .. "&" .. date .. "&" .. ngx.encode_base64(policy) .. '&' .. content_md5 print('to be signed...', to_be_signed) print('md5 pwd...', ngx.md5(password)) local hmac_sha1 = ngx.hmac_sha1(ngx.md5(password), to_be_signed) local signature = ngx.encode_base64(hmac_sha1) print("generated... " .. signature .. ", expected... k+fHTJndCFAraoeIrd60sJ/8Vb8=") end calc_token();