add upyun operation (#45)

* add upyun operation

* uft8 encoder fail

* calculate authorization ok

* clean up

* clean up
This commit is contained in:
XiaoLi
2022-01-13 19:37:01 +08:00
committed by GitHub
parent c9ef8eff33
commit 6580d73d31
5 changed files with 125 additions and 1 deletions
+6
View File
@@ -86,6 +86,12 @@ weli@ovpn-12-178:/u/l/o/o/n/resty_funcs
```
Pre-Install lua third module
```bash
luarocks install lua-resty-jit-uuid
luarocks install lua-resty-jwt
```
+4
View File
@@ -50,6 +50,10 @@ server {
location /api/ngx/audio/convert {
content_by_lua_file $resty_loc/resty_funcs/convert_audio.lua;
}
#Upyun token
location /api/ngx/upyun_token{
content_by_lua_file $resty_loc/resty_funcs/upyun_token.lua;
}
#Static file server
location /file_upload {
default_type "";
+9 -1
View File
@@ -49,7 +49,11 @@ server {
set $task_server "http://127.0.0.1:8080"; # task server host
set $htyuc "http://127.0.0.1:3000"; #htyuc host
set $resty_loc "/usr/local/opt/openresty"; # MacOS
set $upyun_operator "moicen";
set $upyun_passwd = "NyJ51zRwFApY9Wo9EHJMrb8GI9YtvpVN";
#set $resty_loc "/usr/local/opt/openresty"; # MacOS
set $resty_loc "/usr/local/openresty"; # CentOS
set $convert "/usr/local/bin/convert";
location / {
@@ -87,6 +91,10 @@ server {
location /api/ngx/audio/convert {
content_by_lua_file $resty_loc/resty_funcs/convert_audio.lua;
}
#Upyun token
location /api/ngx/upyun_token{
content_by_lua_file $resty_loc/resty_funcs/upyun_token.lua;
}
#Static file server
location /file_upload {
default_type "";
+37
View File
@@ -0,0 +1,37 @@
local strip_path = {}
--获取路径
function strip_path.strip_filename(filename)
return string.match(filename, "(.+)/[^/]*%.%w+$") --*nix system
--return string.match(filename, “(.+)\\[^\\]*%.%w+$”) — windows
end
--获取文件名
function strip_path.strip_path(filename)
return string.match(filename, ".+/([^/]*%.%w+)$") -- *nix system
--return string.match(filename, “.+\\([^\\]*%.%w+)$”) — *nix system
end
--去除扩展名
function strip_path.strip_extension(filename)
local idx = filename:match(".+()%.%w+$")
if(idx) then
return filename:sub(1, idx-1)
else
return filename
end
end
--获取扩展名
function strip_path.get_extension(filename)
return filename:match(".+%.(%w+)$")
end
-- Test strip_path module
--local paths = "/use/local/openresty/nginx/movies/fffff.tar.gz"
--print (strip_path.strip_filename(paths))
--print (strip_path.strip_path(paths))
--print (strip_path.strip_extension(paths))
--print (strip_path.get_extension(paths))
return strip_path
+69
View File
@@ -0,0 +1,69 @@
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