前言:在使用腾讯云COS上传对象时,需要先获取《临时密钥》,然后再生成《临时签名》,然后携带临时签名上传即可。
整体架构图如下所示:
其中:
应用 APP:即用户手机上的 App。
COS:腾讯云对象存储,负责存储 App 上传的数据。
CAM:腾讯云访问管理,用于生成 COS 的临时密钥。
应用服务器:用户自己的后台服务器,这里用于获取临时密钥,并返回给应用 App。
一、临时密钥服务器的作用
1、上传对象到COS腾讯云时,需要携带临时签名。生成临时签名前需要先获取临时密钥。
上传对象到COS腾讯云代码如下:
// 签名回调
var getAuthorization = function (options, callback) {
// 格式一、(推荐)后端通过获取临时密钥给到前端,前端计算签名
// 服务端 JS 和 PHP 例子:https://github.com/tencentyun/cos-js-sdk-v5/blob/master/server/
// 服务端其他语言参考 COS STS SDK :https://github.com/tencentyun/qcloud-cos-sts-sdk
wx.request({
method: 'GET',
//服务端签名,参考 server 目录下的两个签名例子
//url: 'http://demo.sts.cn/sts.php',
url: 'http://demo.sts.cn:3000/sts', //nodeJS方式获取临时密钥
dataType: 'json',
success: function (result) {
var data = result.data;
var credentials = data.credentials;
console.log("获取临时密钥", JSON.stringify(credentials));
callback({
TmpSecretId: credentials.tmpSecretId,
TmpSecretKey: credentials.tmpSecretKey,
XCosSecurityToken: credentials.sessionToken,
ExpiredTime: data.expiredTime, // SDK 在 ExpiredTime 时间前,不会再次调用 getAuthorization
});
}
});
}
//获取签名
var cos = new COS({
getAuthorization: getAuthorization
});
//上传按钮
option.simpleUpload = function () {
that = this; //这里定义 this
// 选择文件
wx.chooseImage({
count: 1, // 默认9
sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], //可以指定来源是相册还是相机,默认二者都有
success: function (res) {
var file = res.tempFiles[0];
var filepath = file.path;
// 这里指定上传的文件名 key 是自动生成的随机无重复文件名
var Key = filepath.substr(filepath.lastIndexOf('/') + 1);
cos.postObject({
Bucket: config.Bucket,
Region: config.Region,
Key: "001/" + Key, //上传COS 的路径 和 文件唯一名
FilePath: filepath,
//进度的回调函数(进度条)
onProgress: function (info) {
console.log(JSON.stringify(info));
}
}, function(err, data){
//获取返回结果
console.log(err || data);
});
}//success
})//wx.chooseImage
};
2、(推荐)后端通过获取临时密钥给到前端,前端计算签名:
服务端获取密钥的方式有四种语言:
https://github.com/tencentyun/qcloud-cos-sts-sdk
其中,服务端 JS 和 PHP 例子:
https://github.com/tencentyun/cos-js-sdk-v5/blob/master/server/
说明:server/ 下 sts.js 为部署NodeJs 临时密钥服务。
https://github.com/tencentyun/cos-wx-sdk-v5/blob/master/server/sts.js
nodejs 部署临时密钥服务的源码和demo:
https://github.com/tencentyun/qcloud-cos-sts-sdk/tree/master/nodejs
二、如何生成临时签名
在获取临时密钥之后,通过临时密钥生成临时签名:生成临时签名的方法有两种:
1、API方式生成
需要先引入“cos-auth.min.js”获取签名文件。
H5引入方法:
<script src="./lib/cos-auth.js" alt="此文件为计算签名"></script>
小程序引入方法:
var CosAuth = require('./lib/cos-auth');
github的Demo地址:github.com/tencentyun/…
cos-auth文件地址:github.com/tencentyun/…
API方式生成临时签名和上传对象到COS的示例代码:
var CosAuth = require('./lib/cos-auth');
//获取临时密钥
var getCredentials = function(callback) {
wx.request({
method: 'GET',
// 服务端签名,参考 server 目录下的两个签名例子
url: config.stsUrl,
dataType: 'json',
success: function (result) {
console.log("获取临时密钥///" + JSON.stringify(result.data.credentials));
var data = result.data;
callback(data.credentials);
},
error: function(err) {
wx.showModal('临时密钥获取失败');
}
});
};
// 计算签名
var getAuthorization = function (options, callback) {
getCredentials(function (credentials) {
callback({
XCosSecurityToken: credentials.sessionToken,
Authorization: CosAuth({
SecretId: credentials.tmpSecretId,
SecretKey: credentials.tmpSecretKey,
Method: options.Method,
Pathname: options.Pathname,
})
});
});
};
// 上传文件
var uploadFile = function (filePath) {
// 这里指定上传的文件名
var Key = filePath.substr(filePath.lastIndexOf('/') + 1);
var _this = this;
getAuthorization({
Method: 'POST',
Pathname: '/'
},
function(AuthData) {
console.log("计算签名///", JSON.stringify(AuthData));
var requestTask = wx.uploadFile({
url: prefix,
name: 'file',
filePath: filePath,
formData: {
'key': '001/' + Key,
'success_action_status': 200,
'Signature': AuthData.Authorization,
'x-cos-security-token': AuthData.XCosSecurityToken,
'Content-Type': '',
},
success: function(res) {
//获取上传后的文件名
var url = prefix + camSafeUrlEncode(Key).replace(/%2F/g, '/');
if (res.statusCode === 200) {
console.log('上传成功',res);
} else {
console.log('上传失败',res);
}
},
fail: function(err) {
console.log('上传失败',err);
}
});
requestTask.onProgressUpdate(function(res) {
console.log('正在进度:', res);
});
});
};
// 选择文件
wx.chooseImage({
count: 1, // 默认9
sizeType: ['original'],
sourceType: ['album', 'camera'],
success: function (res) {
uploadFile(res.tempFiles[0].path);
}
});
上传对象
小程序上传接口 wx.uploadFile 只支持 POST 请求,SDK 上传文件则需要使用 postObject 接口,如果小程序里只需要用到上传文件接口,建议不引用 SDK,请直接参考简单例子 demo。
2、引用SDK方式生成
H5方式引入:
<script src="../dist/cos-js-sdk-v5.min.js" alt="此文件是SDK"></script>
小程序方式引入:
cos-js-sdk-v5:github.com/tencentyun/…https://github.com/tencentyun/cos-js-sdk-v5/blob/master/dist/cos-js-sdk-v5.min.js
腾讯云 COS 小程序 SDK:github.com/tencentyun/…把 demo/lib/cos-wx-sdk-v5.js 复制到自己小程序项目代码里
SDK方式生成临时签名和上传对象到COS的示例代码:
var COS = require('../demo-sdk/lib/cos-wx-sdk-v5')
// 签名回调
var getAuthorization = function (options, callback) {
// 格式一、(推荐)后端通过获取临时密钥给到前端,前端计算签名
// 服务端 JS 和 PHP 例子:https://github.com/tencentyun/cos-js-sdk-v5/blob/master/server/
// 服务端其他语言参考 COS STS SDK :https://github.com/tencentyun/qcloud-cos-sts-sdk
wx.request({
method: 'GET',
// 服务端签名,参考 server 目录下的两个签名例子
//url: 'http://demo.sts.cn/sts.php',
url: 'http://demo.sts.cn:3000/sts', //nodeJS方式获取临时密钥
dataType: 'json',
success: function (result) {
var data = result.data;
var credentials = data.credentials;
console.log("获取临时密钥", JSON.stringify(credentials));
callback({
TmpSecretId: credentials.tmpSecretId,
TmpSecretKey: credentials.tmpSecretKey,
XCosSecurityToken: credentials.sessionToken,
ExpiredTime: data.expiredTime, // SDK 在 ExpiredTime 时间前,不会再次调用 getAuthorization
});
}
});
}
var cos = new COS({
getAuthorization: getAuthorization
});
var requestCallback =function (err, data) {
console.log(err || data);
//获取带秘钥的图片地址
option.getObjectUrl(data.Location);
};
//上传按钮
option.simpleUpload = function () {
that = this; //这里定义 this
// 选择文件
wx.chooseImage({
count: 1, // 默认9
sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success: function (res) {
var file = res.tempFiles[0];
var filepath = file.path;
// 这里指定上传的文件名 key 是自动生成的随机无重复文件名
var Key = filepath.substr(filepath.lastIndexOf('/') + 1);
cos.postObject({
Bucket: config.Bucket,
Region: config.Region,
Key: "001/" + Key, //上传COS 的路径 和 文件唯一名
FilePath: filepath,
onProgress: function (info) { //进度的回调函数(进度条)
console.log(JSON.stringify(info));
}
}, requestCallback);
}
})
};
option.getObjectUrl = function(url){
var getUrl = url.substr(url.lastIndexOf('/') + 1);
cos.getObjectUrl({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Key: '001/' + getUrl,
Expires: 60,
Sign: true,
Expires: 3600, // 单位秒
}, function (err, data) {
console.log(err || data);
console.log("getObjectUrl///", data.Url)
//这里设置data
that.setData({
cosImage: data.Url
})
});
}
//获取应用实例
Page(option);
注意:引入SDK,可动 COS存储桶(Bucket)进入操作,例如:下载对象,设置对象访问权限,删除对象等权限操作。
三、关于上传COS对象的权限操作
COS存储对象分为:私有读写、公有读私有写、公有读写。
用户权限:主账号默认拥有存储桶所有权限(即完全控制)。
1、私有读写:无论是上传对象和访问对象时,都需要携带临时签名认证,认证通过后方可操作成功,否则返回:403 【AccessDenied】
2、公有读私有写:上传需要携带临时签名,访问时不需要,可直接展示。
3、公有读写:上传和访问都不需要携带临时签名(只有调试时才使用)
错误码查询:cloud.tencent.com/document/pr…
注意:API只有上传和访问权限,没有操作Bucket的权限。只有 SDK在服务端取得权限后方可操作Bucket。
服务端临时密钥设置权限:
当访问COS时返回:403,AccessDenied 时:其中一种可能是 服务端临时密钥没有设置权限,权限设置如下:
// 所有 action 请看文档 https://cloud.tencent.com/document/product/436/31923
// 简单上传
'name/cos:PutObject',
'name/cos:PostObject',
// 这样写可以包含所有权限
'name/cos:*', // 临时密钥服务例子
var STS = require('qcloud-cos-sts');
var bodyParser = require('body-parser');
var express = require('express');
// 配置参数
var config = {
secretId: 'AKIDxxxxx',
secretKey: 'wxtxxxxx',
proxy: '',
durationSeconds: 1800,
bucket: '001-125000000',
region: 'ap-beijing',
allowPrefix: '001/*',
// 密钥的权限列表
allowActions: [
// 所有 action 请看文档 https://cloud.tencent.com/document/product/436/31923
// 简单上传
'name/cos:PutObject',
'name/cos:PostObject',
'name/cos:*', // 这样写可以包含下面所有权限
// 分片上传
'name/cos:InitiateMultipartUpload',
'name/cos:ListMultipartUploads',
'name/cos:ListParts',
'name/cos:UploadPart',
'name/cos:CompleteMultipartUpload'
],
};
四、临时密钥服务端部署方法
PHP部署方法很简单,只要把 "sts.php/qcloud-sts-sdk.php" 文件上传到PHP服务器环境中,更改sts.php中的配置信息,即可访问。
需要注意:
获取临时密钥文件中,需要包含头部信息:
PHP如下:
// 获取临时密钥,计算签名
$tempKeys = $sts->getTempKeys($config);
// 返回数据给前端
header('Content-Type: application/json');
// http://127.0.0.1 可更改为 * 号,表示所有域都可访问
header('Access-Control-Allow-Origin: http://127.0.0.1'); // 这里修改允许跨域访问的网站
header('Access-Control-Allow-Headers: origin,accept,content-type');
echo json_encode($tempKeys);
nodeJS如下:
// 创建临时密钥服务
var app = express();
app.use(bodyParser.json());
// 支持跨域访问
// http://127.0.0.1 可更改为 * 号,表示所有域都可访问
app.all('*', function (req, res, next) {
res.header('Content-Type', 'application/json');
res.header('Access-Control-Allow-Origin', '*'); //http://127.0.0.1:88
res.header('Access-Control-Allow-Headers', 'origin,accept,content-type');
if (req.method.toUpperCase() === 'OPTIONS') {
res.end();
} else {
next();
}
});
以下重点讲解NodeJS部署方法:
以下以CentOS系统为例:
首先上传 sts.js 到服务器nodejs 环境中,修改 sts.js 中的配置信息。
sts.js:https://github.com/tencentyun/cos-wx-sdk-v5/blob/master/server/sts.js
gitHub:https://github.com/tencentyun/qcloud-cos-sts-sdk/tree/master/nodejs
首先安装模块:
进入 /usr/local/lib
npm i qcloud-cos-sts --save安装 qcloud-cos-sts 模块到 node_modules 文件夹中。
然后 # node sts.js 运行!
如果出现:
[root@VM_0_12_centos sts]# npm i qcloud-cos-sts --save
npm WARN engine request@2.88.0: wanted: {"node":">= 4"} (current: {"node":"0.12.7","npm":"
npm WARN engine har-validator@5.1.3: wanted: {"node":">=6"} (current: {"node":"0.12.7","np)
npm WARN engine har-schema@2.0.0: wanted: {"node":">=4"} (current: {"node":"0.12.7","npm":
npm WARN engine punycode@2.1.1: wanted: {"node":">=6"} (current: {"node":"0.12.7","npm":"2
qcloud-cos-sts@3.0.0 node_modules/qcloud-cos-sts
└── request@2.88.0 (is-typedarray@1.0.0, aws-sign2@0.7.0, oauth-sign@0.9.0, forever-agent@l-agent@0.6.0, caseless@0.12.0, isstream@0.1.2, json-stringify-safe@5.0.1, safe-buffer@5.2.0, extend@3.0.2, performance-now@2.1.0, uuid@3.3.2, qs@6.5.2, combined-stream@1.0.8, mime4, form-data@2.3.3, tough-cookie@2.4.3, http-signature@1.2.0, har-validator@5.1.3)
[root@VM_0_12_centos m.doubia.cn]# node -v
v0.12.7说明nodeJS版本不对,需要更换版本,更换成 nodeJS 稳定版最新版本即可。
继续运行:# node sts.js
如果出现:
[root@VM_0_12_centos sts]# node sts.js
internal/modules/cjs/loader.js:638
throw err;
^
Error: Cannot find module 'body-parser'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Module.require (internal/modules/cjs/loader.js:690:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/data/www/sts/sts.js:3:18)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
[root@VM_0_12_centos m.doubia.cn]# npm i qcloud-cos-sts --save
npm WARN saveError ENOENT: no such file or directory, open '/data/www/sts/package.
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '/data/www/sts/package.jso
npm WARN m.doubia.cn No description
npm WARN m.doubia.cn No repository field.
npm WARN m.doubia.cn No README data
npm WARN m.doubia.cn No license field.
+ qcloud-cos-sts@3.0.0
updated 1 package, moved 48 packages and audited 64 packages in 5.955s
found 0 vulnerabilities
说明 sts.js 中所需的模块没有安装到 nodeJS 的 node_modules文件夹中。
进入nodeJS 的 node_modules文件夹
1、全局安装 Express
# npm install -g express2、全局安装 body-parser
# npm install --save body-parser然后继续 # node sts.js 运行!
出现:
app is listening at http://127.0.0.1:3000即可部署完毕!
CentOS 部署 sts.js 服务操作纪录
[root@VM_0_12_centos sts]# npm i qcloud-cos-sts --save
npm WARN engine request@2.88.0: wanted: {"node":">= 4"} (current: {"node":"0.12.7","npm":"
npm WARN engine har-validator@5.1.3: wanted: {"node":">=6"} (current: {"node":"0.12.7","np)
npm WARN engine har-schema@2.0.0: wanted: {"node":">=4"} (current: {"node":"0.12.7","npm":
npm WARN engine punycode@2.1.1: wanted: {"node":">=6"} (current: {"node":"0.12.7","npm":"2
qcloud-cos-sts@3.0.0 node_modules/qcloud-cos-sts
└── request@2.88.0 (is-typedarray@1.0.0, aws-sign2@0.7.0, oauth-sign@0.9.0, forever-agent@l-agent@0.6.0, caseless@0.12.0, isstream@0.1.2, json-stringify-safe@5.0.1, safe-buffer@5.2.0, extend@3.0.2, performance-now@2.1.0, uuid@3.3.2, qs@6.5.2, combined-stream@1.0.8, mime4, form-data@2.3.3, tough-cookie@2.4.3, http-signature@1.2.0, har-validator@5.1.3)
[root@VM_0_12_centos sts]# node -v
v0.12.7
[root@VM_0_12_centos sts]# n
node/10.16.0
[root@VM_0_12_centos sts]# node -v
v0.12.7
[root@VM_0_12_centos sts]# n stable1
Error: invalid version 'stable1'
[root@VM_0_12_centos sts]# n
[root@VM_0_12_centos sts]# export NODE_HOME=/usr/local
[root@VM_0_12_centos sts]# export PATH=$NODE_HOME/bin:$PATH
[root@VM_0_12_centos sts]# export NODE_PATH=$NODE_HOME/lib/node_modules:$PATH
[root@VM_0_12_centos sts]# ll
total 16
-rw-r--r-- 1 root root 936 Jul 18 11:24 getauth.js
drwxr-xr-x 3 root root 4096 Jul 18 11:27 node_modules
-rw-r--r-- 1 root root 4282 Jul 17 16:55 sts.js
[root@VM_0_12_centos sts]# node -v
v10.16.0
[root@VM_0_12_centos sts]# ll
total 16
-rw-r--r-- 1 root root 936 Jul 18 11:24 getauth.js
drwxr-xr-x 3 root root 4096 Jul 18 11:27 node_modules
-rw-r--r-- 1 root root 4282 Jul 17 16:55 sts.js
[root@VM_0_12_centos sts]# node getauth.js
{ expiredTime: 1563422766,
expiration: '2019-07-18T04:06:06Z',
credentials:
{ sessionToken:
'lgSGE9tQ2gznQthItHWrFK_Y9KNaPfsdafasfasfas3457676fasdfer78gdwerewrZa',
tmpSecretId:
'AKID2Stt-6eRjCvA_vaM_LAsXu_XtTgn',
tmpSecretKey: 'Q6Y9epBg8D+aOwlmap5YBvKN=' },
requestId: '66d2-fa63-4458-8d7d-37e88',
startTime: 1563420966 }
[root@VM_0_12_centos sts]# ll
total 16
-rw-r--r-- 1 root root 936 Jul 18 11:24 getauth.js
drwxr-xr-x 3 root root 4096 Jul 18 11:27 node_modules
-rw-r--r-- 1 root root 4282 Jul 17 16:55 sts.js
[root@VM_0_12_centos sts]# node sts.js
internal/modules/cjs/loader.js:638
throw err;
^
Error: Cannot find module 'body-parser'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Module.require (internal/modules/cjs/loader.js:690:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/data/www/sts/sts.js:3:18)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
[root@VM_0_12_centos sts]# npm i qcloud-cos-sts --save
npm WARN saveError ENOENT: no such file or directory, open '/data/www/sts/package.
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '/data/www/sts/package.jso
npm WARN sts No description
npm WARN sts No repository field.
npm WARN sts No README data
npm WARN sts No license field.
+ qcloud-cos-sts@3.0.0
updated 1 package, moved 48 packages and audited 64 packages in 5.955s
found 0 vulnerabilities
[root@VM_0_12_centos sts]# ll
total 32
-rw-r--r-- 1 root root 936 Jul 18 11:24 getauth.js
drwxr-xr-x 51 root root 4096 Jul 18 11:42 node_modules
-rw-r--r-- 1 root root 13271 Jul 18 11:42 package-lock.json
-rw-r--r-- 1 root root 4282 Jul 17 16:55 sts.js
[root@VM_0_12_centos sts]# node sts.js
internal/modules/cjs/loader.js:638
throw err;
^
Error: Cannot find module 'body-parser'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Module.require (internal/modules/cjs/loader.js:690:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/data/www/sts/sts.js:3:18)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
[root@VM_0_12_centos sts]# ll
total 32
-rw-r--r-- 1 root root 936 Jul 18 11:24 getauth.js
drwxr-xr-x 51 root root 4096 Jul 18 11:42 node_modules
-rw-r--r-- 1 root root 13271 Jul 18 11:42 package-lock.json
-rw-r--r-- 1 root root 4282 Jul 17 16:55 sts.js
[root@VM_0_12_centos sts]# ll
total 32
-rw-r--r-- 1 root root 936 Jul 18 11:24 getauth.js
drwxr-xr-x 51 root root 4096 Jul 18 11:42 node_modules
-rw-r--r-- 1 root root 13271 Jul 18 11:42 package-lock.json
-rw-r--r-- 1 root root 4282 Jul 17 16:55 sts.js
[root@VM_0_12_centos lib]# cd node_modules
[root@VM_0_12_centos node_modules]# ll
total 4
drwxr-xr-x 10 root root 4096 Jul 18 11:34 npm
[root@VM_0_12_centos node_modules]# cd /data/www/
[root@VM_0_12_centos www]# ll
total 12
drwxr-xr-x 6 root root 4096 Jun 14 14:10 2002800
drwxr-xr-x 7 root root 4096 Jun 13 17:00 linux.doubia.cn
drwxr-xr-x 4 root root 4096 Jul 18 11:42 sts
[root@VM_0_12_centos www]# cd sts
[root@VM_0_12_centos sts]# ll
total 32
-rw-r--r-- 1 root root 936 Jul 18 11:24 getauth.js
drwxr-xr-x 51 root root 4096 Jul 18 11:42 node_modules
-rw-r--r-- 1 root root 13271 Jul 18 11:42 package-lock.json
-rw-r--r-- 1 root root 4282 Jul 17 16:55 sts.js
[root@VM_0_12_centos sts]# rm -rf node_modules
[root@VM_0_12_centos sts]# ll
total 28
-rw-r--r-- 1 root root 936 Jul 18 11:24 getauth.js
-rw-r--r-- 1 root root 13271 Jul 18 11:42 package-lock.json
-rw-r--r-- 1 root root 4282 Jul 17 16:55 sts.js
[root@VM_0_12_centos sts]# rm -f package-lock.json
[root@VM_0_12_centos sts]# ll
total 12
-rw-r--r-- 1 root root 936 Jul 18 11:24 getauth.js
-rw-r--r-- 1 root root 4282 Jul 17 16:55 sts.js
[root@VM_0_12_centos sts]# cd /usr/local/lib
[root@VM_0_12_centos lib]# ll
total 4
drwxr-xr-x 3 root root 4096 Jul 18 11:34 node_modules
[root@VM_0_12_centos lib]# npm i qcloud-cos-sts --save
+ qcloud-cos-sts@3.0.0
added 49 packages from 60 contributors in 7.558s
╭────────────────────────────────────────────────────────────────╮
│ │
│ New minor version of npm available! 6.9.0 → 6.10.1 │
│ Changelog: https://github.com/npm/cli/releases/tag/v6.10.1 │
│ Run npm install -g npm to update! │
│ │
╰────────────────────────────────────────────────────────────────╯
[root@VM_0_12_centos lib]# New minor version of npm available! 6.9.0 → 6.10.1 │
-bash: New: command not found
[root@VM_0_12_centos lib]# │ Changelog: https://github.com/npm/cli/releases/tag/v6.10
-bash: $'\342\224\202': command not found
[root@VM_0_12_centos lib]# npm install -g npm
/usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js
/usr/local/bin/npx -> /usr/local/lib/node_modules/npm/bin/npx-cli.js
+ npm@6.10.1
added 19 packages from 13 contributors, removed 12 packages and updated 32 packages in 9.0
[root@VM_0_12_centos lib]# ll
total 4
drwxr-xr-x 4 root root 4096 Jul 18 11:57 node_modules
[root@VM_0_12_centos lib]# cd node_modules
[root@VM_0_12_centos node_modules]# ll
total 8
drwxr-xr-x 10 root root 4096 Jul 18 11:57 npm
drwxr-xr-x 5 root root 4096 Jul 18 11:56 qcloud-cos-sts
[root@VM_0_12_centos node_modules]# cd qcloud-cos-sts
[root@VM_0_12_centos qcloud-cos-sts]# ll
total 28
drwxr-xr-x 2 root root 4096 Jul 18 11:56 demo
-rw-r--r-- 1 root root 56 Oct 26 1985 index.js
drwxr-xr-x 50 root root 4096 Jul 18 11:56 node_modules
-rw-r--r-- 1 root root 1539 Jul 18 11:56 package.json
-rw-r--r-- 1 root root 5036 Oct 26 1985 README.md
drwxr-xr-x 2 root root 4096 Jul 18 11:56 sdk
[root@VM_0_12_centos qcloud-cos-sts]# cd /data/www/sts
[root@VM_0_12_centos sts]# ll
total 12
-rw-r--r-- 1 root root 936 Jul 18 11:24 getauth.js
-rw-r--r-- 1 root root 4282 Jul 17 16:55 sts.js
[root@VM_0_12_centos sts]# ll
total 12
-rw-r--r-- 1 root root 936 Jul 18 11:24 getauth.js
-rw-r--r-- 1 root root 4282 Jul 17 16:55 sts.js
[root@VM_0_12_centos sts]# cd /usr/local/lib
[root@VM_0_12_centos lib]# ll
total 4
drwxr-xr-x 4 root root 4096 Jul 18 11:57 node_modules
[root@VM_0_12_centos lib]# npm install --save body-parser
+ body-parser@1.19.0
added 22 packages from 17 contributors in 5.187s
[root@VM_0_12_centos lib]# ll
total 4
drwxr-xr-x 5 root root 4096 Jul 18 12:30 node_modules
[root@VM_0_12_centos lib]# cd node_modules
[root@VM_0_12_centos node_modules]# ll
total 12
drwxr-xr-x 4 root root 4096 Jul 18 12:30 body-parser
drwxr-xr-x 10 root root 4096 Jul 18 11:57 npm
drwxr-xr-x 5 root root 4096 Jul 18 11:56 qcloud-cos-sts
[root@VM_0_12_centos node_modules]# cd /data/www/sts
[root@VM_0_12_centos sts]# ll
total 12
-rw-r--r-- 1 root root 936 Jul 18 11:24 getauth.js
-rw-r--r-- 1 root root 4282 Jul 17 16:55 sts.js
[root@VM_0_12_centos sts]# node sts.js
warning: the default view engine will not be jade in future releases
warning: use `--view=jade' or `--help' for additional options
/data/www/sts/sts.js:35
var app = express();
^
TypeError: express is not a function
at Object.<anonymous> (/data/www/sts/sts.js:35:11)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
[root@VM_0_12_centos sts]# cd /usr/local/lib
[root@VM_0_12_centos lib]# ll
total 4
drwxr-xr-x 5 root root 4096 Jul 18 12:30 node_modules
[root@VM_0_12_centos lib]# npm install -g express
+ express@4.17.1
added 50 packages from 37 contributors in 8.859s
[root@VM_0_12_centos lib]# cd /data/www/sts
[root@VM_0_12_centos sts]# ll
total 12
-rw-r--r-- 1 root root 936 Jul 18 11:24 getauth.js
-rw-r--r-- 1 root root 4282 Jul 17 16:55 sts.js
[root@VM_0_12_centos sts]# node sts.js
app is listening at http://127.0.0.1:3000