音频转换及上传upyun
This commit is contained in:
@@ -22,6 +22,8 @@ nameserver 183.60.83.19
|
|||||||
[weli@moicen ~]$
|
[weli@moicen ~]$
|
||||||
```
|
```
|
||||||
|
|
||||||
|
使用上述nameserver的时候,请求upyun会提示no route to host错误,使用8.8.8.8偶尔也会出现
|
||||||
|
|
||||||
put `upload.lua` into:
|
put `upload.lua` into:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ server {
|
|||||||
set $htyuc "http://127.0.0.1:3000"; #htyuc host
|
set $htyuc "http://127.0.0.1:3000"; #htyuc host
|
||||||
set $resty_loc "/usr/local/openresty";
|
set $resty_loc "/usr/local/openresty";
|
||||||
set $convert "/usr/bin/convert";
|
set $convert "/usr/bin/convert";
|
||||||
|
set $upyun_cdn "https://upyun.alchemy-studio.cn/";
|
||||||
|
|
||||||
location / {
|
location / {
|
||||||
try_files $uri $uri/ /index.html;
|
try_files $uri $uri/ /index.html;
|
||||||
@@ -54,6 +55,10 @@ server {
|
|||||||
content_by_lua_file $resty_loc/nginx/scripts/convert_audio.lua;
|
content_by_lua_file $resty_loc/nginx/scripts/convert_audio.lua;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
location /api/ngx/convert/test {
|
||||||
|
content_by_lua_file $resty_loc/nginx/scripts/test.lua;
|
||||||
|
}
|
||||||
|
|
||||||
location /api/v1/ws/ {
|
location /api/v1/ws/ {
|
||||||
proxy_pass http://127.0.0.1:3001/api/v1/ws/;
|
proxy_pass http://127.0.0.1:3001/api/v1/ws/;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,8 +19,7 @@ server {
|
|||||||
set $upyun_password "NyJ51zRwFApY9Wo9EHJMrb8GI9YtvpVN";
|
set $upyun_password "NyJ51zRwFApY9Wo9EHJMrb8GI9YtvpVN";
|
||||||
set $upyun_bucket "huiwing";
|
set $upyun_bucket "huiwing";
|
||||||
set $upyun_directory "music-room";
|
set $upyun_directory "music-room";
|
||||||
set $upyun_cdn "https://upyun.alchemy-studio.cn/music-room/";
|
set $upyun_cdn "https://upyun.alchemy-studio.cn/";
|
||||||
set $upyun_api "https://v2.api.upyun.com";
|
|
||||||
|
|
||||||
location / {
|
location / {
|
||||||
try_files $uri $uri/ /index.html;
|
try_files $uri $uri/ /index.html;
|
||||||
|
|||||||
+35
-40
@@ -1,7 +1,11 @@
|
|||||||
|
-- 指定模块引用目录,否则无法加载同目录下的其他文件
|
||||||
|
package.path = package.path .. ';/usr/local/openresty/nginx/scripts/?.lua';
|
||||||
local cjson = require "cjson"
|
local cjson = require "cjson"
|
||||||
local uuid = require "resty.jit-uuid"
|
local uuid = require "resty.jit-uuid"
|
||||||
local http = require "resty.http"
|
local http = require "resty.http"
|
||||||
local upyun_token = require('upyun_token')
|
local strip_path = require("strip_path")
|
||||||
|
local Upyun = require('upyun')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local TaskTypes = {
|
local TaskTypes = {
|
||||||
@@ -24,7 +28,7 @@ local function get_access_token()
|
|||||||
ngx.log(ngx.INFO, "URL -> ", ngx.var.uri)
|
ngx.log(ngx.INFO, "URL -> ", ngx.var.uri)
|
||||||
|
|
||||||
|
|
||||||
local remote_url = string.format("%s/api/v1/uc/wx/get_access_token", "https://test-music-room.moicen.com")
|
local remote_url = string.format("%s/api/v1/uc/wx/get_access_token", htyuc)
|
||||||
ngx.log(ngx.INFO, 'remote_url -> ', remote_url)
|
ngx.log(ngx.INFO, 'remote_url -> ', remote_url)
|
||||||
ngx.log(ngx.INFO, 'HtyHostHeader -> ', htyhostHeader)
|
ngx.log(ngx.INFO, 'HtyHostHeader -> ', htyhostHeader)
|
||||||
ngx.log(ngx.INFO, 'HtySudoerToken -> ', sudoerToken)
|
ngx.log(ngx.INFO, 'HtySudoerToken -> ', sudoerToken)
|
||||||
@@ -35,10 +39,9 @@ local function get_access_token()
|
|||||||
ssl_verify = false, -- 设置参数 ssl_verify 为false 不校验ssl证书
|
ssl_verify = false, -- 设置参数 ssl_verify 为false 不校验ssl证书
|
||||||
method = "GET",
|
method = "GET",
|
||||||
headers = {
|
headers = {
|
||||||
["HtyHost"] = htyhostHeader,
|
["HtyHost"] = "wx.moicen.com",
|
||||||
["HtySudoerToken"] = sudoerToken,
|
["HtySudoerToken"] = sudoerToken,
|
||||||
},
|
}
|
||||||
body = body_text,
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
if res == nil then
|
if res == nil then
|
||||||
@@ -76,7 +79,7 @@ local function get_wx_media(media_id)
|
|||||||
ngx.exit(res.status)
|
ngx.exit(res.status)
|
||||||
end
|
end
|
||||||
|
|
||||||
local filename = ngx.re.match(res.headers['Content-disposition'], [[filename="(\w+\.(amr|speex))"]], "jo")[1]
|
local filename = ngx.re.match(res.headers['Content-disposition'], [[filename="(.+\.(amr|speex))"]], "jo")[1]
|
||||||
local file_dir = ngx.var.tmp_file_dir
|
local file_dir = ngx.var.tmp_file_dir
|
||||||
local saved_audio = file_dir .. "/" .. filename;
|
local saved_audio = file_dir .. "/" .. filename;
|
||||||
print('saved audio file...' .. saved_audio);
|
print('saved audio file...' .. saved_audio);
|
||||||
@@ -91,43 +94,37 @@ local function get_wx_media(media_id)
|
|||||||
return saved_audio;
|
return saved_audio;
|
||||||
end
|
end
|
||||||
|
|
||||||
local function upload(filename)
|
local function upload(filepath)
|
||||||
local upyun_api = ngx.var.upyun_api
|
local upyun, err = Upyun:new({
|
||||||
local token = upyun_token(filename)
|
user = ngx.var.upyun_operator,
|
||||||
local file, err = io.open(saved_audio, 'w')
|
password = ngx.var.upyun_password,
|
||||||
local form = {
|
localFilePath = filepath
|
||||||
["Authorization"] = token.token,
|
})
|
||||||
["file"] = file,
|
|
||||||
["policy"] = token.policy
|
if not upyun then
|
||||||
}
|
ngx.status = 500
|
||||||
local res, err = httpc:request_uri(
|
ngx.log(ngx.ERR, "failed to initialize upyun: " .. err)
|
||||||
upyun_api,
|
return
|
||||||
{
|
|
||||||
ssl_verify = false, -- 设置参数 ssl_verify 为false 不校验ssl证书
|
|
||||||
method = "POST",
|
|
||||||
body = form,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
if res == nil then
|
|
||||||
ngx.log(ngx.ERR, "FAILED TO CONNECT TO *Upyun*", err)
|
|
||||||
ngx.say(err)
|
|
||||||
ngx.exit(500)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if 200 ~= res.status then
|
local bucket = ngx.var.upyun_bucket
|
||||||
ngx.log(ngx.ERR, 'GET WECHET MEDIA *FAILED*', err)
|
local directory = ngx.var.upyun_directory
|
||||||
ngx.say(err)
|
local filename = strip_path.strip_path(filepath)
|
||||||
ngx.exit(res.status)
|
|
||||||
|
local savePath = bucket .. "/" .. directory .. "/" .. filename
|
||||||
|
|
||||||
|
local info, err = upyun:upload_file(savePath, nil, nil)
|
||||||
|
if not info then
|
||||||
|
ngx.status = 502
|
||||||
|
ngx.log(ngx.ERR, "failed to upload image file : " .. err)
|
||||||
|
return
|
||||||
|
else
|
||||||
|
ngx.status = 200
|
||||||
|
ngx.say(ngx.var.upyun_cdn .. directory .. "/" .. filename)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function convert()
|
local function convert()
|
||||||
-- mock for test
|
|
||||||
local res = cjson.encode({
|
|
||||||
r = true, d = "https://upyun.alchemy-studio.cn/daily-music/audios/Song-of-joy.mp3"
|
|
||||||
})
|
|
||||||
ngx.say(res)
|
|
||||||
ngx.exit(200)
|
|
||||||
|
|
||||||
local file_dir = ngx.var.tmp_file_dir
|
local file_dir = ngx.var.tmp_file_dir
|
||||||
ngx.req.read_body()
|
ngx.req.read_body()
|
||||||
@@ -147,13 +144,11 @@ local function convert()
|
|||||||
local result, _, code = os.execute(cmd)
|
local result, _, code = os.execute(cmd)
|
||||||
if result and code == 0 then
|
if result and code == 0 then
|
||||||
ngx.log(ngx.INFO, "result -> ", result);
|
ngx.log(ngx.INFO, "result -> ", result);
|
||||||
upload(result)
|
upload(converted_audio_file)
|
||||||
else
|
else
|
||||||
ngx.status = 500
|
ngx.status = 500
|
||||||
ngx.log(ngx.ERR, "AUDIO CONVERT *FAILED*")
|
ngx.log(ngx.ERR, "AUDIO CONVERT *FAILED*")
|
||||||
end
|
end
|
||||||
|
|
||||||
ngx.log(ngx.INFO, "Output audio file -> ", result);
|
|
||||||
end
|
end
|
||||||
|
|
||||||
convert();
|
convert();
|
||||||
|
|||||||
@@ -1,16 +1,18 @@
|
|||||||
|
package.path = package.path .. ';/usr/local/openresty/nginx/scripts/?.lua';
|
||||||
local yun = require("upyun")
|
local yun = require("upyun")
|
||||||
|
local cjson = require "cjson"
|
||||||
|
|
||||||
local function upyun_upload_file()
|
local function upyun_upload_file()
|
||||||
|
|
||||||
local config = {
|
local config = {
|
||||||
user = "moicen", --授权操作员名称
|
user = "moicen", --授权操作员名称
|
||||||
passwd = "NyJ51zRwFApY9Wo9EHJMrb8GI9YtvpVN", --操作员密码
|
passwd = "NyJ51zRwFApY9Wo9EHJMrb8GI9YtvpVN", --操作员密码
|
||||||
localFilePath = "/Users/liyong/Code/Lua/resty_functions/test_files_dir/r2d2.jpeg"
|
localFilePath = "/file_upload/abc.mp3"
|
||||||
}
|
}
|
||||||
|
|
||||||
local upyun = yun:new(config)
|
local upyun = yun:new(config)
|
||||||
|
|
||||||
local savePath = "huiwing/test/r2d2.jpeg"
|
local savePath = "huiwing/test/abc.mp3"
|
||||||
local gmkerl = nil
|
local gmkerl = nil
|
||||||
local options = nil
|
local options = nil
|
||||||
|
|
||||||
@@ -19,7 +21,7 @@ local function upyun_upload_file()
|
|||||||
ngx.say("failed to upload image file : " .. err)
|
ngx.say("failed to upload image file : " .. err)
|
||||||
return
|
return
|
||||||
else
|
else
|
||||||
ngx.say(info)
|
ngx.say(cjson.encode(info))
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
+6
-6
@@ -284,8 +284,8 @@ local function _request(sock, method, path, headers, body, extra)
|
|||||||
-- Build and send request header
|
-- Build and send request header
|
||||||
print("Debug upyun module request header method : ", method)
|
print("Debug upyun module request header method : ", method)
|
||||||
print("Debug upyun module request header path : ", path)
|
print("Debug upyun module request header path : ", path)
|
||||||
print("Debug upyun module request header headers : ", headers)
|
--print("Debug upyun module request header headers : ", headers)
|
||||||
print("Debug upyun module request header extra : ", extra)
|
--print("Debug upyun module request header extra : ", extra)
|
||||||
|
|
||||||
local header = _req_header(method, path, headers, extra)
|
local header = _req_header(method, path, headers, extra)
|
||||||
local bytes, err = sock:send(header)
|
local bytes, err = sock:send(header)
|
||||||
@@ -636,21 +636,21 @@ function _M.upload_file(self, path, gmkerl, option)
|
|||||||
if not ret then
|
if not ret then
|
||||||
return nil, err
|
return nil, err
|
||||||
end
|
end
|
||||||
print("Upyun module upload file request ret : ", ret)
|
--print("Upyun module upload file request ret : ", ret)
|
||||||
|
|
||||||
print("Upyun module upload file parse response headers : ", ret)
|
--print("Upyun module upload file parse response headers : ", ret)
|
||||||
ret, err = _parse_upyun_headers(ret.headers, [[^x-upyun-([\w-]+)$]])
|
ret, err = _parse_upyun_headers(ret.headers, [[^x-upyun-([\w-]+)$]])
|
||||||
if not ret then
|
if not ret then
|
||||||
return nil, err
|
return nil, err
|
||||||
end
|
end
|
||||||
print("Upyun module upload file parse response headers ret : ", ret)
|
--print("Upyun module upload file parse response headers ret : ", ret)
|
||||||
|
|
||||||
-- write the original author back as header.Authorization
|
-- write the original author back as header.Authorization
|
||||||
-- may be changed in the _upyun_request()
|
-- may be changed in the _upyun_request()
|
||||||
headers.Authorization = author
|
headers.Authorization = author
|
||||||
headers["Content-Length"] = "0"
|
headers["Content-Length"] = "0"
|
||||||
|
|
||||||
print("Upyun module upload file ret : ", ret)
|
--print("Upyun module upload file ret : ", ret)
|
||||||
return ret
|
return ret
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user