forked from tencentyun/cos-wx-sdk-v5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo-sdk.js
440 lines (424 loc) · 15 KB
/
demo-sdk.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
var COS = require('./lib/cos-wx-sdk-v5');
var config = require('./config');
var TaskId;
// 签名回调
var getAuthorization = function(options, callback) {
// 方法一、后端通过获取临时密钥给到前端,前端计算签名
wx.request({
method: 'GET',
url: config.serverPrefix + 'sts.php', // 服务端签名,参考 server 目录下的两个签名例子
dataType: 'json',
success: function(result) {
var data = result.data;
callback({
TmpSecretId: data.credentials && data.credentials.tmpSecretId,
TmpSecretKey: data.credentials && data.credentials.tmpSecretKey,
XCosSecurityToken: data.credentials && data.credentials.sessionToken,
ExpiredTime: data.expiredTime,
});
}
});
// // 方法二、【细粒度控制权限】后端通过获取临时密钥给到前端,前端只有相同请求才重用临时密钥,后端可以通过 Scope 细粒度控制权限
// wx.request({
// method: 'POST',
// url: 'http://127.0.0.1:3000/sts-scope', // 服务端签名,参考 server 目录下的两个签名例子
// data: options.Scope,
// dataType: 'json',
// success: function(result) {
// var data = result.data;
// var credentials = data.credentials;
// callback({
// TmpSecretId: credentials.tmpSecretId,
// TmpSecretKey: credentials.tmpSecretKey,
// XCosSecurityToken: credentials.sessionToken,
// ExpiredTime: data.expiredTime,
// ScopeLimit: true, // 设为 true 可限制密钥只在相同请求可重用,默认不限制一直可重用,细粒度控制权限需要设为 true
// });
// }
// });
// // 方法三、后端使用固定密钥计算签名,返回给前端,auth.php,注意:这种有安全风险,后端需要通过 method、pathname 控制好权限,比如不允许 put / 等,这里暂不提供
// wx.request({
// method: 'POST',
// url: 'https://example.com/sts-auth.php, // 服务端签名,参考 server 目录下的两个签名例子
// data: {
// method: options.Method,
// pathname: options.Pathname,
// query: options.Query,
// headers: options.Headers,
// },
// dataType: 'json',
// success: function(result) {
// callback({
// Authorization: result.data,
// // XCosSecurityToken: sessionToken, // 如果使用临时密钥,需要传 sessionToken
// });
// }
// });
// // 方法四、前端使用固定密钥计算签名(仅适用于前端调试)
// var authorization = COS.getAuthorization({
// SecretId: 'AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
// SecretKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
// Method: options.Method,
// Pathname: options.Pathname,
// Query: options.Query,
// Headers: options.Headers,
// Expires: 60,
// });
// callback(authorization);
};
var cos = new COS({
// path style 指正式请求时,Bucket 是在 path 里,这样用途相同园区多个 bucket 只需要配置一个园区域名
// ForcePathStyle: true,
getAuthorization: getAuthorization,
});
// 回调统一处理函数
var requestCallback = function(err, data) {
console.log(err || data);
if (err && err.error) {
wx.showModal({
title: '返回错误',
content: '请求失败:' + (err.error.Message || err.error) + ';状态码:' + err.statusCode,
showCancel: false
});
} else if (err) {
wx.showModal({
title: '请求出错',
content: '请求出错:' + err + ';状态码:' + err.statusCode,
showCancel: false
});
} else {
wx.showToast({
title: '请求成功',
icon: 'success',
duration: 3000
});
}
};
// 展示的所有接口
var dao = {
getObjectUrl: function() {
var url = cos.getObjectUrl({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Key: '1mb.zip',
Expires: 60,
Sign: true,
}, function(err, data) {
console.log(err || data);
});
console.log(url);
},
// Service
getService: function() {
cos.getService(requestCallback);
},
// 简单 Bucket 操作
putBucket: function() {
cos.putBucket({
Bucket: config.Bucket,
Region: config.Region,
}, requestCallback);
},
getBucket: function() {
cos.getBucket({
Bucket: config.Bucket,
Region: config.Region
}, requestCallback);
},
headBucket: function() {
cos.headBucket({
Bucket: config.Bucket,
Region: config.Region
}, requestCallback);
},
deleteBucket: function() {
cos.deleteBucket({
Bucket: config.Bucket,
Region: config.Region
}, requestCallback);
},
getBucketACL: function() {
cos.getBucketAcl({
Bucket: config.Bucket,
Region: config.Region
}, requestCallback);
},
putBucketACL: function() {
cos.putBucketAcl({
Bucket: config.Bucket,
Region: config.Region,
ACL: 'public-read'
}, requestCallback);
},
getBucketCors: function() {
cos.getBucketCors({
Bucket: config.Bucket,
Region: config.Region
}, requestCallback);
},
putBucketCors: function() {
cos.putBucketCors({
Bucket: config.Bucket,
Region: config.Region,
CORSRules: [{
"AllowedOrigin": ["*"],
"AllowedMethod": ["GET", "POST", "PUT", "DELETE", "HEAD"],
"AllowedHeader": ["*"],
"ExposeHeader": ["ETag", "Content-Length"],
"MaxAgeSeconds": "5"
}]
}, requestCallback);
},
deleteBucketCors: function() {
cos.deleteBucketCors({
Bucket: config.Bucket,
Region: config.Region
}, requestCallback);
},
putBucketPolicy: function() {
var AppId = config.Bucket.substr(config.Bucket.lastIndexOf('-') + 1);
cos.putBucketPolicy({
Bucket: config.Bucket,
Region: config.Region,
Policy: {
"version": "2.0",
"principal": {
"qcs": ["qcs::cam::uin/10001:uin/10001"]
}, // 这里的 10001 是 QQ 号
"statement": [{
"effect": "allow",
"action": [
"name/cos:GetBucket",
"name/cos:PutObject",
"name/cos:PostObject",
"name/cos:PutObjectCopy",
"name/cos:InitiateMultipartUpload",
"name/cos:UploadPart",
"name/cos:UploadPartCopy",
"name/cos:CompleteMultipartUpload",
"name/cos:AbortMultipartUpload",
"name/cos:AppendObject"
],
// "resource": ["qcs::cos:ap-guangzhou:uid/1250000000:test-1250000000/*"] // 1250000000 是 appid
"resource": ["qcs::cos:" + config.Region + ":uid/" + AppId + ":" + config.Bucket + "/*"] // 1250000000 是 appid
}]
},
}, requestCallback);
},
getBucketPolicy: function() {
cos.getBucketPolicy({
Bucket: config.Bucket,
Region: config.Region
}, requestCallback);
},
deleteBucketPolicy: function() {
cos.deleteBucketPolicy({
Bucket: config.Bucket,
Region: config.Region
}, requestCallback);
},
getBucketLocation: function() {
cos.getBucketLocation({
Bucket: config.Bucket,
Region: config.Region
}, requestCallback);
},
getBucketTagging: function() {
cos.getBucketTagging({
Bucket: config.Bucket,
Region: config.Region
}, requestCallback);
},
putBucketTagging: function() {
cos.putBucketTagging({
Bucket: config.Bucket,
Region: config.Region,
Tagging: {
"Tags": [
{
"Key": "k1",
"Value": "v1"
},
{
"Key": "k2",
"Value": "v2"
}
]
}
}, requestCallback);
},
deleteBucketTagging: function() {
cos.deleteBucketTagging({
Bucket: config.Bucket,
Region: config.Region
}, requestCallback);
},
// Object
putObject: function() {
cos.putObject({
Bucket: config.Bucket,
Region: config.Region,
Key: '1.txt',
Body: 'hello world' // 在小程序里,putObject 接口只允许传字符串的内容,不支持 TaskReady 和 onProgress,上传请使用 cos.postObject 接口
}, requestCallback);
},
getObject: function() {
cos.getObject({
Bucket: config.Bucket,
Region: config.Region,
Key: '1.txt'
}, requestCallback);
},
headObject: function() {
cos.headObject({
Bucket: config.Bucket,
Region: config.Region,
Key: '1.txt'
}, requestCallback);
},
deleteObject: function() {
cos.deleteObject({
Bucket: config.Bucket,
Region: config.Region,
Key: '1.txt'
}, requestCallback);
},
getObjectACL: function() {
cos.getObjectAcl({
Bucket: config.Bucket,
Region: config.Region,
Key: '1.txt'
}, requestCallback);
},
putObjectACL: function() {
cos.putObjectAcl({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Key: '1.txt',
// GrantFullControl: 'id="qcs::cam::uin/1001:uin/1001",id="qcs::cam::uin/1002:uin/1002"',
// GrantWrite: 'id="qcs::cam::uin/1001:uin/1001",id="qcs::cam::uin/1002:uin/1002"',
// GrantRead: 'id="qcs::cam::uin/1001:uin/1001",id="qcs::cam::uin/1002:uin/1002"',
// ACL: 'public-read-write',
// ACL: 'public-read',
// ACL: 'private',
ACL: 'default', // 继承上一级目录权限
// AccessControlPolicy: {
// "Owner": { // AccessControlPolicy 里必须有 owner
// "ID": 'qcs::cam::uin/459000000:uin/459000000' // 459000000 是 Bucket 所属用户的 QQ 号
// },
// "Grants": [{
// "Grantee": {
// "ID": "qcs::cam::uin/10002:uin/10002", // 10002 是 QQ 号
// },
// "Permission": "READ"
// }]
// }
}, requestCallback);
},
deleteMultipleObject: function() {
cos.deleteMultipleObject({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Objects: [{
Key: '1.txt'
},
{
Key: '1.copy.txt'
},
]
}, requestCallback);
},
putObjectCopy: function() {
cos.putObjectCopy({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Key: '1.copy.txt',
CopySource: config.Bucket + '.cos.' + config.Region + '.myqcloud.com/1.txt',
}, requestCallback);
},
// 上传文件
postObject: function() {
wx.chooseImage({
count: 1, // 默认9
sizeType: ['original'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success: function(res) {
var file = res.tempFiles[0];
cos.postObject({
Bucket: config.Bucket,
Region: config.Region,
Key: '1.png',
FilePath: file.path,
FileSize: file.size,
TaskReady: function(taskId) {
TaskId = taskId
},
onProgress: function(info) {
console.log(JSON.stringify(info));
}
}, requestCallback);
}
})
},
restoreObject: function() {
cos.restoreObject({
Bucket: config.Bucket,
Region: config.Region,
Key: '1.txt',
RestoreRequest: {
Days: 1,
CASJobParameters: {
Tier: 'Expedited'
}
}
}, requestCallback);
},
abortUploadTask: function() {
cos.abortUploadTask({
Bucket: config.Bucket,
/* 必须 */ // Bucket 格式:test-1250000000
Region: config.Region,
/* 必须 */
// 格式1,删除单个上传任务
// Level: 'task',
// Key: '10mb.zip',
// UploadId: '14985543913e4e2642e31db217b9a1a3d9b3cd6cf62abfda23372c8d36ffa38585492681e3',
// 格式2,删除单个文件所有未完成上传任务
Level: 'file',
Key: '10mb.zip',
// 格式3,删除 Bucket 下所有未完成上传任务
// Level: 'bucket',
}, requestCallback);
},
sliceCopyFile: function() {
// 创建测试文件
var sourceName = '1.txt';
var Key = '1.slicecopy.exe';
var sourcePath = config.Bucket + '.cos.' + config.Region + '.myqcloud.com/' + sourceName;
cos.sliceCopyFile({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Key: Key,
CopySource: sourcePath,
SliceSize: 20 * 1024 * 1024, // 大于20M的文件用分片复制,小于则用单片复制
onProgress: function(info) {
var percent = parseInt(info.percent * 10000) / 100;
var speed = parseInt(info.speed / 1024 / 1024 * 100) / 100;
console.log('进度:' + percent + '%; 速度:' + speed + 'Mb/s;');
}
}, requestCallback);
},
cancelTask: function() {
cos.cancelTask(TaskId);
console.log('canceled');
},
pauseTask: function() {
cos.pauseTask(TaskId);
console.log('paused');
},
restartTask: function() {
cos.restartTask(TaskId);
console.log('restart');
},
};
module.exports = dao;