-
Notifications
You must be signed in to change notification settings - Fork 3
/
cos.js
205 lines (168 loc) · 5.58 KB
/
cos.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
cos.js
Copyright (c) 2013
PuterJam
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var request = require('request'),
fs = require('fs'),
events = require('events'),
util = require('util'),
url = require("url");
var Sign = require('./lib/sign'),
bucket = require('./lib/api/bucket'),
file = require('./lib/api/file'),
upload = require('./lib/api/upload'),
initOpt = require("./lib/util").initOpt;
/**
* cos api host
* @type {string}
*/
var cos_host = "cosapi.myqcloud.com";
//COS_HOST_INNER = "cosapi.tencentyun.com"; //用于测试环境测试
//var cosToken;
/**
* 初始化COS接口
*
* @param {Object} Obj 包含accessId,secretId,secretKey的json数据
* @type {Class}
* @returns {COS} 返回cos对象
*/
var COS = function (Obj) {
if (!Obj || !Obj.accessId || !Obj.secretKey) {
throw new Error("invalid app access key.");
}
this.accessId = Obj.accessId;
this.secretId = Obj.secretId;
this.secretKey = Obj.secretKey;
//返回 Sign对象,可以使用sign.get来获取sign。
this.signObj = new Sign(this.secretKey);
//开启调试模式
this._debugger = false;
//cos api 地址
//this._host = cos_host;
return true;
};
//COS对象的prototype设置
COS.prototype = {
upload: upload.upload,
convert: upload.convert,
mkBucket: bucket.create,
rmBucket: bucket.delete,
lsBucket: bucket.list,
getBucketMeta: bucket.getInfo,
setBucketMeta: bucket.setInfo,
rename: file.rename,
ls: file.list,
del: file.del,
//zip: file.compress,
mkdir: file.mkdir,
rmdir: file.rmdir,
setDirMeta: file.setDirMeta,
stat: file.getMeta,
url: file.getDownloadUrl,
/**
* 启用调试模式
*/
debugger: function () {
this._debugger = true;
},
// /**
// * 设置cos api的接口url,这个接口仅提供给腾讯程序猿内网调试用
// *
// * @param {string} url cos api的url地址,默认是 cosapi.myqcloud.com
// * @private
// */
// setHost: function (url) {
// this._host = url;
// },
/**
* 对 api 地址进行签名认证
*
* @param {string} apiUrl 需要签名的url地址
* @param {object} queryString url地址的参数
* @returns {string} 签名后的url地址
*/
signUrl: function (apiUrl, queryString) {
var uri = url.parse(apiUrl, true),
queryString = queryString || {};
queryString.accessId = this.accessId;
this.secretId && (queryString.secretId = this.secretId);
//获得当前服务器时间
queryString.time = queryString.time || parseInt((new Date()).getTime() / 1000, 10);
uri.query = initOpt(queryString, uri.query);
uri.search = ""; //清空search,让uri对象根据query来计算结果
uri.query.sign = this.signObj.get(url.format(uri));
return url.format(uri);
},
/**
* 发起API的请求
*
* @param {string} method 默认是get
* @param {string} api 请求的API地址,相对路径
* @param {Object} queryString 请求的参数
* @param {[object]} opt 请求参数options
* @param {Function} callback 请求完成的回调函数
* @private
* @returns {WriteStream} 返回请求的数据WriteStream
*/
request: function (method, api, queryString, opt, callback) {
//支持opt和callback的多态, 如果opt的类型是函数,则当callback处理
callback = callback || ((typeof opt == "function") ? opt : null);
opt = ((typeof opt == "function") ? {} : opt) || {};
var method = method.toLowerCase() || "get",
opt = opt || {},
uri = this.signUrl(api, queryString),
requestUrl = ["http://", COS.getHost(), uri].join("");
//如果开启调试模式就显示出cos的请求url,方便定位问题
if (this._debugger) {
console.log(requestUrl);
}
//请求
return request[method](requestUrl, opt, function (error, response, body) {
if (!error) {
try {
var data = JSON.parse(body);
} catch (e) {
error = new Error("返回无效的json数据格式:" + body);
}
callback && callback(error, data);
} else {
callback(error);
}
});
}
};
/**
* 获取 cos 操作对象, 用于兼容老的调用方式
* @param {Object} Obj 包含accessId,secretId,secretKey的json数据
* @returns {COS} 返回cos对象
*/
COS.getContext = function (Obj) {
return new COS(Obj);
}
/**
* 设置cos api的接口url,这个接口仅提供给腾讯程序猿内网调试用
*
* @param {string} domain cos api的域名地址,默认是 cosapi.myqcloud.com
*/
COS.setHost = function(domain){
cos_host = domain;
}
/**
* 获取当前的cos api host域名
* @returns {string}
*/
COS.getHost = function(){
return cos_host;
}
//输出 cos 的接口
module.exports = COS;