本文檔介紹騰訊雲·萬象優圖服務端nodejs的部署和集成,搭建一個nodejs+nginx為基礎,對web端或者移動端提供http簽名接口服務的例子程序。
注意:本文檔只是簡單的示例,展示了服務端為終端提供簽名的基本示例,開發者務必根據自身業務開發相應的鑒權服務邏輯,並集成到自身服務器中。
1 環境准備
下面以在騰訊雲雲服務器CentOS 6.2 64位上安裝nginx為例,簡單介紹如何將騰訊雲萬象優圖集成,對web端或者移動端提供http簽名接口服務所需要的基礎環境搭建。開發者可以根據自己業務的需要,構建http或者非http服務,為自身業務的web端、移動端提供簽名。
1.1 安裝nginx
yum install nginx –y service nginx restart
1.2 驗證nginx
直接訪問雲服務器ip地址,驗證nginx是否已經運行起來。
2 安裝配置Nodejs環境
下面介紹安裝Nodejs和配置web container的詳細步驟。
1 安裝Nodejs
yum install -y nodejs npm
2 配置web container
修改/etc/nginx/conf.d/default.conf如下:
# # The default server # server { listen 80 default_server; server_name _; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location ^~ /node/ { proxy_pass http://localhost:9002; } }
3 重新加載nginx配置
修改配置完成后,需要執行以下命令重新加載配置。
nginx -s reload
3 安裝Nodejs SDK
執行以下命令安裝Nodejs SDK。
cd /data/www/tencentyun/node npm install tencentyun
4 開發鑒權服務邏輯
將sdk集成到開發者代碼,開發鑒權服務邏輯,這里以node目錄下getsignv2.js為例(開發者務必根據自身業務開發相應的鑒權服務邏輯):
注意:如果開發者想按照本示例做簡單地測試,需要將下面代碼中的相應字段替換為自己的項目信息,具體見代碼注釋。
var http=require('http'); var url = require('url'); var util = require('util'); var tencentyun = require('tencentyun'); var server=new http.Server(); server.on('request',function(req,res){ var urlinfo = url.parse(req.url,true), type = 'upload'; if (urlinfo.query && urlinfo.query.type) { type = urlinfo.query.type; } //請將下面的bucket, projectId, secretId和secretKey替換成開發者自己的項目信息 var bucket = 'test0706', projectId = '10000037', userid = 0, secretId = 'AKIDpoKBfMK7aYcYNlqxnEtYA1ajAqji2P7T', secretKey = 'P4FewbltIpGeAbwgdrG6eghMUVlpmjIe'; tencentyun.conf.setAppInfo(projectId, secretId, secretKey); var error = false; switch(type) { case 'upload': var fileid = '/u/can/use/slash/sample' + Math.round(+new Date()/1000), expired = Math.round(+new Date()/1000) + 999, uploadurl = tencentyun.imagev2.generateResUrlV2(bucket, userid, fileid), sign = tencentyun.auth.getAppSignV2(bucket, fileid, expired); ret = {'sign':sign,'url':uploadurl}; break; case 'stat': if (!urlinfo.query || !urlinfo.query.fileid) { error = true; } else { var fileid = decodeURIComponent(urlinfo.query.fileid), otherurl = tencentyun.imagev2.generateResUrlV2(bucket, userid, fileid), ret = {'url':otherurl}; } break; case 'del': case 'copy': if (!urlinfo.query || !urlinfo.query.fileid) { error = true; } else { var fileid = decodeURIComponent(urlinfo.query.fileid), otherurl = tencentyun.imagev2.generateResUrlV2(bucket, userid, fileid, type), sign = tencentyun.auth.getAppSignV2(bucket, fileid, 0); ret = {'sign':sign,'url':otherurl}; } break; case 'download': if (!urlinfo.query || !urlinfo.query.fileid) { error = true; } else { var fileid = decodeURIComponent(urlinfo.query.fileid), expired = Math.round(+new Date()/1000) + 999, sign = tencentyun.auth.getAppSignV2(bucket, fileid, expired); ret = {'sign':sign}; } break; } res.writeHead(200,{'Content-Type':'application/json'}); if (error) { res.end({'error':'params error'}); } else { res.end(JSON.stringify(ret)); } }); server.listen(9002); console.log('HTTP SERVER is LISTENING AT PORT 9002.');
5 運行程序
cd /data/www/tencentyun/node nohup node getsignv2.js &
6 測試
- 終端通過CGI:http://203.195.194.28/node/?type=[opType]&fileid=[fileid]來獲取相應的簽名。
opType:可取值:upload(上傳), stat(查詢), copy(復制), del(刪除)和download(下載,如果開啟token防盜鏈);
fileid:是圖片資源的唯一標識;當opType為upload時,如果開發者沒有指定fileid,fileid置空,否則指定為相應的fileid;下載簽名,fileid可以空,也可以為開發者查看的圖片fileid。
注意: 下載簽名只有開發者在控制台上面設置了token防盜鏈時才使用,如果沒有token防盜鏈,不需要下載簽名,直接使用下載url下載圖片。
示例:http://203.195.194.28/node/?type=del&fileid=sample123 http://203.195.194.28/node/?type=copy&fileid=sample123 http://203.195.194.28/node/?type=stat&fileid=sample123 http://203.195.194.28/node/?type=download&fileid=sample123 http://203.195.194.28/node/?type=upload&fileid=sample123
