forked from tencentyun/cos-nodejs-sdk-v5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.js
723 lines (675 loc) · 22.5 KB
/
demo.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
var fs = require('fs');
var path = require('path');
var COS = require('../index');
var util = require('./util');
var config = require('./config');
var cos = new COS({
// 必选参数
SecretId: config.SecretId,
SecretKey: config.SecretKey,
// 可选参数
FileParallelLimit: 3, // 控制文件上传并发数
ChunkParallelLimit: 8, // 控制单个文件下分片上传并发数,在同园区上传可以设置较大的并发数
ChunkSize: 1024 * 1024, // 控制分片大小,单位 B,在同园区上传可以设置较大的分片大小
});
var TaskId;
function getService() {
cos.getService(function (err, data) {
console.log(err || data);
});
}
function getAuth() {
var key = '1mb.zip';
var auth = cos.getAuth({
Method: 'get',
Key: key,
Expires: 60,
});
// 注意:这里的 Bucket 格式是 test-1250000000
console.log('http://' + config.Bucket + '.cos.' + config.Region + '.myqcloud.com' + '/' + encodeURIComponent(key) + '?sign=' + encodeURIComponent(auth));
}
function getV4Auth() {
console.log();
var key = '中文.txt';
var auth = cos.getV4Auth({
Bucket: config.Bucket,
Key: key,
Expires: 60,
});
// 注意:这里的 Bucket 格式是 test-1250000000
console.log('http://' + config.Bucket + '.cos.' + config.Region + '.myqcloud.com' + '/' + encodeURIComponent(key) + '?sign=' + encodeURIComponent(auth));
}
function getObjectUrl() {
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);
}
function putBucket() {
cos.putBucket({
Bucket: 'testnew-' + config.Bucket.substr(config.Bucket.lastIndexOf('-') + 1),
Region: 'ap-guangzhou'
}, function (err, data) {
console.log(err || data);
});
}
function getBucket() {
cos.getBucket({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region
}, function (err, data) {
console.log(err || data);
});
}
function headBucket() {
cos.headBucket({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region
}, function (err, data) {
console.log(err || data);
});
}
function putBucketAcl() {
cos.putBucketAcl({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
// 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"',
// GrantReadAcp: 'id="qcs::cam::uin/1001:uin/1001",id="qcs::cam::uin/1002:uin/1002"',
// GrantWriteAcp: 'id="qcs::cam::uin/1001:uin/1001",id="qcs::cam::uin/1002:uin/1002"',
// ACL: 'public-read-write',
// ACL: 'public-read',
ACL: 'private',
// AccessControlPolicy: {
// "Owner": { // AccessControlPolicy 里必须有 owner
// "ID": 'qcs::cam::uin/459000000:uin/459000000' // 459000000 是 Bucket 所属用户的 QQ 号
// },
// "Grants": [{
// "Grantee": {
// "ID": "qcs::cam::uin/1001:uin/1001", // 10002 是 QQ 号
// "DisplayName": "qcs::cam::uin/1001:uin/1001" // 10002 是 QQ 号
// },
// "Permission": "READ"
// }, {
// "Grantee": {
// "ID": "qcs::cam::uin/10002:uin/10002", // 10002 是 QQ 号
// },
// "Permission": "WRITE"
// }, {
// "Grantee": {
// "ID": "qcs::cam::uin/10002:uin/10002", // 10002 是 QQ 号
// },
// "Permission": "READ_ACP"
// }, {
// "Grantee": {
// "ID": "qcs::cam::uin/10002:uin/10002", // 10002 是 QQ 号
// },
// "Permission": "WRITE_ACP"
// }]
// }
}, function (err, data) {
console.log(err || data);
});
}
function getBucketAcl() {
cos.getBucketAcl({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region
}, function (err, data) {
console.log(err || data);
});
}
function putBucketCors() {
cos.putBucketCors({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
CORSConfiguration: {
"CORSRules": [{
"AllowedOrigin": ["*"],
"AllowedMethod": ["GET", "POST", "PUT", "DELETE", "HEAD"],
"AllowedHeader": ["*"],
"ExposeHeader": ["ETag", "x-cos-acl", "x-cos-version-id", "x-cos-delete-marker", "x-cos-server-side-encryption"],
"MaxAgeSeconds": "5"
}]
}
}, function (err, data) {
console.log(err || data);
});
}
function getBucketCors() {
cos.getBucketCors({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region
}, function (err, data) {
console.log(err || data);
});
}
function deleteBucketCors() {
cos.deleteBucketCors({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region
}, function (err, data) {
console.log(err || data);
});
}
function putBucketTagging() {
cos.putBucketTagging({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Tagging: {
"Tags": [
{"Key": "k1", "Value": "v1"},
{"Key": "k2", "Value": "v2"}
]
}
}, function (err, data) {
console.log(err || data);
});
}
function getBucketTagging() {
cos.getBucketTagging({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region
}, function (err, data) {
console.log(err || data);
});
}
function deleteBucketTagging() {
cos.deleteBucketTagging({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region
}, function (err, data) {
console.log(err || data);
});
}
function putBucketPolicy() {
var AppId = config.Bucket.substr(config.Bucket.lastIndexOf('-') + 1);
cos.putBucketPolicy({
Policy: {
"version": "2.0",
"principal": {"qcs": ["qcs::cam::uin/10001:uin/10001"]}, // 这里的 10001 是 QQ 号
"statement": [
{
"effect": "allow",
"action": [
// 这里可以从临时密钥的权限上控制前端允许的操作
// 'name/cos:*', // 这样写可以包含下面所有权限
// // 列出所有允许的操作
// // ACL 读写
// 'name/cos:GetBucketACL',
// 'name/cos:PutBucketACL',
// 'name/cos:GetObjectACL',
// 'name/cos:PutObjectACL',
// // 简单 Bucket 操作
// 'name/cos:PutBucket',
// 'name/cos:HeadBucket',
// 'name/cos:GetBucket',
// 'name/cos:DeleteBucket',
// 'name/cos:GetBucketLocation',
// // Versioning
// 'name/cos:PutBucketVersioning',
// 'name/cos:GetBucketVersioning',
// // CORS
// 'name/cos:PutBucketCORS',
// 'name/cos:GetBucketCORS',
// 'name/cos:DeleteBucketCORS',
// // Lifecycle
// 'name/cos:PutBucketLifecycle',
// 'name/cos:GetBucketLifecycle',
// 'name/cos:DeleteBucketLifecycle',
// // Replication
// 'name/cos:PutBucketReplication',
// 'name/cos:GetBucketReplication',
// 'name/cos:DeleteBucketReplication',
// // 删除文件
// 'name/cos:DeleteMultipleObject',
// 'name/cos:DeleteObject',
// 简单文件操作
'name/cos:PutObject',
'name/cos:AppendObject',
'name/cos:GetObject',
'name/cos:HeadObject',
'name/cos:OptionsObject',
'name/cos:PutObjectCopy',
'name/cos:PostObjectRestore',
// 分片上传操作
'name/cos:InitiateMultipartUpload',
'name/cos:ListMultipartUploads',
'name/cos:ListParts',
'name/cos:UploadPart',
'name/cos:CompleteMultipartUpload',
'name/cos:AbortMultipartUpload',
],
// "resource": ["qcs::cos:ap-guangzhou:uid/1250000000:test-1250000000/*"] // 1250000000 是 appid
"resource": ["qcs::cos:" + config.Region + ":uid/" + AppId + ":" + config.Bucket + "/*"] // 1250000000 是 appid
}
]
},
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region
}, function (err, data) {
console.log(err || data);
});
}
function getBucketPolicy() {
cos.getBucketPolicy({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region
}, function (err, data) {
console.log(err || data);
});
}
function getBucketLocation() {
cos.getBucketLocation({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region
}, function (err, data) {
console.log(err || data);
});
}
function putBucketLifecycle() {
cos.putBucketLifecycle({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
LifecycleConfiguration: {
"Rules": [{
'ID': 1,
'Filter': {
'Prefix': 'cas',
},
'Status': 'Enabled',
'Transition': {
'Days': 0,
'StorageClass': 'ARCHIVE'
}
}]
}
}, function (err, data) {
console.log(err || data);
});
}
function getBucketLifecycle() {
cos.getBucketLifecycle({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region
}, function (err, data) {
console.log(err || data);
});
}
function deleteBucketLifecycle() {
cos.deleteBucketLifecycle({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region
}, function (err, data) {
console.log(err || data);
});
}
function putBucketVersioning() {
cos.putBucketVersioning({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
VersioningConfiguration: {
MFADelete: "Enabled",
Status: "Enabled"
}
}, function (err, data) {
console.log(err || data);
});
}
function getBucketVersioning() {
cos.getBucketVersioning({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region
}, function (err, data) {
console.log(err || data);
});
}
function listObjectVersions() {
cos.listObjectVersions({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Prefix: "1mb.zip"
}, function (err, data) {
console.log(err || JSON.stringify(data, null, ' '));
});
}
function putBucketReplication() {
var AppId = config.Bucket.substr(config.Bucket.lastIndexOf('-') + 1);
cos.putBucketReplication({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
ReplicationConfiguration: {
Role: "qcs::cam::uin/459000000:uin/459000000",
Rules: [{
ID: "1",
Status: "Enabled",
Prefix: "img/",
Destination: {
Bucket: "qcs::cos:ap-guangzhou::test-" + AppId
},
}]
}
}, function (err, data) {
console.log(err || data);
});
}
function getBucketReplication() {
cos.getBucketReplication({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region
}, function (err, data) {
console.log(err || data);
});
}
function deleteBucketReplication() {
cos.deleteBucketReplication({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region
}, function (err, data) {
console.log(err || data);
});
}
function deleteBucket() {
cos.deleteBucket({
Bucket: 'testnew-' + config.Bucket.substr(config.Bucket.lastIndexOf('-') + 1),
Region: 'ap-guangzhou'
}, function (err, data) {
console.log(err || data);
});
}
function putObject() {
// 创建测试文件
var filename = '1mb.zip';
var filepath = path.resolve(__dirname, filename);
util.createFile(filepath, 1024 * 1024, function (err) {
// 调用方法
cos.putObject({
Bucket: config.Bucket, /* 必须 */ // Bucket 格式:test-1250000000
Region: config.Region,
Key: filename, /* 必须 */
TaskReady: function (tid) {
TaskId = tid;
},
onProgress: function (progressData) {
console.log(JSON.stringify(progressData));
},
// 格式1. 传入文件内容
// Body: fs.readFileSync(filepath),
// 格式2. 传入文件流,必须需要传文件大小
Body: fs.createReadStream(filepath),
ContentLength: fs.statSync(filepath).size
}, function (err, data) {
console.log(err || data);
fs.unlinkSync(filepath);
});
});
}
function putObjectCopy() {
cos.putObjectCopy({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Key: '1mb.copy.zip',
CopySource: config.Bucket + '.cos.' + config.Region + '.myqcloud.com/1mb.zip', // Bucket 格式:test-1250000000
}, function (err, data) {
console.log(err || data);
});
}
function getObject() {
var filepath1 = path.resolve(__dirname, '1mb.out1.zip');
var filepath2 = path.resolve(__dirname, '1mb.out2.zip');
var filepath3 = path.resolve(__dirname, '1mb.out3.zip');
// cos.getObject({
// Bucket: config.Bucket, // Bucket 格式:test-1250000000
// Region: config.Region,
// Key: '1mb.zip',
// onProgress: function (progressData) {
// console.log(JSON.stringify(progressData));
// }
// }, function (err, data) {
// fs.writeFileSync(filepath1, data.Body);
// });
// cos.getObject({
// Bucket: config.Bucket, // Bucket 格式:test-1250000000
// Region: config.Region,
// Key: '1mb.zip',
// Output: fs.createWriteStream(filepath2),
// onProgress: function (progressData) {
// console.log(JSON.stringify(progressData));
// }
// }, function (err, data) {
// console.log(err || data);
// });
cos.getObject({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Key: '1mb.zip',
Output: filepath3,
onProgress: function (progressData) {
console.log(JSON.stringify(progressData));
}
}, function (err, data) {
console.log(err || data);
});
}
function headObject() {
cos.headObject({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Key: '1mb.zip'
}, function (err, data) {
console.log(err || data);
});
}
function putObjectAcl() {
cos.putObjectAcl({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Key: '1mb.zip',
// 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"
// }]
// }
}, function (err, data) {
console.log(err || data);
});
}
function getObjectAcl() {
cos.getObjectAcl({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Key: '1mb.zip'
}, function (err, data) {
console.log(err || data);
});
}
function deleteObject() {
cos.deleteObject({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Key: '1mb.zip'
}, function (err, data) {
console.log(err || data);
});
}
function deleteMultipleObject() {
cos.deleteMultipleObject({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Objects: [
{Key: '1mb.zip'},
{Key: '3mb.zip'},
]
}, function (err, data) {
console.log(err || data);
});
}
function restoreObject() {
cos.restoreObject({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Key: '1.txt',
RestoreRequest: {
Days: 1,
CASJobParameters: {
Tier: 'Expedited'
}
}
}, function (err, data) {
console.log(err || data);
});
}
function abortUploadTask() {
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',
}, function (err, data) {
console.log(err || data);
});
}
function sliceUploadFile() {
// 创建测试文件
var filename = '10mb.zip';
var filepath = path.resolve(__dirname, filename);
util.createFile(filepath, 1024 * 1024 * 10, function (err) {
// 调用方法
cos.sliceUploadFile({
Bucket: config.Bucket, /* 必须 */ // Bucket 格式:test-1250000000
Region: config.Region,
Key: filename, /* 必须 */
FilePath: filepath, /* 必须 */
TaskReady: function (tid) {
TaskId = tid;
},
onHashProgress: function (progressData) {
console.log(JSON.stringify(progressData));
},
onProgress: function (progressData) {
console.log(JSON.stringify(progressData));
},
}, function (err, data) {
console.log(err || data);
fs.unlinkSync(filepath);
});
});
}
function cancelTask() {
cos.cancelTask(TaskId);
console.log('canceled');
}
function pauseTask() {
cos.pauseTask(TaskId);
console.log('paused');
}
function restartTask() {
cos.restartTask(TaskId);
console.log('restart');
}
function uploadFiles() {
var filepath = path.resolve(__dirname, '1mb.zip');
util.createFile(filepath, 1024 * 1024 * 10, function (err) {
var filename = 'mb.zip';
cos.uploadFiles({
files: [{
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Key: '1' + filename,
FilePath: filepath,
}, {
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Key: '2' + filename,
FilePath: filepath,
// }, {
// Bucket: config.Bucket, // Bucket 格式:test-1250000000
// Region: config.Region,
// Key: '3' + filename,
// FilePath: filepath,
}],
SliceSize: 1024 * 1024,
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;');
},
onFileFinish: function (err, data, options) {
console.log(options.Key + ' 上传' + (err ? '失败' : '完成'));
},
}, function (err, data) {
console.log(err || data);
fs.unlinkSync(filepath);
});
});
}
getService();
// getAuth();
// getV4Auth();
// getObjectUrl();
// putBucket();
// getBucket();
// headBucket();
// putBucketAcl();
// getBucketAcl();
// putBucketCors();
// getBucketCors();
// deleteBucketCors();
// putBucketTagging();
// getBucketTagging();
// deleteBucketTagging();
// putBucketPolicy();
// getBucketPolicy();
// getBucketLocation();
// getBucketLifecycle();
// putBucketLifecycle();
// deleteBucketLifecycle();
// getBucketVersioning();
// listObjectVersions();
// putBucketVersioning();
// getBucketReplication();
// putBucketReplication();
// deleteBucketReplication();
// deleteBucket();
// putObject();
// putObjectCopy();
// getObject();
// headObject();
// putObjectAcl();
// getObjectAcl();
// deleteObject();
// deleteMultipleObject();
// restoreObject();
// abortUploadTask();
// sliceUploadFile();
// cancelTask();
// pauseTask();
// restartTask();
// uploadFiles();