Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Qcloud_CDN_API/lua/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## description

This is the **Lua** version of the open api demo, which implements the *refresh_cdn_url* api based on **OpenResty**.

## third-part package

```bash
opm get ledgetech/lua-resty-http
opm get jkeys089/lua-resty-hmac
```

## usage

Add and modify your code

```lua
# nginx.conf:

location = /refresh_cdn_url {
content_by_lua_file conf/lua/refresh_cdn_url.lua;
}

- refresh_cdn_url:
local qcloud_secret_id = '' -- pls use your secret id
local qcloud_secret_key = '' -- pls use your secret key
```

post demo

```json
# POST ****/refresh_cdn_url

{
"urls": [
"https://xxxx/index.html"
"https://xxxx/yyyy.png",
]
}
```

140 changes: 140 additions & 0 deletions Qcloud_CDN_API/lua/refresh_cdn_url.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
local ngx = require 'ngx'
local cjson = require 'cjson.safe'
local hmac = require "resty.hmac"
local http = require 'resty.http'

local ok, new_tab = pcall(require, 'table.new')
if not ok then
new_tab = function(narr, nreci) return {} end
end

local get_post_json = function()
if 'POST' ~= ngx.req.get_method() then
ngx.exit(406)
end

-- get post body
ngx.req.read_body()
local body = ngx.req.get_body_data()

if nil == body then
ngx.log(ngx.ERR, "body is null")
ngx.exit(406)
end

local data = cjson.decode(body)
if nil == data then
ngx.exit(406)
end

return data
end

local protocol = 'https://'
local host = 'cdn.api.qcloud.com'
local uri = '/v2/index.php'
local action = 'RefreshCdnUrl'
local method = 'POST' -- can use GET or POST
local qcloud_secret_id = '' -- pls use your secret id
local qcloud_secret_key = '' -- pls use your secret key

local params = {
['Action'] = action,
['SecretId'] = qcloud_secret_id,
['Timestamp'] = ngx.time(),
['Nonce'] = math.random(1000),
}

local urls = get_post_json()['urls']
if 'table' ~= type(urls) then
ngx.exit(406)
end

for index, url in ipairs(urls) do
params['urls.' .. tostring(index - 1)] = url
end

-- 1. Sort parameters

local keys = new_tab(#params, 0)
local index = 1
for k, _ in pairs(params) do
keys[index] = k
index = index + 1
end

table.sort(keys)

-- 2. Concatenate request string

local tmp = new_tab(#keys, 0)
for i, k in ipairs(keys) do
tmp[i] = k .. "=" .. tostring(params[k])
end

local sig_params = table.concat(tmp, '&')

-- 3. Concatenate the original signature string
local sig_url = method .. host .. uri .. '?' .. sig_params

-- 4. Generate signature string

local hmac_sha1 = hmac:new(qcloud_secret_key, hmac.ALGOS.SHA1)
if not hmac_sha1 then
ngx.log(ngx.ERR, 'failed to create the hmac_sha1 object')
ngx.exit(500)
end

ok = hmac_sha1:update(sig_url)
if not ok then
ngx.log(ngx.ERR, 'failed to add data')
ngx.exit(500)
end

local mac = hmac_sha1:final()
local base64 = ngx.encode_base64(mac) .. '='
params['Signature'] = base64

-- 5. Send http request

local httpc = http:new()
httpc:set_timeouts(30 * 1000, 30 * 1000, 30 * 1000)

local request_url = protocol .. host .. uri
local res, err
if method == 'GET' then
res, err = httpc:request_uri(request_url, {
method = method,
query = params,
})
else
res, err = httpc:request_uri(request_url, {
method = method,
body = sig_params .. '&Signature=' .. base64,
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
}
})
end

httpc:close()

if not res then
ngx.log(ngx.ERR, 'refresh_cdn_url: request failed', err)
ngx.exit(500)
end

if 200 ~= res.status then
ngx.log(ngx.ERR, 'refresh_cdn_url: response header status code is not 200')
ngx.exit(500)
end

local res_body = cjson.decode(res.body)
if not res_body['code'] or tonumber(res_body['code']) ~= 0 then
ngx.log(ngx.ERR, 'refresh_cdn_url: response body status code is not 0, body: ', res.body)
ngx.exit(500)
end

ngx.header['Content-Type'] = 'application/json'
ngx.say(res.body)
ngx.exit(200)
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* [php](https://github.com/QCloudCDN/CDN_API_SDK/tree/master/Qcloud_CDN_API/php)
* [python](https://github.com/QCloudCDN/CDN_API_SDK/tree/master/Qcloud_CDN_API/python)
* [java](https://github.com/QCloudCDN/CDN_API_SDK/tree/master/Qcloud_CDN_API/java/cdn_openapi_demo/src)
* [lua](https://github.com/QCloudCDN/CDN_API_SDK/tree/master/Qcloud_CDN_API/lua)

## API 概览

Expand Down
Loading