test.html 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport"
  6. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <title>cos-js-sdk-v5-demo</title>
  9. <link rel="stylesheet" href="../test/qunit-2.4.0.css">
  10. </head>
  11. <body>
  12. <h1 id="qunit-header">cos-js-sdk-v5</h1>
  13. <h2 id="qunit-banner"></h2>
  14. <ol id="qunit-tests"></ol>
  15. <script src="../test/qunit-2.4.0.js"></script>
  16. <script src="../dist/cos-js-sdk-v5.js"></script>
  17. <script>
  18. var config = {
  19. SecretId: 'xxx',
  20. SecretKey: 'xxx',
  21. Bucket: 'test-1250000000',
  22. Region: 'default'
  23. };
  24. var util = {
  25. createFile: function (options) {
  26. var buffer = new ArrayBuffer(options.size || 0);
  27. var arr = new Uint8Array(buffer);
  28. arr.forEach(function (char, i) {
  29. arr[i] = 0;
  30. });
  31. var opt = {};
  32. options.type && (opt.type = options.type);
  33. var blob = new Blob([buffer], options);
  34. return blob;
  35. },
  36. str2blob: function (str) {
  37. var size = str.length;
  38. var buffer = new ArrayBuffer(size || 0);
  39. var arr = new Uint8Array(buffer);
  40. arr.forEach(function (char, i) {
  41. arr[i] = str[i];
  42. });
  43. var blob = new Blob([buffer]);
  44. return blob;
  45. }
  46. };
  47. var cos = new COS({
  48. // 必选参数
  49. SecretId: config.SecretId,
  50. SecretKey: config.SecretKey,
  51. CompatibilityMode: true,
  52. ServiceDomain: 'http://cos.default.yun.ccb.com',
  53. Domain: 'http://{Bucket}.cos.{Region}.yun.ccb.com',
  54. // 可选参数
  55. FileParallelLimit: 3, // 控制文件上传并发数
  56. ChunkParallelLimit: 3, // 控制单个文件下分片上传并发数
  57. ChunkSize: 1024 * 1024, // 控制分片大小,单位 B
  58. ProgressInterval: 1, // 控制 onProgress 回调的间隔
  59. ChunkRetryTimes: 3, // 控制文件切片后单片上传失败后重试次数
  60. });
  61. var AppId = config.AppId;
  62. var Bucket = config.Bucket;
  63. var BucketShortName = Bucket;
  64. var BucketLongName = Bucket + '-' + AppId;
  65. var TaskId;
  66. var match = config.Bucket.match(/^(.+)-(\d+)$/);
  67. if (match) {
  68. BucketLongName = config.Bucket; // Bucket 格式:test-1250000000
  69. BucketShortName = match[1];
  70. AppId = match[2];
  71. }
  72. var it = QUnit.test;
  73. function comparePlainObject(a, b) {
  74. a instanceof Array && a.sort();
  75. b instanceof Array && b.sort();
  76. if (Object.keys(a).length !== Object.keys(b).length) {
  77. return false;
  78. }
  79. for (var key in a) {
  80. if (typeof a[key] === 'object' && typeof b[key] === 'object') {
  81. if (!comparePlainObject(a[key], b[key])) {
  82. return false;
  83. }
  84. } else if (a[key] != b[key]) {
  85. return false;
  86. }
  87. }
  88. return true;
  89. }
  90. it('getAuth()', function (assert) {
  91. return new Promise(function (done) {
  92. var content = Date.now().toString();
  93. var key = '1.txt';
  94. cos.putObject({
  95. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  96. Region: config.Region,
  97. Key: key,
  98. Body: content
  99. }, function (err, data) {
  100. cos.getObjectUrl({
  101. Bucket: config.Bucket,
  102. Region: config.Region,
  103. Key: key,
  104. }, function (err, data) {
  105. var link = data.Url;
  106. var xhr = new XMLHttpRequest();
  107. xhr.open('GET', link, true);
  108. data.XCosSecurityToken && xhr.setRequestHeader('x-cos-security-token', data.XCosSecurityToken);
  109. xhr.onload = function (e) {
  110. assert.ok(xhr.status === 200, '获取文件 200');
  111. assert.ok(xhr.responseText === content, '通过获取签名能正常获取文件');
  112. done();
  113. };
  114. xhr.onerror = function (e) {
  115. assert.ok(false, '文件获取出错');
  116. done();
  117. };
  118. xhr.send();
  119. });
  120. });
  121. });
  122. });
  123. it('auth check', function (assert) {
  124. return new Promise(function (done) {
  125. cos.getBucket({
  126. Bucket: config.Bucket,
  127. Region: config.Region,
  128. Prefix: 'aksjhdlash sajlhj!@#$%^&*()_+=-[]{}\';:"/.<>?.,??sadasd#/.,/~`',
  129. Headers: {
  130. 'x-cos-test': 'aksjhdlash sajlhj!@#$%^&*()_+=-[]{}\';:\"/.<>?.,??sadasd#/.,/~`',
  131. },
  132. }, function (err, data) {
  133. assert.ok(!err);
  134. done();
  135. });
  136. });
  137. });
  138. it('cancelTask()', function (assert) {
  139. return new Promise(function (done) {
  140. var filename = '10mb.zip';
  141. var blob = util.createFile({size: 1024 * 1024 * 10});
  142. var alive = false;
  143. var canceled = false;
  144. cos.putObject({
  145. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  146. Region: config.Region,
  147. Key: filename,
  148. Body: blob,
  149. TaskReady: function (taskId) {
  150. TaskId = taskId;
  151. },
  152. onProgress: function (info) {
  153. alive = true;
  154. if (!canceled) {
  155. cos.cancelTask(TaskId);
  156. alive = false;
  157. canceled = true;
  158. setTimeout(function () {
  159. assert.ok(!alive, '取消上传已经生效');
  160. done();
  161. }, 1200);
  162. }
  163. }
  164. }, function (err, data) {
  165. alive = true;
  166. });
  167. });
  168. });
  169. it('pauseTask(),restartTask()', function (assert) {
  170. return new Promise(function (done) {
  171. var filename = '10mb.zip';
  172. var blob = util.createFile({size: 1024 * 1024 * 10});
  173. var paused = false;
  174. var restarted = false;
  175. cos.abortUploadTask({
  176. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  177. Region: config.Region,
  178. Key: filename,
  179. Level: 'file',
  180. }, function (err, data) {
  181. cos.sliceUploadFile({
  182. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  183. Region: config.Region,
  184. Key: filename,
  185. Body: blob,
  186. TaskReady: function (taskId) {
  187. TaskId = taskId;
  188. },
  189. onProgress: function (info) {
  190. if (!paused && info.percent > 0.6) {
  191. cos.pauseTask(TaskId);
  192. paused = true;
  193. setTimeout(function () {
  194. cos.restartTask(TaskId);
  195. restarted = true;
  196. }, 1000);
  197. }
  198. if (restarted) {
  199. assert.ok(info.percent > 0.3, '暂停和重试成功');
  200. done();
  201. }
  202. }
  203. }, function (err, data) {
  204. });
  205. });
  206. });
  207. });
  208. it('分片上传', function (assert) {
  209. return new Promise(function (done) {
  210. var filename = '10mb.zip';
  211. var blob = util.createFile({size: 1024 * 1024 * 10});
  212. var paused = false;
  213. cos.abortUploadTask({
  214. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  215. Region: config.Region,
  216. Key: filename,
  217. Level: 'file',
  218. }, function (err, data) {
  219. cos.sliceUploadFile({
  220. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  221. Region: config.Region,
  222. Key: filename,
  223. Body: blob,
  224. TaskReady: function (taskId) {
  225. TaskId = taskId;
  226. },
  227. onProgress: function (info) {
  228. if (!paused && info.percent >= 0.6) {
  229. paused = true;
  230. cos.cancelTask(TaskId);
  231. cos.sliceUploadFile({
  232. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  233. Region: config.Region,
  234. Key: filename,
  235. Body: blob,
  236. TaskReady: function (taskId) {
  237. TaskId = taskId;
  238. },
  239. onProgress: function (info) {
  240. assert.ok(info.percent > 0.3, '分片续传成功');
  241. cos.cancelTask(TaskId);
  242. done();
  243. }
  244. });
  245. }
  246. }
  247. });
  248. });
  249. });
  250. });
  251. it('mock readAsBinaryString', function (assert) {
  252. return new Promise(function (done) {
  253. FileReader.prototype._readAsBinaryString = FileReader.prototype.readAsBinaryString;
  254. FileReader.prototype.readAsBinaryString = false;
  255. var filename = '10mb.zip';
  256. var blob = util.createFile({size: 1024 * 1024 * 10});
  257. var paused = false;
  258. cos.sliceUploadFile({
  259. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  260. Region: config.Region,
  261. Key: filename,
  262. Body: blob,
  263. TaskReady: function (taskId) {
  264. TaskId = taskId;
  265. },
  266. onProgress: function (info) {
  267. if (!paused && info.percent > 0.6) {
  268. cos.cancelTask(TaskId);
  269. cos.sliceUploadFile({
  270. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  271. Region: config.Region,
  272. Key: filename,
  273. Body: blob,
  274. TaskReady: function (taskId) {
  275. TaskId = taskId;
  276. },
  277. onProgress: function (info) {
  278. assert.ok(info.percent > 0.3, '分片续传成功');
  279. cos.cancelTask(TaskId);
  280. FileReader.prototype.readAsBinaryString = FileReader.prototype._readAsBinaryString;
  281. delete FileReader.prototype._readAsBinaryString;
  282. done();
  283. }
  284. });
  285. }
  286. }
  287. });
  288. });
  289. });
  290. it('abortUploadTask(),Level=task', function (assert) {
  291. return new Promise(function (done) {
  292. var filename = '10mb.zip';
  293. cos.multipartInit({
  294. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  295. Region: config.Region,
  296. Key: filename,
  297. }, function (err, data) {
  298. cos.abortUploadTask({
  299. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  300. Region: config.Region,
  301. Key: filename,
  302. Level: 'task',
  303. UploadId: data.UploadId,
  304. }, function (err, data) {
  305. var nameExist = false;
  306. data.successList.forEach(function (item) {
  307. if (filename === item.Key) {
  308. nameExist = true;
  309. }
  310. });
  311. assert.ok(data.successList.length >= 1, '成功取消单个分片任务');
  312. assert.ok(nameExist, '成功取消单个分片任务');
  313. done();
  314. });
  315. });
  316. });
  317. });
  318. it('abortUploadTask(),Level=file', function (assert) {
  319. return new Promise(function (done) {
  320. var filename = '10mb.zip';
  321. var blob = util.createFile({size: 1024 * 1024 * 10});
  322. cos.sliceUploadFile({
  323. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  324. Region: config.Region,
  325. Key: filename,
  326. Body: blob,
  327. TaskReady: function (taskId) {
  328. TaskId = taskId;
  329. },
  330. onProgress: function (info) {
  331. cos.cancelTask(TaskId);
  332. cos.abortUploadTask({
  333. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  334. Region: config.Region,
  335. Level: 'file',
  336. Key: filename,
  337. }, function (err, data) {
  338. assert.ok(data.successList.length >= 1, '成功舍弃单个文件下的所有分片任务');
  339. assert.ok(data.successList[0] && data.successList[0].Key === filename, '成功舍弃单个文件的所有分片任务');
  340. done();
  341. });
  342. }
  343. });
  344. });
  345. });
  346. it('abortUploadTask(),Level=bucket', function (assert) {
  347. return new Promise(function (done) {
  348. var filename = '10mb.zip';
  349. var blob = util.createFile({size: 1024 * 1024 * 10});
  350. cos.sliceUploadFile({
  351. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  352. Region: config.Region,
  353. Key: filename,
  354. Body: blob,
  355. TaskReady: function (taskId) {
  356. TaskId = taskId;
  357. },
  358. onProgress: function (info) {
  359. cos.cancelTask(TaskId);
  360. cos.abortUploadTask({
  361. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  362. Region: config.Region,
  363. Level: 'bucket',
  364. }, function (err, data) {
  365. var nameExist = false;
  366. data.successList.forEach(function (item) {
  367. if (filename === item.Key) {
  368. nameExist = true;
  369. }
  370. });
  371. assert.ok(data.successList.length >= 1, '成功舍弃Bucket下所有分片任务');
  372. assert.ok(nameExist, '成功舍弃Bucket下所有分片任务');
  373. done();
  374. });
  375. }
  376. });
  377. });
  378. });
  379. it('headBucket()', function (assert) {
  380. return new Promise(function (done) {
  381. cos.headBucket({
  382. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  383. Region: config.Region
  384. }, function (err, data) {
  385. assert.ok(data, '正常获取 head bucket');
  386. done();
  387. });
  388. });
  389. });
  390. it('headBucket() not exist', function (assert) {
  391. return new Promise(function (done) {
  392. cos.headBucket({
  393. Bucket: config.Bucket + Date.now().toString(36),
  394. Region: config.Region
  395. }, function (err, data) {
  396. assert.ok(err, 'bucket 不存在');
  397. done();
  398. });
  399. });
  400. });
  401. it('deleteBucket()', function (assert) {
  402. return new Promise(function (done) {
  403. cos.deleteBucket({
  404. Bucket: config.Bucket + Date.now().toString(36),
  405. Region: config.Region
  406. }, function (err, data) {
  407. assert.ok(err, '正常获取 head bucket');
  408. done();
  409. });
  410. });
  411. });
  412. it('getBucket()', function (assert) {
  413. return new Promise(function (done) {
  414. cos.getBucket({
  415. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  416. Region: config.Region
  417. }, function (err, data) {
  418. assert.equal(true, data.Name === BucketLongName, '能列出 bucket');
  419. assert.equal(data.Contents.constructor, Array, '正常获取 bucket 里的文件列表');
  420. done();
  421. });
  422. });
  423. });
  424. it('putObject()', function (assert) {
  425. var filename = '1.txt';
  426. var getObjectETag = function (callback) {
  427. setTimeout(function () {
  428. cos.headObject({
  429. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  430. Region: config.Region,
  431. Key: filename,
  432. }, function (err, data) {
  433. callback(data && data.headers && data.headers.etag);
  434. });
  435. }, 2000);
  436. };
  437. return new Promise(function (done) {
  438. var content = Date.now().toString();
  439. var lastPercent = 0;
  440. var blob = util.str2blob(content);
  441. cos.putObject({
  442. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  443. Region: config.Region,
  444. Key: filename,
  445. Body: blob,
  446. onProgress: function (processData) {
  447. lastPercent = processData.percent;
  448. },
  449. }, function (err, data) {
  450. if (err) throw err;
  451. assert.ok(data && data.ETag, 'putObject 有返回 ETag');
  452. getObjectETag(function (ETag) {
  453. assert.ok(data.ETag === ETag, 'Blob 创建 object');
  454. done();
  455. });
  456. });
  457. });
  458. });
  459. it('putObject(),string', function (assert) {
  460. var filename = '1.txt';
  461. return new Promise(function (done) {
  462. var content = '中文_' + Date.now().toString(36);
  463. var lastPercent = 0;
  464. cos.putObject({
  465. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  466. Region: config.Region,
  467. Key: filename,
  468. Body: content,
  469. onProgress: function (processData) {
  470. lastPercent = processData.percent;
  471. },
  472. }, function (err, data) {
  473. if (err) throw err;
  474. assert.ok(data && data.ETag, 'putObject 有返回 ETag');
  475. cos.getObject({
  476. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  477. Region: config.Region,
  478. Key: filename,
  479. }, function (err, data) {
  480. assert.ok(data && data.headers && data.ETag === data.ETag && data.Body === content, '上传和下载内容一致');
  481. done();
  482. });
  483. });
  484. });
  485. });
  486. it('putObject(),string empty', function (assert) {
  487. var filename = '1.txt';
  488. return new Promise(function (done) {
  489. var content = '';
  490. var lastPercent = 0;
  491. cos.putObject({
  492. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  493. Region: config.Region,
  494. Key: filename,
  495. Body: content,
  496. onProgress: function (processData) {
  497. lastPercent = processData.percent;
  498. },
  499. }, function (err, data) {
  500. if (err) throw err;
  501. assert.ok(data && data.ETag, 'putObject 有返回 ETag');
  502. cos.getObject({
  503. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  504. Region: config.Region,
  505. Key: filename,
  506. }, function (err, data) {
  507. assert.ok(data && data.headers && data.ETag === data.ETag && data.Body === content, '上传和下载内容一致');
  508. done();
  509. });
  510. });
  511. });
  512. });
  513. it('Key 特殊字符', function (assert) {
  514. return new Promise(function (done) {
  515. cos.putObject({
  516. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  517. Region: config.Region,
  518. Key: '(!\'*) "#$%&+,-./0123456789:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
  519. Body: Date.now().toString()
  520. }, function (err, data) {
  521. if (err) throw err;
  522. assert.ok(data, 'putObject 特殊字符的 Key 能通过');
  523. done();
  524. });
  525. });
  526. });
  527. it('getObject()', function (assert) {
  528. return new Promise(function (done) {
  529. var key = '1.txt';
  530. var content = Date.now().toString(36);
  531. cos.putObject({
  532. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  533. Region: config.Region,
  534. Key: key,
  535. Body: content
  536. }, function (err, data) {
  537. setTimeout(function () {
  538. cos.getObject({
  539. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  540. Region: config.Region,
  541. Key: key,
  542. }, function (err, data) {
  543. if (err) throw err;
  544. assert.ok(data.Body === content);
  545. done();
  546. });
  547. }, 2000);
  548. });
  549. });
  550. });
  551. it('putObjectCopy()', function (assert) {
  552. return new Promise(function (done) {
  553. var content = Date.now().toString(36);
  554. cos.putObject({
  555. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  556. Region: config.Region,
  557. Key: '1.txt',
  558. Body: content,
  559. }, function (err, data) {
  560. var ETag = data.ETag;
  561. cos.deleteObject({
  562. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  563. Region: config.Region,
  564. Key: '1.copy.txt',
  565. }, function (err, data) {
  566. cos.putObjectCopy({
  567. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  568. Region: config.Region,
  569. Key: '1.copy.txt',
  570. CopySource: BucketLongName + '.cos.' + config.Region + '.yun.ccb.com/1.txt',
  571. }, function (err, data) {
  572. cos.headObject({
  573. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  574. Region: config.Region,
  575. Key: '1.copy.txt',
  576. }, function (err, data) {
  577. assert.ok(data.headers.etag === ETag, '成功复制文件');
  578. done();
  579. });
  580. });
  581. });
  582. });
  583. });
  584. });
  585. it('deleteMultipleObject()', function (assert) {
  586. return new Promise(function (done) {
  587. var content = Date.now().toString(36);
  588. cos.putObject({
  589. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  590. Region: config.Region,
  591. Key: '1.txt',
  592. Body: content,
  593. }, function (err, data) {
  594. cos.putObject({
  595. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  596. Region: config.Region,
  597. Key: '2.txt',
  598. Body: content,
  599. }, function (err, data) {
  600. cos.deleteMultipleObject({
  601. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  602. Region: config.Region,
  603. Objects : [
  604. {Key: '1.txt'},
  605. {Key: '2.txt'}
  606. ],
  607. }, function (err, data) {
  608. assert.ok(data.Deleted.length === 2);
  609. cos.headObject({
  610. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  611. Region: config.Region,
  612. Key: '1.txt',
  613. }, function (err, data) {
  614. assert.ok(err.statusCode === 404, '1.txt 删除成功');
  615. cos.headObject({
  616. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  617. Region: config.Region,
  618. Key: '2.txt',
  619. }, function (err, data) {
  620. assert.ok(err.statusCode === 404, '2.txt 删除成功');
  621. done();
  622. });
  623. });
  624. });
  625. });
  626. });
  627. });
  628. });
  629. it('sliceUploadFile()', function (assert) {
  630. return new Promise(function (done) {
  631. var filename = '3mb.zip';
  632. var blob = util.createFile({size: 1024 * 1024 * 3});
  633. var lastPercent = 0;
  634. cos.sliceUploadFile({
  635. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  636. Region: config.Region,
  637. Key: filename,
  638. Body: blob,
  639. SliceSize: 1024 * 1024,
  640. AsyncLimit: 5,
  641. onHashProgress: function (progressData) {
  642. },
  643. onProgress: function (progressData) {
  644. lastPercent = progressData.percent;
  645. },
  646. }, function (err, data) {
  647. assert.ok(data && data.ETag && lastPercent === 1, '上传成功');
  648. done();
  649. });
  650. });
  651. });
  652. (function () {
  653. var AccessControlPolicy = {
  654. "Owner": {
  655. "ID": 'qcs::cam::uin/10001:uin/10001' // 10001 是 QQ 号
  656. },
  657. "Grants": [{
  658. "Grantee": {
  659. "ID": "qcs::cam::uin/10002:uin/10002", // 10002 是 QQ 号
  660. },
  661. "Permission": "READ"
  662. }]
  663. };
  664. var AccessControlPolicy2 = {
  665. "Owner": {
  666. "ID": 'qcs::cam::uin/10001:uin/10001' // 10001 是 QQ 号
  667. },
  668. "Grant": {
  669. "Grantee": {
  670. "ID": "qcs::cam::uin/10002:uin/10002", // 10002 是 QQ 号
  671. },
  672. "Permission": "READ"
  673. }
  674. };
  675. it('putBucketAcl() header ACL:private', function (assert) {
  676. return new Promise(function (done) {
  677. cos.putBucketAcl({
  678. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  679. Region: config.Region,
  680. ACL: 'private'
  681. }, function (err, data) {
  682. assert.ok(!err, 'putBucketAcl 成功');
  683. cos.getBucketAcl({
  684. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  685. Region: config.Region
  686. }, function (err, data) {
  687. AccessControlPolicy.Owner.ID = data.Owner.ID;
  688. AccessControlPolicy2.Owner.ID = data.Owner.ID;
  689. assert.ok(data.ACL === 'private' || data.ACL === 'default');
  690. done();
  691. });
  692. });
  693. });
  694. });
  695. it('putBucketAcl() header ACL:public-read', function (assert) {
  696. return new Promise(function (done) {
  697. cos.putBucketAcl({
  698. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  699. Region: config.Region,
  700. ACL: 'public-read',
  701. }, function (err, data) {
  702. assert.ok(!err, 'putBucketAcl 成功');
  703. cos.getBucketAcl({Bucket: config.Bucket, Region: config.Region}, function (err, data) { // Bucket 格式:test-1250000000
  704. assert.ok(data.ACL === 'public-read');
  705. done();
  706. });
  707. });
  708. });
  709. });
  710. it('putBucketAcl() header ACL:public-read-write', function (assert) {
  711. return new Promise(function (done) {
  712. cos.putBucketAcl({
  713. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  714. Region: config.Region,
  715. ACL: 'public-read-write',
  716. }, function (err, data) {
  717. assert.ok(!err, 'putBucketAcl 成功');
  718. cos.getBucketAcl({Bucket: config.Bucket, Region: config.Region}, function (err, data) { // Bucket 格式:test-1250000000
  719. assert.ok(data.ACL === 'public-read-write');
  720. done();
  721. });
  722. });
  723. });
  724. });
  725. it('putBucketAcl() header GrantRead:1001,1002"', function (assert) {
  726. return new Promise(function (done) {
  727. var GrantRead = 'id="qcs::cam::uin/1001:uin/1001", id="qcs::cam::uin/1002:uin/1002"';
  728. cos.putBucketAcl({
  729. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  730. Region: config.Region,
  731. GrantRead: GrantRead,
  732. }, function (err, data) {
  733. assert.ok(!err, 'putBucketAcl 成功');
  734. cos.getBucketAcl({Bucket: config.Bucket, Region: config.Region}, function (err, data) { // Bucket 格式:test-1250000000
  735. assert.ok(data.GrantRead = GrantRead);
  736. done();
  737. });
  738. });
  739. });
  740. });
  741. it('putBucketAcl() header GrantWrite:1001,1002', function (assert) {
  742. return new Promise(function (done) {
  743. var GrantWrite = 'id="qcs::cam::uin/1001:uin/1001", id="qcs::cam::uin/1002:uin/1002"';
  744. cos.putBucketAcl({
  745. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  746. Region: config.Region,
  747. GrantWrite: GrantWrite,
  748. }, function (err, data) {
  749. assert.ok(!err, 'putBucketAcl 成功');
  750. cos.getBucketAcl({Bucket: config.Bucket, Region: config.Region}, function (err, data) { // Bucket 格式:test-1250000000
  751. assert.ok(data.GrantWrite = GrantWrite);
  752. done();
  753. });
  754. });
  755. });
  756. });
  757. it('putBucketAcl() header GrantFullControl:1001,1002', function (assert) {
  758. return new Promise(function (done) {
  759. var GrantFullControl = 'id="qcs::cam::uin/1001:uin/1001", id="qcs::cam::uin/1002:uin/1002"';
  760. cos.putBucketAcl({
  761. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  762. Region: config.Region,
  763. GrantFullControl: GrantFullControl,
  764. }, function (err, data) {
  765. assert.ok(!err, 'putBucketAcl 成功');
  766. cos.getBucketAcl({Bucket: config.Bucket, Region: config.Region}, function (err, data) { // Bucket 格式:test-1250000000
  767. assert.ok(data.GrantFullControl = GrantFullControl);
  768. done();
  769. });
  770. });
  771. });
  772. });
  773. it('putBucketAcl() header ACL:public-read, GrantFullControl:1001,1002', function (assert) {
  774. return new Promise(function (done) {
  775. var GrantFullControl = 'id="qcs::cam::uin/1001:uin/1001", id="qcs::cam::uin/1002:uin/1002"';
  776. cos.putBucketAcl({
  777. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  778. Region: config.Region,
  779. GrantFullControl: GrantFullControl,
  780. ACL: 'public-read',
  781. }, function (err, data) {
  782. assert.ok(!err, 'putBucketAcl 成功');
  783. cos.getBucketAcl({Bucket: config.Bucket, Region: config.Region}, function (err, data) { // Bucket 格式:test-1250000000
  784. assert.ok(data.GrantFullControl = GrantFullControl);
  785. assert.ok(data.ACL === 'public-read');
  786. done();
  787. });
  788. });
  789. });
  790. });
  791. it('putBucketAcl() xml', function (assert) {
  792. return new Promise(function (done) {
  793. cos.putBucketAcl({
  794. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  795. Region: config.Region,
  796. AccessControlPolicy: AccessControlPolicy
  797. }, function (err, data) {
  798. assert.ok(!err, 'putBucketAcl 成功');
  799. cos.getBucketAcl({Bucket: config.Bucket, Region: config.Region}, function (err, data) { // Bucket 格式:test-1250000000
  800. assert.ok(data.Grants.length === 1);
  801. assert.ok(data.Grants[0] && data.Grants[0].Grantee.ID === 'qcs::cam::uin/10002:uin/10002', '设置 AccessControlPolicy ID 正确');
  802. assert.ok(data.Grants[0] && data.Grants[0].Permission === 'READ', '设置 AccessControlPolicy Permission 正确');
  803. done();
  804. });
  805. });
  806. });
  807. });
  808. it('putBucketAcl() xml2', function (assert) {
  809. return new Promise(function (done) {
  810. cos.putBucketAcl({
  811. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  812. Region: config.Region,
  813. AccessControlPolicy: AccessControlPolicy2,
  814. }, function (err, data) {
  815. assert.ok(!err, 'putBucketAcl 成功');
  816. cos.getBucketAcl({Bucket: config.Bucket, Region: config.Region}, function (err, data) { // Bucket 格式:test-1250000000
  817. assert.ok(data.Grants.length === 1);
  818. assert.ok(data.Grants[0] && data.Grants[0].Grantee.ID === 'qcs::cam::uin/10002:uin/10002');
  819. assert.ok(data.Grants[0] && data.Grants[0].Permission === 'READ');
  820. done();
  821. });
  822. });
  823. });
  824. });
  825. it('putBucketAcl() decodeAcl', function (assert) {
  826. return new Promise(function (done) {
  827. cos.getBucketAcl({
  828. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  829. Region: config.Region
  830. }, function (err, data) {
  831. cos.putBucketAcl({
  832. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  833. Region: config.Region,
  834. GrantFullControl: data.GrantFullControl,
  835. GrantWrite: data.GrantWrite,
  836. GrantRead: data.GrantRead,
  837. ACL: data.ACL,
  838. }, function (err, data) {
  839. assert.ok(data);
  840. done();
  841. });
  842. });
  843. });
  844. });
  845. })();
  846. (function () {
  847. var AccessControlPolicy = {
  848. "Owner": {
  849. "ID": 'qcs::cam::uin/10001:uin/10001' // 10001 是 QQ 号
  850. },
  851. "Grants": [{
  852. "Grantee": {
  853. "ID": "qcs::cam::uin/10002:uin/10002", // 10002 是 QQ 号
  854. },
  855. "Permission": "READ"
  856. }]
  857. };
  858. var AccessControlPolicy2 = {
  859. "Owner": {
  860. "ID": 'qcs::cam::uin/10001:uin/10001' // 10001 是 QQ 号
  861. },
  862. "Grant": {
  863. "Grantee": {
  864. "ID": "qcs::cam::uin/10002:uin/10002", // 10002 是 QQ 号
  865. },
  866. "Permission": "READ"
  867. }
  868. };
  869. it('putObjectAcl() header ACL:private', function (assert) {
  870. return new Promise(function (done) {
  871. cos.putObject({
  872. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  873. Region: config.Region,
  874. Key: '1.txt',
  875. Body: util.str2blob('hello!'),
  876. }, function (err, data) {
  877. assert.ok(!err);
  878. cos.putObjectAcl({
  879. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  880. Region: config.Region,
  881. ACL: 'private',
  882. Key: '1.txt',
  883. }, function (err, data) {
  884. assert.ok(!err, 'putObjectAcl 成功');
  885. cos.getObjectAcl({
  886. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  887. Region: config.Region,
  888. Key: '1.txt'
  889. }, function (err, data) {
  890. assert.ok(data.ACL = 'private');
  891. AccessControlPolicy.Owner.ID = data.Owner.ID;
  892. AccessControlPolicy2.Owner.ID = data.Owner.ID;
  893. assert.ok(data.Grants.length === 1);
  894. done();
  895. });
  896. });
  897. });
  898. });
  899. });
  900. it('putObjectAcl() header ACL:default', function (assert) {
  901. return new Promise(function (done) {
  902. cos.putObjectAcl({
  903. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  904. Region: config.Region,
  905. ACL: 'default',
  906. Key: '1.txt',
  907. }, function (err, data) {
  908. assert.ok(!err, 'putObjectAcl 成功');
  909. cos.getObjectAcl({
  910. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  911. Region: config.Region,
  912. Key: '1.txt'
  913. }, function (err, data) {
  914. assert.ok(data.ACL = 'default');
  915. done();
  916. });
  917. });
  918. });
  919. });
  920. it('putObjectAcl() header ACL:public-read', function (assert) {
  921. return new Promise(function (done) {
  922. cos.putObjectAcl({
  923. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  924. Region: config.Region,
  925. ACL: 'public-read',
  926. Key: '1.txt',
  927. }, function (err, data) {
  928. assert.ok(!err, 'putObjectAcl 成功');
  929. cos.getObjectAcl({Bucket: config.Bucket, Region: config.Region, Key: '1.txt'}, function (err, data) { // Bucket 格式:test-1250000000
  930. assert.ok(data.ACL = 'public-read');
  931. done();
  932. });
  933. });
  934. });
  935. });
  936. it('putObjectAcl() header ACL:public-read-write', function (assert) {
  937. return new Promise(function (done) {
  938. cos.putObjectAcl({
  939. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  940. Region: config.Region,
  941. ACL: 'public-read-write',
  942. Key: '1.txt',
  943. }, function (err, data) {
  944. assert.ok(!err, 'putObjectAcl 成功');
  945. cos.getObjectAcl({Bucket: config.Bucket, Region: config.Region, Key: '1.txt'}, function (err, data) { // Bucket 格式:test-1250000000
  946. assert.ok(data.ACL = 'public-read-write');
  947. done();
  948. });
  949. });
  950. });
  951. });
  952. it('putObjectAcl() header GrantRead:1001,1002', function (assert) {
  953. return new Promise(function (done) {
  954. var GrantRead = 'id="qcs::cam::uin/1001:uin/1001",id="qcs::cam::uin/1002:uin/1002"';
  955. cos.putObjectAcl({
  956. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  957. Region: config.Region,
  958. GrantRead: GrantRead,
  959. Key: '1.txt',
  960. }, function (err, data) {
  961. assert.ok(!err, 'putObjectAcl 成功');
  962. cos.getObjectAcl({Bucket: config.Bucket, Region: config.Region, Key: '1.txt'}, function (err, data) { // Bucket 格式:test-1250000000
  963. assert.ok(data.GrantRead = GrantRead);
  964. done();
  965. });
  966. });
  967. });
  968. });
  969. it('putObjectAcl() header GrantWrite:1001,1002', function (assert) {
  970. return new Promise(function (done) {
  971. var GrantWrite = 'id="qcs::cam::uin/1001:uin/1001", id="qcs::cam::uin/1002:uin/1002"';
  972. cos.putObjectAcl({
  973. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  974. Region: config.Region,
  975. GrantWrite: GrantWrite,
  976. Key: '1.txt',
  977. }, function (err, data) {
  978. assert.ok(!err, 'putObjectAcl 成功');
  979. cos.getObjectAcl({Bucket: config.Bucket, Region: config.Region, Key: '1.txt'}, function (err, data) { // Bucket 格式:test-1250000000
  980. assert.ok(data.GrantWrite = GrantWrite);
  981. done();
  982. });
  983. });
  984. });
  985. });
  986. it('putObjectAcl() header GrantFullControl:1001,1002', function (assert) {
  987. return new Promise(function (done) {
  988. var GrantFullControl = 'id="qcs::cam::uin/1001:uin/1001", id="qcs::cam::uin/1002:uin/1002"';
  989. cos.putObjectAcl({
  990. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  991. Region: config.Region,
  992. GrantFullControl: GrantFullControl,
  993. Key: '1.txt',
  994. }, function (err, data) {
  995. assert.ok(!err, 'putObjectAcl 成功');
  996. cos.getObjectAcl({Bucket: config.Bucket, Region: config.Region, Key: '1.txt'}, function (err, data) { // Bucket 格式:test-1250000000
  997. assert.ok(data.GrantFullControl = GrantFullControl);
  998. done();
  999. });
  1000. });
  1001. });
  1002. });
  1003. it('putObjectAcl() header ACL:public-read, GrantRead:1001,1002', function (assert) {
  1004. return new Promise(function (done) {
  1005. var GrantFullControl = 'id="qcs::cam::uin/1001:uin/1001", id="qcs::cam::uin/1002:uin/1002"';
  1006. cos.putObjectAcl({
  1007. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1008. Region: config.Region,
  1009. GrantFullControl: GrantFullControl,
  1010. ACL: 'public-read',
  1011. Key: '1.txt',
  1012. }, function (err, data) {
  1013. assert.ok(!err, 'putObjectAcl 成功');
  1014. cos.getObjectAcl({Bucket: config.Bucket, Region: config.Region, Key: '1.txt'}, function (err, data) { // Bucket 格式:test-1250000000
  1015. assert.ok(data.GrantFullControl = GrantFullControl);
  1016. assert.ok(data.ACL = 'public-read');
  1017. done();
  1018. });
  1019. });
  1020. });
  1021. });
  1022. it('putObjectAcl() xml', function (assert) {
  1023. return new Promise(function (done) {
  1024. cos.putObjectAcl({
  1025. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1026. Region: config.Region,
  1027. AccessControlPolicy: AccessControlPolicy,
  1028. Key: '1.txt',
  1029. }, function (err, data) {
  1030. assert.ok(!err, 'putObjectAcl 成功');
  1031. cos.getBucketAcl({Bucket: config.Bucket, Region: config.Region, Key: '1.txt'}, function (err, data) { // Bucket 格式:test-1250000000
  1032. assert.ok(data.Grants.length === 1);
  1033. assert.ok(data.Grants[0] && data.Grants[0].Grantee.ID === 'qcs::cam::uin/10002:uin/10002', '设置 AccessControlPolicy ID 正确');
  1034. assert.ok(data.Grants[0] && data.Grants[0].Permission === 'READ', '设置 AccessControlPolicy Permission 正确');
  1035. done();
  1036. });
  1037. });
  1038. });
  1039. });
  1040. it('putObjectAcl() xml2', function (assert) {
  1041. return new Promise(function (done) {
  1042. cos.putObjectAcl({
  1043. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1044. Region: config.Region,
  1045. AccessControlPolicy: AccessControlPolicy2,
  1046. Key: '1.txt',
  1047. }, function (err, data) {
  1048. assert.ok(!err, 'putObjectAcl 成功');
  1049. cos.getObjectAcl({
  1050. Bucket: config.Bucket,
  1051. Region: config.Region,
  1052. Key: '1.txt'
  1053. }, function (err, data) { // Bucket 格式:test-1250000000
  1054. assert.ok(data.Grants.length === 1);
  1055. assert.ok(data.Grants[0] && data.Grants[0].Grantee.ID === 'qcs::cam::uin/10002:uin/10002', 'ID 正确');
  1056. assert.ok(data.Grants[0] && data.Grants[0].Permission === 'READ', 'Permission 正确');
  1057. done();
  1058. });
  1059. });
  1060. });
  1061. });
  1062. it('putObjectAcl() decodeAcl', function (assert) {
  1063. return new Promise(function (done) {
  1064. cos.getObjectAcl({
  1065. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1066. Region: config.Region,
  1067. Key: '1.txt'
  1068. }, function (err, data) {
  1069. cos.putObjectAcl({
  1070. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1071. Region: config.Region,
  1072. Key: '1.txt',
  1073. GrantFullControl: data.GrantFullControl,
  1074. GrantWrite: data.GrantWrite,
  1075. GrantRead: data.GrantRead,
  1076. ACL: data.ACL,
  1077. }, function (err, data) {
  1078. assert.ok(data);
  1079. done();
  1080. });
  1081. });
  1082. });
  1083. });
  1084. })();
  1085. (function () {
  1086. var CORSRules = [{
  1087. "AllowedOrigins": ["*"],
  1088. "AllowedMethods": ["GET", "POST", "PUT", "DELETE", "HEAD"],
  1089. "AllowedHeaders": ["*"],
  1090. "ExposeHeaders": ["ETag","Content-Length"],
  1091. "MaxAgeSeconds": "5"
  1092. }];
  1093. var CORSRulesMulti = [{
  1094. "AllowedOrigins": ["*"],
  1095. "AllowedMethods": ["GET", "POST", "PUT", "DELETE", "HEAD"],
  1096. "AllowedHeaders": ["*"],
  1097. "ExposeHeaders": ["ETag","Content-Length"],
  1098. "MaxAgeSeconds": "5"
  1099. }, {
  1100. "AllowedOrigins": ["http://qq.com", "http://qcloud.com"],
  1101. "AllowedMethods": ["GET", "POST", "PUT", "DELETE", "HEAD"],
  1102. "AllowedHeaders": ["*"],
  1103. "ExposeHeaders": ["ETag"],
  1104. "MaxAgeSeconds": "5"
  1105. }];
  1106. it('putBucketCors(),getBucketCors()', function (assert) {
  1107. return new Promise(function (done) {
  1108. CORSRules[0].AllowedHeaders.push('test-' + Date.now().toString(36));
  1109. cos.putBucketCors({
  1110. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1111. Region: config.Region,
  1112. CORSConfiguration: {
  1113. CORSRules: CORSRules
  1114. }
  1115. }, function (err, data) {
  1116. assert.ok(!err);
  1117. setTimeout(function () {
  1118. cos.getBucketCors({
  1119. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1120. Region: config.Region
  1121. }, function (err, data) {
  1122. assert.ok(data && comparePlainObject(CORSRules, data.CORSRules));
  1123. done();
  1124. });
  1125. }, 2000);
  1126. });
  1127. });
  1128. });
  1129. it('putBucketCors() old', function (assert) {
  1130. return new Promise(function (done) {
  1131. CORSRules[0].AllowedHeaders.push('test-' + Date.now().toString(36));
  1132. cos.putBucketCors({
  1133. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1134. Region: config.Region,
  1135. CORSConfiguration: {
  1136. CORSRules: CORSRules
  1137. }
  1138. }, function (err, data) {
  1139. assert.ok(!err);
  1140. setTimeout(function () {
  1141. cos.getBucketCors({
  1142. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1143. Region: config.Region
  1144. }, function (err, data) {
  1145. assert.ok(comparePlainObject(CORSRules, data.CORSRules));
  1146. done();
  1147. });
  1148. }, 2000);
  1149. });
  1150. });
  1151. });
  1152. it('putBucketCors() multi', function (assert) {
  1153. return new Promise(function (done) {
  1154. cos.putBucketCors({
  1155. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1156. Region: config.Region,
  1157. CORSConfiguration: {
  1158. CORSRules: CORSRulesMulti
  1159. }
  1160. }, function (err, data) {
  1161. assert.ok(!err);
  1162. setTimeout(function () {
  1163. cos.getBucketCors({
  1164. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1165. Region: config.Region
  1166. }, function (err, data) {
  1167. assert.ok(comparePlainObject(CORSRulesMulti, data.CORSRules));
  1168. done();
  1169. });
  1170. }, 2000);
  1171. });
  1172. });
  1173. });
  1174. })();
  1175. (function () {
  1176. var Tags = [
  1177. {Key: "k1", Value: "v1"}
  1178. ];
  1179. var TagsMulti = [
  1180. {Key: "k1", Value: "v1"},
  1181. {Key: "k2", Value: "v2"},
  1182. ];
  1183. it('putBucketTagging(),getBucketTagging()', function (assert) {
  1184. return new Promise(function (done) {
  1185. Tags[0].Value = Date.now().toString(36);
  1186. cos.putBucketTagging({
  1187. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1188. Region: config.Region,
  1189. Tagging: {
  1190. Tags: Tags
  1191. }
  1192. }, function (err, data) {
  1193. assert.ok(!err);
  1194. setTimeout(function () {
  1195. cos.getBucketTagging({
  1196. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1197. Region: config.Region
  1198. }, function (err, data) {
  1199. assert.ok(comparePlainObject(Tags, data.Tags));
  1200. done();
  1201. });
  1202. }, 1000);
  1203. });
  1204. });
  1205. });
  1206. it('deleteBucketTagging()', function (assert) {
  1207. return new Promise(function (done) {
  1208. cos.deleteBucketTagging({
  1209. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1210. Region: config.Region
  1211. }, function (err, data) {
  1212. assert.ok(!err);
  1213. setTimeout(function () {
  1214. cos.getBucketTagging({
  1215. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1216. Region: config.Region
  1217. }, function (err, data) {
  1218. assert.ok(comparePlainObject([], data.Tags));
  1219. done();
  1220. });
  1221. }, 1000);
  1222. });
  1223. });
  1224. });
  1225. it('putBucketTagging() multi', function (assert) {
  1226. return new Promise(function (done) {
  1227. Tags[0].Value = Date.now().toString(36);
  1228. cos.putBucketTagging({
  1229. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1230. Region: config.Region,
  1231. Tagging: {
  1232. Tags: TagsMulti
  1233. }
  1234. }, function (err, data) {
  1235. assert.ok(!err);
  1236. setTimeout(function () {
  1237. cos.getBucketTagging({
  1238. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1239. Region: config.Region
  1240. }, function (err, data) {
  1241. assert.ok(comparePlainObject(TagsMulti, data.Tags));
  1242. done();
  1243. });
  1244. }, 2000);
  1245. });
  1246. });
  1247. });
  1248. })();
  1249. (function () {
  1250. var Prefix = Date.now().toString(36);
  1251. var Policy = {
  1252. "version": "2.0",
  1253. "principal": {"qcs": ["qcs::cam::uin/10001:uin/10001"]}, // 这里的 10001 是 QQ 号
  1254. "statement": [{
  1255. "effect": "allow",
  1256. "action": [
  1257. "name/cos:GetBucket",
  1258. "name/cos:PutObject",
  1259. "name/cos:PostObject",
  1260. "name/cos:PutObjectCopy",
  1261. "name/cos:InitiateMultipartUpload",
  1262. "name/cos:UploadPart",
  1263. "name/cos:UploadPartCopy",
  1264. "name/cos:CompleteMultipartUpload",
  1265. "name/cos:AbortMultipartUpload",
  1266. "name/cos:AppendObject"
  1267. ],
  1268. "resource": ["qcs::cos:" + config.Region + ":uid/" + AppId + ":" + BucketLongName + ".cos." + config.Region + ".yun.ccb.com//" + AppId + "/" + BucketShortName + "/" + Prefix + "/*"] // 1250000000 是 appid
  1269. }]
  1270. };
  1271. it('putBucketPolicy(),getBucketPolicy()', function (assert) {
  1272. return new Promise(function (done) {
  1273. cos.putBucketPolicy({
  1274. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1275. Region: config.Region,
  1276. Policy: Policy
  1277. }, function (err, data) {
  1278. assert.ok(!err);
  1279. cos.getBucketPolicy({
  1280. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1281. Region: config.Region
  1282. }, function (err, data) {
  1283. assert.ok(Policy, data.Policy);
  1284. done();
  1285. });
  1286. });
  1287. });
  1288. });
  1289. it('putBucketPolicy() s3', function (assert) {
  1290. return new Promise(function (done) {
  1291. cos.putBucketPolicy({
  1292. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1293. Region: config.Region,
  1294. Policy: JSON.stringify(Policy)
  1295. }, function (err, data) {
  1296. assert.ok(!err);
  1297. cos.getBucketPolicy({
  1298. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1299. Region: config.Region
  1300. }, function (err, data) {
  1301. assert.ok(Policy, data.Policy);
  1302. done();
  1303. });
  1304. });
  1305. });
  1306. });
  1307. })();
  1308. it('getBucketLocation()', function (assert) {
  1309. return new Promise(function (done) {
  1310. cos.getBucketLocation({
  1311. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1312. Region: config.Region
  1313. }, function (err, data) {
  1314. var map1 = {
  1315. 'tianjin': 'ap-beijing-1',
  1316. 'cn-south-2': 'ap-guangzhou-2',
  1317. 'cn-south': 'ap-guangzhou',
  1318. 'cn-east': 'ap-shanghai',
  1319. 'cn-southwest': 'ap-chengdu',
  1320. };
  1321. var map2 = {
  1322. 'ap-beijing-1': 'tianjin',
  1323. 'ap-guangzhou-2': 'cn-south-2',
  1324. 'ap-guangzhou': 'cn-south',
  1325. 'ap-shanghai': 'cn-east',
  1326. 'ap-chengdu': 'cn-southwest',
  1327. };
  1328. assert.ok(data.LocationConstraint === config.Region || data.LocationConstraint === map1[config.Region] ||
  1329. data.LocationConstraint === map2[config.Region]);
  1330. done();
  1331. });
  1332. });
  1333. });
  1334. (function () {
  1335. var Rules = [{
  1336. 'ID': '1',
  1337. 'Filter': {
  1338. 'Prefix': 'test_' + Date.now().toString(36),
  1339. },
  1340. 'Status': 'Enabled',
  1341. 'Transition': {
  1342. 'Date': '2018-07-29T16:00:00.000Z',
  1343. 'StorageClass': 'STANDARD_IA'
  1344. }
  1345. }];
  1346. var RulesMulti = [{
  1347. 'ID': '1',
  1348. 'Filter': {
  1349. 'Prefix': 'test1_' + Date.now().toString(36),
  1350. },
  1351. 'Status': 'Enabled',
  1352. 'Transition': {
  1353. 'Date': '2018-07-29T16:00:00.000Z',
  1354. 'StorageClass': 'STANDARD_IA'
  1355. }
  1356. }, {
  1357. 'ID': '2',
  1358. 'Filter': {
  1359. 'Prefix': 'test2_' + Date.now().toString(36),
  1360. },
  1361. 'Status': 'Enabled',
  1362. 'Transition': {
  1363. 'Date': '2018-07-29T16:00:00.000Z',
  1364. 'StorageClass': 'STANDARD_IA'
  1365. }
  1366. }];
  1367. it('deleteBucketLifecycle()', function (assert) {
  1368. return new Promise(function (done) {
  1369. cos.deleteBucketLifecycle({
  1370. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1371. Region: config.Region
  1372. }, function (err, data) {
  1373. assert.ok(!err);
  1374. setTimeout(function () {
  1375. cos.getBucketLifecycle({
  1376. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1377. Region: config.Region
  1378. }, function (err, data) {
  1379. assert.ok(comparePlainObject([], data.Rules));
  1380. done();
  1381. });
  1382. }, 2000);
  1383. });
  1384. });
  1385. });
  1386. it('putBucketLifecycle(),getBucketLifecycle()', function (assert) {
  1387. return new Promise(function (done) {
  1388. Rules[0].Filter.Prefix = 'test_' + Date.now().toString(36);
  1389. cos.putBucketLifecycle({
  1390. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1391. Region: config.Region,
  1392. LifecycleConfiguration: {
  1393. Rules: Rules
  1394. }
  1395. }, function (err, data) {
  1396. assert.ok(!err);
  1397. setTimeout(function () {
  1398. cos.getBucketLifecycle({
  1399. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1400. Region: config.Region
  1401. }, function (err, data) {
  1402. assert.ok(comparePlainObject(Rules, data && data.Rules));
  1403. done();
  1404. });
  1405. }, 2000);
  1406. });
  1407. });
  1408. });
  1409. it('putBucketLifecycle() multi', function (assert) {
  1410. return new Promise(function (done) {
  1411. Rules[0].Filter.Prefix = 'test_' + Date.now().toString(36);
  1412. cos.putBucketLifecycle({
  1413. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1414. Region: config.Region,
  1415. LifecycleConfiguration: {
  1416. Rules: RulesMulti
  1417. }
  1418. }, function (err, data) {
  1419. assert.ok(!err);
  1420. setTimeout(function () {
  1421. cos.getBucketLifecycle({
  1422. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1423. Region: config.Region
  1424. }, function (err, data) {
  1425. assert.ok(comparePlainObject(RulesMulti, data.Rules));
  1426. done();
  1427. });
  1428. }, 2000);
  1429. });
  1430. });
  1431. });
  1432. })();
  1433. it('params check', function (assert) {
  1434. return new Promise(function (done) {
  1435. cos.headBucket({
  1436. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1437. Region: 'gz'
  1438. }, function (err, data) {
  1439. assert.ok(err.error === 'error');
  1440. done();
  1441. });
  1442. });
  1443. });
  1444. it('params check', function (assert) {
  1445. return new Promise(function (done) {
  1446. cos.headBucket({
  1447. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1448. Region: 'cos.ap-guangzhou'
  1449. }, function (err, data) {
  1450. assert.ok(err.error === 'param Region should not be start with "cos."');
  1451. done();
  1452. });
  1453. });
  1454. });
  1455. it('sliceCopyFile() 正常分片复制', function (assert) {
  1456. return new Promise(function (done) {
  1457. var fileName = '10mb.zip';
  1458. var Key = '10mb.copy.zip';
  1459. var blob = util.createFile({size: 1024 * 1024 * 10});
  1460. cos.putObject({
  1461. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1462. Region: config.Region,
  1463. Key: fileName,
  1464. Body: blob,
  1465. }, function (err, data) {
  1466. cos.sliceCopyFile({
  1467. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1468. Region: config.Region,
  1469. Key: Key,
  1470. CopySource: config.Bucket + '.cos.' + config.Region + '.yun.ccb.com/'+ fileName,
  1471. SliceSize: 5 * 1024 * 1024,
  1472. onProgress:function (processData) {
  1473. lastPercent = processData.percent;
  1474. }
  1475. }, function (err, data) {
  1476. assert.ok(data && data.ETag, '成功进行分片复制');
  1477. done();
  1478. });
  1479. });
  1480. });
  1481. });
  1482. it('sliceCopyFile() 单片复制', function (assert) {
  1483. return new Promise(function (done) {
  1484. var fileName = '10mb.zip';
  1485. var Key = '10mb.copy.zip';
  1486. cos.sliceCopyFile({
  1487. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1488. Region: config.Region,
  1489. Key: Key,
  1490. CopySource: config.Bucket + '.cos.' + config.Region + '.yun.ccb.com/'+ fileName,
  1491. SliceSize: 10 * 1024 * 1024,
  1492. }, function (err, data) {
  1493. if (err) throw err;
  1494. assert.ok(data && data.ETag, '成功进行单片复制');
  1495. done();
  1496. });
  1497. });
  1498. });
  1499. (function () {
  1500. function dataURItoBlob(dataURI) {
  1501. var byteString = atob(dataURI.split(',')[1]);
  1502. var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
  1503. var ab = new ArrayBuffer(byteString.length);
  1504. var ia = new Uint8Array(ab);
  1505. for (var i = 0; i < byteString.length; i++) {
  1506. ia[i] = byteString.charCodeAt(i);
  1507. }
  1508. return new Blob([ab], {type: mimeString});
  1509. }
  1510. var fileBlob = dataURItoBlob('data:text/plain;base64,5Lit5paH');
  1511. // 这里两个用户正式测试的时候需要给 putObject 计算并加上 Content-MD5 字段
  1512. it('putObject 带 Content-MD5 中文文件内容', function (assert) {
  1513. return new Promise(function (done) {
  1514. var Key = '中文.txt';
  1515. cos.putObject({
  1516. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1517. Region: config.Region,
  1518. Key: Key,
  1519. Body: fileBlob,
  1520. }, function (err, data) {
  1521. assert.ok(data && data.ETag, '成功进行上传');
  1522. done();
  1523. });
  1524. });
  1525. });
  1526. // it('putObject 带 Content-MD5 中文字符串', function (assert) {
  1527. // return new Promise(function (done) {
  1528. // var Key = '中文.txt';
  1529. // cos.putObject({
  1530. // Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1531. // Region: config.Region,
  1532. // Key: Key,
  1533. // Body: fileBlob,
  1534. // }, function (err, data) {
  1535. // assert.ok(data && data.ETag, '成功进行上传');
  1536. // done();
  1537. // });
  1538. // });
  1539. // });
  1540. })();
  1541. it('deleteMultipleObject Key 带中文字符', function (assert) {
  1542. return new Promise(function (done) {
  1543. cos.deleteMultipleObject({
  1544. Bucket: config.Bucket, // Bucket 格式:test-1250000000
  1545. Region: config.Region,
  1546. Objects: [
  1547. {Key: '中文/中文.txt'},
  1548. {Key: '中文/中文.zip'},
  1549. ]
  1550. }, function (err, data) {
  1551. assert.ok(!err, '成功进行批量删除');
  1552. done();
  1553. });
  1554. });
  1555. });
  1556. </script>
  1557. </body>
  1558. </html>