Files
resty_functions/upyun.lua
T
XiaoLi 6580d73d31 add upyun operation (#45)
* add upyun operation

* uft8 encoder fail

* calculate authorization ok

* clean up

* clean up
2022-01-13 19:37:01 +08:00

70 lines
2.6 KiB
Lua

strip_path = require("strip_path")
local uuid = require "resty.jit-uuid"
uuid.seed()
local cjson = require "cjson"
-- Upyun upload parameters
local upyun_operator = ngx.var.$upyun_operator
local upyun_passwd = ngx.var.upyun_passwd
local upyun_expiration = 1800
local upyun_method = "POST"
local function calculate_authorization(bucket, remote_dir, local_filename_with_path)
-- Get filename with extension without path
local upyun_filename = strip_path.strip_path(local_filename_with_path)
ngx.log(ngx.INFO, "UPYUN FILENAME -> ", upyun_filename)
-- Connect upyun save key
local upyun_save_key = "/" .. remote_dir .. "/" .. upyun_filename
ngx.log(ngx.INFO, "UPYUN SAVE KEY -> ", upyun_save_key)
-- Get RFC1123 data
local upyun_date = ngx.http_time(ngx.time()) -- RFC1123 date
ngx.log(ngx.INFO, "UPYUN DATE RFC1123 FORMAT -> ", upyun_date)
-- Generate upyun UCT time
local upyun_utc_time = os.time(os.date("!*t"))
ngx.log(ngx.INFO, "UPYUN UTC TIME -> ", upyun_utc_time)
-- Calculate upyun file expiration
local upyun_file_expiration = upyun_utc_time + upyun_expiration
ngx.log(ngx.INFO, "UPYUN FILE EXPIRATION -> ", upyun_file_expiration)
local upyun_policy_json = string.format("{\"bucket\":\"%s\",\"save-key\":\"%s\",\"expiration\":\"%s\",\"date\":\"%s\"}", bucket, upyun_save_key,upyun_file_expiration,upyun_date)
ngx.log(ngx.INFO, "UPYUN POLICY JSON -> ", upyun_policy_json)
-- Calculate upyun policy
local upyun_policy = ngx.encode_base64(upyun_policy_json)
ngx.say("UPYUN POLICY -> ", upyun_policy)
---- Calculate MD5 passwd
local upyun_md5_passwd = ngx.md5(upyun_passwd)
ngx.log(ngx.INFO, "UPYUN MD5 PASSWD -> ", upyun_md5_passwd)
---- Connect to be signed string
local upyun_to_be_signed_string = upyun_method .. "&" .. "/" .. bucket .. "&" .. upyun_date .. "&" .. upyun_policy
ngx.log(ngx.INFO, "UPYUN TO BE SIGNED STRING -> ", upyun_to_be_signed_string)
-- Calculate upyun token hmac sha1
local upyun_token_hmac_sha1 = ngx.hmac_sha1(upyun_md5_passwd, upyun_to_be_signed_string)
--ngx.log(ngx.INFO, "UPYUN TOKEN HMAC SHA1 -> ", upyun_token_hmac_sha1)
-- Calculate upyun signature
local upyun_signature = ngx.encode_base64(upyun_token_hmac_sha1)
ngx.log(ngx.INFO, "UPYUN SIGNATURE -> ", upyun_signature)
-- Connect upyun upload file authorization
local upyun_upload_file_authorization = string.format("UPYUN %s:%s", upyun_operator, upyun_signature)
ngx.log(ngx.INFO, "UPYUN UPLOAD FILE AUTHORIZATION -> ", upyun_upload_file_authorization)
return upyun_upload_file_authorization
end