test.js 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376
  1. var config = {
  2. Bucket: 'test-1250000000',
  3. Region: 'ap-guangzhou'
  4. };
  5. var util = {
  6. createFile: function (options) {
  7. var buffer = new ArrayBuffer(options.size || 0);
  8. var arr = new Uint8Array(buffer);
  9. for (var i = 0; i < arr.length; i++) {
  10. arr[i] = 0;
  11. }
  12. var opt = {};
  13. options.type && (opt.type = options.type);
  14. var blob = new Blob([buffer], options);
  15. return blob;
  16. },
  17. str2blob: function (str) {
  18. var size = str.length;
  19. var buffer = new ArrayBuffer(size || 0);
  20. var arr = new Uint8Array(buffer);
  21. for (var i = 0; i < arr.length; i++) {
  22. arr[i] = str[i];
  23. }
  24. var blob = new Blob([buffer]);
  25. return blob;
  26. }
  27. };
  28. function camSafeUrlEncode(str) {
  29. return encodeURIComponent(str)
  30. .replace(/!/g, '%21')
  31. .replace(/'/g, '%27')
  32. .replace(/\(/g, '%28')
  33. .replace(/\)/g, '%29')
  34. .replace(/\*/g, '%2A');
  35. }
  36. var getAuthorization = function (options, callback) {
  37. // 格式一、(推荐)后端通过获取临时密钥给到前端,前端计算签名
  38. // 服务端 JS 和 PHP 例子:https://github.com/tencentyun/cos-js-sdk-v5/blob/master/server/
  39. // 服务端其他语言参考 COS STS SDK :https://github.com/tencentyun/qcloud-cos-sts-sdk
  40. // var url = '../server/sts.php';
  41. var url = '/sts';
  42. var xhr = new XMLHttpRequest();
  43. xhr.open('GET', url, true);
  44. xhr.onload = function (e) {
  45. try {
  46. var data = JSON.parse(e.target.responseText);
  47. var credentials = data.credentials;
  48. } catch (e) {
  49. }
  50. callback({
  51. TmpSecretId: credentials.tmpSecretId,
  52. TmpSecretKey: credentials.tmpSecretKey,
  53. XCosSecurityToken: credentials.sessionToken,
  54. ExpiredTime: data.expiredTime, // SDK 在 ExpiredTime 时间前,不会再次调用 getAuthorization
  55. });
  56. };
  57. xhr.send();
  58. // // 格式二、(推荐)【细粒度控制权限】后端通过获取临时密钥给到前端,前端只有相同请求才重用临时密钥,后端可以通过 Scope 细粒度控制权限
  59. // // 服务端例子:https://github.com/tencentyun/qcloud-cos-sts-sdk/edit/master/scope.md
  60. // // var url = '../server/sts.php';
  61. // var url = '/sts-scope';
  62. // var xhr = new XMLHttpRequest();
  63. // xhr.open('POST', url, true);
  64. // xhr.setRequestHeader('Content-Type', 'application/json');
  65. // xhr.onload = function (e) {
  66. // try {
  67. // var data = JSON.parse(e.target.responseText);
  68. // var credentials = data.credentials;
  69. // } catch (e) {
  70. // }
  71. // callback({
  72. // TmpSecretId: credentials.tmpSecretId,
  73. // TmpSecretKey: credentials.tmpSecretKey,
  74. // XCosSecurityToken: credentials.sessionToken,
  75. // ExpiredTime: data.expiredTime,
  76. // ScopeLimit: true, // 设为 true 可限制密钥只在相同请求可重用,默认不限制一直可重用,细粒度控制权限需要设为 true
  77. // });
  78. // };
  79. // xhr.send(JSON.stringify(options.Scope));
  80. // // 格式三、(不推荐,分片上传权限不好控制)前端每次请求前都需要通过 getAuthorization 获取签名,后端使用固定密钥或临时密钥计算签名返回给前端
  81. // // 服务端获取签名,请参考对应语言的 COS SDK:https://cloud.tencent.com/document/product/436/6474
  82. // // 注意:这种有安全风险,后端需要通过 method、pathname 严格控制好权限,比如不允许 put / 等
  83. // var method = (options.Method || 'get').toLowerCase();
  84. // var query = options.Query || {};
  85. // var headers = options.Headers || {};
  86. // var pathname = options.Pathname || '/';
  87. // // var url = 'http://127.0.0.1:3000/auth';
  88. // var url = '../server/auth.php';
  89. // var xhr = new XMLHttpRequest();
  90. // var data = {
  91. // method: method,
  92. // pathname: pathname,
  93. // query: query,
  94. // headers: headers,
  95. // };
  96. // xhr.open('POST', url, true);
  97. // xhr.setRequestHeader('content-type', 'application/json');
  98. // xhr.onload = function (e) {
  99. // try {
  100. // var data = JSON.parse(e.target.responseText);
  101. // } catch (e) {
  102. // }
  103. // callback({
  104. // Authorization: data.authorization,
  105. // // XCosSecurityToken: data.sessionToken, // 如果使用临时密钥,需要把 sessionToken 传给 XCosSecurityToken
  106. // });
  107. // };
  108. // xhr.send(JSON.stringify(data));
  109. // // 格式四、(不推荐,适用于前端调试,避免泄露密钥)前端使用固定密钥计算签名,通过 COS.getAuthorization 静态方法计算
  110. // var authorization = COS.getAuthorization({
  111. // SecretId: 'AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', // 可传固定密钥或者临时密钥
  112. // SecretKey: 'xxx', // 可传固定密钥或者临时密钥
  113. // Method: options.Method,
  114. // Pathname: options.Pathname,
  115. // Query: options.Query,
  116. // Headers: options.Headers,
  117. // Expires: 900,
  118. // });
  119. // callback({
  120. // Authorization: authorization,
  121. // // XCosSecurityToken: credentials.sessionToken, // 如果使用临时密钥,需要传 XCosSecurityToken
  122. // });
  123. // // 格式五、(不推荐,适用于前端调试,避免泄露密钥)前端使用固定密钥计算签名,通过 cos-auth.js 计算
  124. // var auth = CosAuth({
  125. // Version: '4.0',
  126. // SecretId: 'xxx',
  127. // SecretKey: 'xxx',
  128. // Bucket: config.Bucket,
  129. // Expires: 900,
  130. // Pathname: '/' + (options.Headers && options.Headers['x-cos-copy-source'] ? '' : options.Key),
  131. // });
  132. // callback({Authorization: auth});
  133. };
  134. var dataURItoUploadBody = function (dataURI) {
  135. var byteString = atob(dataURI.split(',')[1]);
  136. var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
  137. var ab = new ArrayBuffer(byteString.length);
  138. var ia = new Uint8Array(ab);
  139. for (var i = 0; i < byteString.length; i++) {
  140. ia[i] = byteString.charCodeAt(i);
  141. }
  142. return new Blob([ab], {type: mimeString});
  143. };
  144. var createFileSync = function (size) {
  145. return util.createFile({size: size});
  146. };
  147. var group = function (name, fn) {
  148. return fn();
  149. };
  150. var test = function (name, fn) {
  151. QUnit.test(name, function (assert) {
  152. return new Promise(function (done) {
  153. fn(done, assert);
  154. });
  155. });
  156. };
  157. var request = function (opt, callback) {
  158. var i;
  159. var xhr = new XMLHttpRequest();
  160. xhr.open('GET', opt.url, true);
  161. if (opt.headers) {
  162. for (i in opt.headers) {
  163. if (opt.headers.hasOwnProperty(i)) {
  164. xhr.setRequestHeader(i, opt.headers[i]);
  165. }
  166. }
  167. }
  168. xhr.onload = function (e) {
  169. callback(null, {statusCode: xhr.status}, xhr.responseText);
  170. };
  171. xhr.onerror = function (e) {
  172. callback('error', {statusCode: xhr.status});
  173. };
  174. xhr.send();
  175. };
  176. var proxy = '';
  177. var cos = new COS({
  178. // 必选参数
  179. getAuthorization: getAuthorization,
  180. // 可选参数
  181. FileParallelLimit: 3, // 控制文件上传并发数
  182. ChunkParallelLimit: 3, // 控制单个文件下分片上传并发数
  183. ChunkSize: 1024 * 1024, // 控制分片大小,单位 B
  184. ProgressInterval: 1, // 控制 onProgress 回调的间隔
  185. ChunkRetryTimes: 3, // 控制文件切片后单片上传失败后重试次数
  186. UploadCheckContentMd5: true, // 上传过程计算 Content-MD5
  187. });
  188. var AppId = config.AppId;
  189. var Bucket = config.Bucket;
  190. var BucketShortName = Bucket;
  191. var BucketLongName = Bucket + '-' + AppId;
  192. var TaskId;
  193. var match = config.Bucket.match(/^(.+)-(\d+)$/);
  194. if (match) {
  195. BucketLongName = config.Bucket;
  196. BucketShortName = match[1];
  197. AppId = match[2];
  198. }
  199. function comparePlainObject(a, b) {
  200. if (Object.keys(a).length !== Object.keys(b).length) {
  201. return false;
  202. }
  203. for (var key in a) {
  204. if (typeof a[key] === 'object' && typeof b[key] === 'object') {
  205. if (!comparePlainObject(a[key], b[key])) {
  206. return false;
  207. }
  208. } else if (a[key] != b[key]) {
  209. return false;
  210. }
  211. }
  212. return true;
  213. }
  214. function prepareBigObject(needHeaders) {
  215. return new Promise(function (resolve, reject) {
  216. // 创建测试文件
  217. var filename = name || 'bigger.zip';
  218. var content = util.createFile({size: 1024 * 1024 * 10});
  219. var put = function () {
  220. // 调用方法
  221. var params = {
  222. Bucket: config.Bucket,
  223. Region: config.Region,
  224. Key: filename,
  225. Body: content,
  226. ContentLength: content.length,
  227. };
  228. if (needHeaders) {
  229. params.ContentType = 'text/html';
  230. params.CacheControl = 'max-age=7200';
  231. params.ContentDisposition = 'inline;filename=hello.jpg';
  232. params.ContentEncoding = 'gzip';
  233. params.Expires = (new Date()).toGMTString();
  234. params.Headers = {
  235. 'x-cos-meta-test': 'xxx'
  236. };
  237. }
  238. cos.putObject(params, function (err, data) {
  239. err ? reject(err) : resolve()
  240. });
  241. };
  242. put();
  243. });
  244. }
  245. function prepareBucket() {
  246. return new Promise(function (resolve, reject) {
  247. resolve();
  248. });
  249. }
  250. group('mock readAsBinaryString', function () {
  251. test('mock readAsBinaryString', function (done, assert) {
  252. FileReader.prototype._readAsBinaryString = FileReader.prototype.readAsBinaryString;
  253. FileReader.prototype.readAsBinaryString = false;
  254. var filename = '10m.zip';
  255. var blob = util.createFile({size: 1024 * 1024 * 10});
  256. var paused = false;
  257. cos.sliceUploadFile({
  258. Bucket: config.Bucket,
  259. Region: config.Region,
  260. Key: filename,
  261. Body: blob,
  262. onTaskReady: function (taskId) {
  263. TaskId = taskId;
  264. },
  265. onProgress: function (info) {
  266. if (!paused && info.percent > 0.6) {
  267. cos.cancelTask(TaskId);
  268. var hasProgress = false;
  269. cos.sliceUploadFile({
  270. Bucket: config.Bucket,
  271. Region: config.Region,
  272. Key: filename,
  273. Body: blob,
  274. onTaskReady: function (taskId) {
  275. TaskId = taskId;
  276. },
  277. onProgress: function (info) {
  278. if (info.percent === 0) return;
  279. assert.ok(info.percent > 0.3, '分片续传成功');
  280. cos.cancelTask(TaskId);
  281. FileReader.prototype.readAsBinaryString = FileReader.prototype._readAsBinaryString;
  282. delete FileReader.prototype._readAsBinaryString;
  283. done();
  284. }
  285. }, function (err) {
  286. if (hasProgress) {
  287. assert.ok(false, '分片续传');
  288. done();
  289. }
  290. });
  291. }
  292. }
  293. });
  294. });
  295. });
  296. group('getAuth()', function () {
  297. test('getAuth()', function (done, assert) {
  298. var content = Date.now().toString();
  299. var key = '1.txt';
  300. cos.putObject({
  301. Bucket: config.Bucket,
  302. Region: config.Region,
  303. Key: key,
  304. Body: content,
  305. }, function (err, data) {
  306. cos.options.getAuthorization({
  307. Method: 'get',
  308. Key: key,
  309. Scope: [{
  310. action: 'GetObject',
  311. bucket: config.Bucket,
  312. region: config.Region,
  313. prefix: key,
  314. }],
  315. }, function (AuthData) {
  316. if (typeof AuthData === 'string') {
  317. AuthData = {Authorization: AuthData};
  318. }
  319. if (!AuthData.Authorization) {
  320. AuthData.Authorization = COS.getAuthorization({
  321. SecretId: AuthData.TmpSecretId,
  322. SecretKey: AuthData.TmpSecretKey,
  323. Method: 'get',
  324. Key: key,
  325. SystemClockOffset: cos.options.SystemClockOffset,
  326. });
  327. }
  328. var link = 'http://' + config.Bucket + '.cos.' + config.Region + '.myqcloud.com' + '/' +
  329. camSafeUrlEncode(key).replace(/%2F/g, '/') + '?' + AuthData.Authorization +
  330. (AuthData.XCosSecurityToken ? '&x-cos-security-token=' + AuthData.XCosSecurityToken : '');
  331. request({
  332. url: link,
  333. proxy: proxy,
  334. }, function (err, response, body) {
  335. assert.ok(response.statusCode === 200);
  336. assert.ok(body === content);
  337. done();
  338. });
  339. });
  340. });
  341. });
  342. });
  343. group('getObjectUrl()', function () {
  344. test('getObjectUrl()', function (done, assert) {
  345. var content = Date.now().toString();
  346. var key = '1.txt';
  347. cos.putObject({
  348. Bucket: config.Bucket,
  349. Region: config.Region,
  350. Key: key,
  351. Body: content,
  352. }, function (err, data) {
  353. cos.getObjectUrl({
  354. Bucket: config.Bucket,
  355. Region: config.Region,
  356. Key: key,
  357. }, function (err, data) {
  358. request({
  359. url: data.Url,
  360. proxy: proxy,
  361. }, function (err, response, body) {
  362. assert.ok(!err, '文件获取出错');
  363. assert.ok(response.statusCode === 200, '获取文件 200');
  364. assert.ok(body.toString() === content, '通过获取签名能正常获取文件');
  365. done();
  366. });
  367. });
  368. });
  369. });
  370. });
  371. group('auth check', function () {
  372. test('auth check', function (done, assert) {
  373. cos.getBucket({
  374. Bucket: config.Bucket,
  375. Region: config.Region,
  376. Prefix: 'aksjhdlash sajlhj!@#$%^&*()_+=-[]{}\';:"/.<>?.,??sadasd#/.,/~`',
  377. Headers: {
  378. 'x-cos-test': 'aksjhdlash sajlhj!@#$%^&*()_+=-[]{}\';:\"/.<>?.,??sadasd#/.,/~`',
  379. },
  380. }, function (err, data) {
  381. assert.ok(!err);
  382. done();
  383. });
  384. });
  385. });
  386. group('getBucket()', function () {
  387. test('正常获取 bucket 里的文件列表', function (done, assert) {
  388. prepareBucket().then(function () {
  389. cos.getBucket({
  390. Bucket: config.Bucket,
  391. Region: config.Region
  392. }, function (err, data) {
  393. assert.equal(true, data.Name === BucketLongName);
  394. assert.equal(data.Contents.constructor, Array);
  395. done();
  396. });
  397. }).catch(function () {
  398. assert.equal(false);
  399. done();
  400. });
  401. });
  402. });
  403. group('putObject(),cancelTask()', function () {
  404. test('putObject(),cancelTask()', function (done, assert) {
  405. var filename = '10m.zip';
  406. var alive = false;
  407. var canceled = false;
  408. cos.putObject({
  409. Bucket: config.Bucket,
  410. Region: config.Region,
  411. Key: filename,
  412. Body: util.createFile({size: 1024 * 1024 * 10}),
  413. onTaskReady: function (taskId) {
  414. TaskId = taskId;
  415. },
  416. onProgress: function (info) {
  417. alive = true;
  418. if (!canceled) {
  419. cos.cancelTask(TaskId);
  420. alive = false;
  421. canceled = true;
  422. setTimeout(function () {
  423. assert.ok(!alive, '取消上传已经生效');
  424. done();
  425. }, 1200);
  426. }
  427. }
  428. }, function (err, data) {
  429. alive = true;
  430. });
  431. });
  432. });
  433. group('sliceUploadFile() 完整上传文件', function () {
  434. test('sliceUploadFile() 完整上传文件', function (done, assert) {
  435. var lastPercent;
  436. var filename = '3m.zip';
  437. var fileSize = 1024 * 1024 * 3;
  438. var blob = createFileSync(fileSize)
  439. cos.abortUploadTask({
  440. Bucket: config.Bucket,
  441. Region: config.Region,
  442. Key: filename,
  443. Level: 'file',
  444. }, function (err, data) {
  445. cos.sliceUploadFile({
  446. Bucket: config.Bucket,
  447. Region: config.Region,
  448. Key: filename,
  449. Body: blob,
  450. onTaskReady: function (taskId) {
  451. TaskId = taskId;
  452. },
  453. onProgress: function (info) {
  454. lastPercent = info.percent;
  455. }
  456. }, function (err, data) {
  457. assert.ok(data.ETag.length > 0);
  458. cos.headObject({
  459. Bucket: config.Bucket,
  460. Region: config.Region,
  461. Key: filename,
  462. }, function (err, data) {
  463. assert.ok(data && data.headers && data.headers.etag && data.headers.etag.length > 0, '文件已上传成功');
  464. assert.ok(data && data.headers && parseInt(data.headers['content-length'] || 0) === fileSize, '文件大小一致');
  465. done();
  466. });
  467. });
  468. });
  469. });
  470. });
  471. group('sliceUploadFile(),pauseTask(),restartTask()', function () {
  472. test('sliceUploadFile(),pauseTask(),restartTask()', function (done, assert) {
  473. var filename = '10m.zip';
  474. var blob = util.createFile({size: 1024 * 1024 * 10});
  475. var paused = false;
  476. var restarted = false;
  477. cos.abortUploadTask({
  478. Bucket: config.Bucket,
  479. Region: config.Region,
  480. Key: filename,
  481. Level: 'file',
  482. }, function (err, data) {
  483. cos.sliceUploadFile({
  484. Bucket: config.Bucket,
  485. Region: config.Region,
  486. Key: filename,
  487. Body: blob,
  488. onTaskReady: function (taskId) {
  489. TaskId = taskId;
  490. },
  491. onProgress: function (info) {
  492. if (!paused && info.percent > 0.6) {
  493. cos.pauseTask(TaskId);
  494. paused = true;
  495. setTimeout(function () {
  496. restarted = true;
  497. cos.restartTask(TaskId);
  498. }, 1000);
  499. }
  500. if (paused && restarted) {
  501. if (info.percent === 0) return;
  502. assert.ok(info.percent > 0.3, '暂停和重试成功');
  503. cos.cancelTask(TaskId);
  504. done();
  505. }
  506. }
  507. }, function (err, data) {
  508. paused = true;
  509. });
  510. });
  511. });
  512. });
  513. group('sliceUploadFile(),cancelTask()', function () {
  514. test('sliceUploadFile(),cancelTask()', function (done, assert) {
  515. var filename = '3m.zip';
  516. var blob = util.createFile({size: 1024 * 1024 * 3});
  517. var alive = false;
  518. var canceled = false;
  519. cos.sliceUploadFile({
  520. Bucket: config.Bucket,
  521. Region: config.Region,
  522. Key: filename,
  523. Body: blob,
  524. onTaskReady: function (taskId) {
  525. TaskId = taskId;
  526. },
  527. onProgress: function (info) {
  528. alive = true;
  529. if (!canceled) {
  530. cos.cancelTask(TaskId);
  531. alive = false;
  532. canceled = true;
  533. setTimeout(function () {
  534. assert.ok(!alive, '取消上传已经生效');
  535. done();
  536. }, 1200);
  537. }
  538. }
  539. }, function (err, data) {
  540. alive = true;
  541. });
  542. });
  543. });
  544. group('abortUploadTask()', function () {
  545. test('abortUploadTask(),Level=task', function (done, assert) {
  546. var filename = '1m.zip';
  547. cos.multipartInit({
  548. Bucket: config.Bucket,
  549. Region: config.Region,
  550. Key: filename,
  551. }, function (err, data) {
  552. cos.abortUploadTask({
  553. Bucket: config.Bucket,
  554. Region: config.Region,
  555. Key: filename,
  556. Level: 'task',
  557. UploadId: data.UploadId,
  558. }, function (err, data) {
  559. var nameExist = false;
  560. data.successList.forEach(function (item) {
  561. if (filename === item.Key) {
  562. nameExist = true;
  563. }
  564. });
  565. assert.ok(data.successList.length >= 1, '成功取消单个分片任务');
  566. assert.ok(nameExist, '成功取消单个分片任务');
  567. done();
  568. });
  569. });
  570. });
  571. test('abortUploadTask(),Level=file', function (done, assert) {
  572. var filename = '1m.zip';
  573. var blob = util.createFile({size: 1024 * 1024});
  574. cos.sliceUploadFile({
  575. Bucket: config.Bucket,
  576. Region: config.Region,
  577. Key: filename,
  578. Body: blob,
  579. onTaskReady: function (taskId) {
  580. TaskId = taskId;
  581. },
  582. onProgress: function (info) {
  583. cos.cancelTask(TaskId);
  584. cos.abortUploadTask({
  585. Bucket: config.Bucket,
  586. Region: config.Region,
  587. Level: 'file',
  588. Key: filename,
  589. }, function (err, data) {
  590. assert.ok(data.successList.length >= 1, '成功舍弃单个文件下的所有分片任务');
  591. assert.ok(data.successList[0] && data.successList[0].Key === filename, '成功舍弃单个文件的所有分片任务');
  592. done();
  593. });
  594. }
  595. });
  596. });
  597. test('abortUploadTask(),Level=bucket', function (done, assert) {
  598. var filename = '1m.zip';
  599. var blob = util.createFile({size: 1024 * 1024 * 10});
  600. cos.sliceUploadFile({
  601. Bucket: config.Bucket,
  602. Region: config.Region,
  603. Key: filename,
  604. Body: blob,
  605. onTaskReady: function (taskId) {
  606. TaskId = taskId;
  607. },
  608. onProgress: function (info) {
  609. cos.cancelTask(TaskId);
  610. cos.abortUploadTask({
  611. Bucket: config.Bucket,
  612. Region: config.Region,
  613. Level: 'bucket',
  614. }, function (err, data) {
  615. var nameExist = false;
  616. data.successList.forEach(function (item) {
  617. if (filename === item.Key) {
  618. nameExist = true;
  619. }
  620. });
  621. assert.ok(data.successList.length >= 1, '成功舍弃Bucket下所有分片任务');
  622. assert.ok(nameExist, '成功舍弃Bucket下所有分片任务');
  623. done();
  624. });
  625. }
  626. });
  627. });
  628. });
  629. group('headBucket()', function () {
  630. test('headBucket()', function (done, assert) {
  631. cos.headBucket({
  632. Bucket: config.Bucket,
  633. Region: config.Region
  634. }, function (err, data) {
  635. assert.ok(data, '正常获取 head bucket');
  636. done();
  637. });
  638. });
  639. test('headBucket() not exist', function (done, assert) {
  640. cos.headBucket({
  641. Bucket: config.Bucket + Date.now().toString(36),
  642. Region: config.Region
  643. }, function (err, data) {
  644. assert.ok(err, 'bucket 不存在');
  645. done();
  646. });
  647. });
  648. test('deleteBucket()', function (done, assert) {
  649. cos.deleteBucket({
  650. Bucket: config.Bucket + Date.now().toString(36),
  651. Region: config.Region
  652. }, function (err, data) {
  653. assert.ok(err, '正常获取 head bucket');
  654. done();
  655. });
  656. });
  657. test('getBucket()', function (done, assert) {
  658. cos.getBucket({
  659. Bucket: config.Bucket,
  660. Region: config.Region
  661. }, function (err, data) {
  662. assert.equal(true, data.Name === BucketLongName, '能列出 bucket');
  663. assert.equal(data.Contents.constructor, Array, '正常获取 bucket 里的文件列表');
  664. done();
  665. });
  666. });
  667. });
  668. group('putObject()', function () {
  669. test('putObject()', function (done, assert) {
  670. var filename = '1.txt';
  671. var getObjectETag = function (callback) {
  672. setTimeout(function () {
  673. cos.headObject({
  674. Bucket: config.Bucket,
  675. Region: config.Region,
  676. Key: filename,
  677. }, function (err, data) {
  678. callback(data && data.headers && data.headers.etag);
  679. });
  680. }, 2000);
  681. };
  682. var content = Date.now().toString();
  683. var lastPercent = 0;
  684. cos.putObject({
  685. Bucket: config.Bucket,
  686. Region: config.Region,
  687. Key: filename,
  688. Body: util.str2blob(content),
  689. onProgress: function (info) {
  690. lastPercent = info.percent;
  691. },
  692. }, function (err, data) {
  693. if (err) throw err;
  694. assert.ok(data && data.ETag, 'putObject 有返回 ETag');
  695. getObjectETag(function (ETag) {
  696. assert.ok(data.ETag === ETag, 'Blob 创建 object');
  697. done();
  698. });
  699. });
  700. });
  701. test('putObject(),string', function (done, assert) {
  702. var filename = '1.txt';
  703. var content = '中文_' + Date.now().toString(36);
  704. var lastPercent = 0;
  705. cos.putObject({
  706. Bucket: config.Bucket,
  707. Region: config.Region,
  708. Key: filename,
  709. Body: content,
  710. onProgress: function (info) {
  711. lastPercent = info.percent;
  712. },
  713. }, function (err, data) {
  714. if (err) throw err;
  715. var ETag = data && data.ETag;
  716. assert.ok(ETag, 'putObject 有返回 ETag');
  717. cos.getObject({
  718. Bucket: config.Bucket,
  719. Region: config.Region,
  720. Key: filename,
  721. }, function (err, data) {
  722. assert.ok(data.Body && data.Body === content && (data.headers && data.headers.etag) === ETag);
  723. done();
  724. });
  725. });
  726. });
  727. test('putObject(),string,empty', function (done, assert) {
  728. var content = '';
  729. var lastPercent = 0;
  730. var Key = '1.txt';
  731. cos.putObject({
  732. Bucket: config.Bucket,
  733. Region: config.Region,
  734. Key: Key,
  735. Body: content,
  736. onProgress: function (info) {
  737. lastPercent = info.percent;
  738. },
  739. }, function (err, data) {
  740. if (err) throw err;
  741. var ETag = data && data.ETag;
  742. assert.ok(ETag, 'putObject 有返回 ETag');
  743. cos.getObject({
  744. Bucket: config.Bucket,
  745. Region: config.Region,
  746. Key: Key,
  747. }, function (err, data) {
  748. assert.ok(data.Body === content && (data.headers && data.headers.etag) === ETag);
  749. done();
  750. });
  751. });
  752. });
  753. });
  754. group('getObject()', function () {
  755. test('getObject() body', function (done, assert) {
  756. var key = '1.txt';
  757. var content = Date.now().toString();
  758. cos.putObject({
  759. Bucket: config.Bucket,
  760. Region: config.Region,
  761. Key: key,
  762. Body: content,
  763. }, function (err, data) {
  764. cos.getObject({
  765. Bucket: config.Bucket,
  766. Region: config.Region,
  767. Key: key
  768. }, function (err, data) {
  769. if (err) throw err;
  770. var objectContent = data.Body.toString();
  771. assert.ok(data.headers['content-length'] === '' + content.length);
  772. assert.ok(objectContent === content);
  773. done();
  774. });
  775. });
  776. });
  777. });
  778. group('Key 特殊字符', function () {
  779. test('Key 特殊字符', function (done, assert) {
  780. cos.putObject({
  781. Bucket: config.Bucket,
  782. Region: config.Region,
  783. Key: '(!\'*) "#$%&+,-./0123456789:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
  784. Body: Date.now().toString()
  785. }, function (err, data) {
  786. if (err) throw err;
  787. assert.ok(data, 'putObject 特殊字符的 Key 能通过');
  788. done();
  789. });
  790. });
  791. });
  792. group('putObjectCopy() 1', function () {
  793. test('putObjectCopy() 1', function (done, assert) {
  794. var content = Date.now().toString(36);
  795. cos.putObject({
  796. Bucket: config.Bucket,
  797. Region: config.Region,
  798. Key: '1.txt',
  799. Body: content,
  800. }, function (err, data) {
  801. var ETag = data.ETag;
  802. cos.deleteObject({
  803. Bucket: config.Bucket,
  804. Region: config.Region,
  805. Key: '1.copy.txt',
  806. }, function (err, data) {
  807. cos.putObjectCopy({
  808. Bucket: config.Bucket,
  809. Region: config.Region,
  810. Key: '1.copy.txt',
  811. CopySource: BucketLongName + '.cos.' + config.Region + '.myqcloud.com/1.txt',
  812. }, function (err, data) {
  813. cos.headObject({
  814. Bucket: config.Bucket,
  815. Region: config.Region,
  816. Key: '1.copy.txt',
  817. }, function (err, data) {
  818. assert.ok(data.headers && data.headers.etag === ETag, '成功复制文件');
  819. done();
  820. });
  821. });
  822. });
  823. });
  824. });
  825. });
  826. group('putObjectCopy()', function () {
  827. var filename = '1.txt';
  828. test('正常复制 object', function (done, assert) {
  829. cos.putObjectCopy({
  830. Bucket: config.Bucket,
  831. Region: config.Region,
  832. Key: '1.copy.txt',
  833. CopySource: config.Bucket + '.cos.' + config.Region + '.myqcloud.com/' + filename,
  834. }, function (err, data) {
  835. assert.ok(!err);
  836. assert.ok(data.ETag.length > 0);
  837. done();
  838. });
  839. });
  840. test('捕获 object 异常', function (done, assert) {
  841. var errFileName = '12345.txt';
  842. cos.putObjectCopy({
  843. Bucket: config.Bucket,
  844. Region: config.Region,
  845. Key: '1.copy.txt',
  846. CopySource: config.Bucket + '.cos.' + config.Region + '.myqcloud.com/' + errFileName,
  847. }, function (err, data) {
  848. assert.equal(true, err.statusCode === 404);
  849. assert.equal(true, err.error.Code === 'NoSuchKey')
  850. done();
  851. });
  852. });
  853. });
  854. group('sliceCopyFile()', function () {
  855. var filename = 'bigger.zip';
  856. var Key = 'bigger.copy.zip';
  857. test('正常分片复制 object', function (done, assert) {
  858. prepareBigObject(true).then(function () {
  859. cos.headObject({
  860. Bucket: config.Bucket,
  861. Region: config.Region,
  862. Key: filename,
  863. }, function (err, data1) {
  864. if (err) throw err;
  865. cos.sliceCopyFile({
  866. Bucket: config.Bucket,
  867. Region: config.Region,
  868. Key: Key,
  869. CopySource: config.Bucket + '.cos.' + config.Region + '.myqcloud.com/'+ filename,
  870. SliceSize: 5 * 1024 * 1024,
  871. },function (err, data) {
  872. if (err) throw err;
  873. assert.ok(data.ETag.length > 0);
  874. cos.headObject({
  875. Bucket: config.Bucket,
  876. Region: config.Region,
  877. Key: Key,
  878. }, function (err, data2) {
  879. if (err) throw err;
  880. delete data1.VersionId;
  881. delete data2.VersionId;
  882. delete data1.headers['x-cos-request-id'];
  883. delete data2.headers['x-cos-request-id'];
  884. delete data1.headers['x-cos-version-id'];
  885. delete data2.headers['x-cos-version-id'];
  886. delete data1.headers['last-modified'];
  887. delete data2.headers['last-modified'];
  888. delete data1.headers['date'];
  889. delete data2.headers['date'];
  890. delete data1.headers['etag'];
  891. delete data2.headers['etag'];
  892. delete data1.ETag;
  893. delete data2.ETag;
  894. assert.ok(comparePlainObject(data1, data2));
  895. done();
  896. });
  897. });
  898. });
  899. }).catch(function () {
  900. assert.ok(false);
  901. done();
  902. });
  903. });
  904. test('单片复制 object', function (done, assert) {
  905. setTimeout(function () {
  906. prepareBigObject(true).then(function () {
  907. cos.headObject({
  908. Bucket: config.Bucket,
  909. Region: config.Region,
  910. Key: filename,
  911. }, function (err, data1) {
  912. if (err) throw err;
  913. cos.sliceCopyFile({
  914. Bucket: config.Bucket,
  915. Region: config.Region,
  916. Key: Key,
  917. CopySource: config.Bucket + '.cos.' + config.Region + '.myqcloud.com/' + filename,
  918. SliceSize: 10 * 1024 * 1024,
  919. }, function (err, data) {
  920. if (err) throw err;
  921. assert.ok(data.ETag.length > 0);
  922. setTimeout(function () {
  923. cos.headObject({
  924. Bucket: config.Bucket,
  925. Region: config.Region,
  926. Key: Key,
  927. }, function (err, data2) {
  928. if (err) throw err;
  929. delete data1.VersionId;
  930. delete data2.VersionId;
  931. delete data1.headers['x-cos-request-id'];
  932. delete data2.headers['x-cos-request-id'];
  933. delete data1.headers['x-cos-version-id'];
  934. delete data2.headers['x-cos-version-id'];
  935. delete data1.headers['last-modified'];
  936. delete data2.headers['last-modified'];
  937. delete data1.headers['date'];
  938. delete data2.headers['date'];
  939. assert.ok(comparePlainObject(data1, data2));
  940. done();
  941. });
  942. }, 2000);
  943. });
  944. });
  945. }).catch(function () {
  946. assert.ok(false);
  947. done();
  948. });
  949. }, 2000);
  950. });
  951. });
  952. group('deleteMultipleObject', function () {
  953. test('deleteMultipleObject()', function (done, assert) {
  954. var content = Date.now().toString(36);
  955. cos.putObject({
  956. Bucket: config.Bucket,
  957. Region: config.Region,
  958. Key: '1.txt',
  959. Body: content,
  960. }, function (err, data) {
  961. cos.putObject({
  962. Bucket: config.Bucket,
  963. Region: config.Region,
  964. Key: '2.txt',
  965. Body: content,
  966. }, function (err, data) {
  967. cos.deleteMultipleObject({
  968. Bucket: config.Bucket,
  969. Region: config.Region,
  970. Objects: [
  971. {Key: '1.txt'},
  972. {Key: '2.txt'}
  973. ],
  974. }, function (err, data) {
  975. assert.ok(data.Deleted.length === 2);
  976. cos.headObject({
  977. Bucket: config.Bucket,
  978. Region: config.Region,
  979. Key: '1.txt',
  980. }, function (err, data) {
  981. assert.ok(err.statusCode === 404, '1.txt 删除成功');
  982. cos.headObject({
  983. Bucket: config.Bucket,
  984. Region: config.Region,
  985. Key: '2.txt',
  986. }, function (err, data) {
  987. assert.ok(err.statusCode === 404, '2.txt 删除成功');
  988. done();
  989. });
  990. });
  991. });
  992. });
  993. });
  994. });
  995. });
  996. group('BucketAcl', function () {
  997. var AccessControlPolicy = {
  998. "Owner": {
  999. "ID": 'qcs::cam::uin/10001:uin/10001' // 10001 是 QQ 号
  1000. },
  1001. "Grants": [{
  1002. "Grantee": {
  1003. "ID": "qcs::cam::uin/10002:uin/10002", // 10002 是 QQ 号
  1004. },
  1005. "Permission": "READ"
  1006. }]
  1007. };
  1008. var AccessControlPolicy2 = {
  1009. "Owner": {
  1010. "ID": 'qcs::cam::uin/10001:uin/10001' // 10001 是 QQ 号
  1011. },
  1012. "Grant": {
  1013. "Grantee": {
  1014. "ID": "qcs::cam::uin/10002:uin/10002", // 10002 是 QQ 号
  1015. },
  1016. "Permission": "READ"
  1017. }
  1018. };
  1019. test('putBucketAcl() header ACL:private', function (done, assert) {
  1020. cos.putBucketAcl({
  1021. Bucket: config.Bucket,
  1022. Region: config.Region,
  1023. ACL: 'private'
  1024. }, function (err, data) {
  1025. assert.ok(!err, 'putBucketAcl 成功');
  1026. cos.getBucketAcl({
  1027. Bucket: config.Bucket,
  1028. Region: config.Region
  1029. }, function (err, data) {
  1030. AccessControlPolicy.Owner.ID = data.Owner.ID;
  1031. AccessControlPolicy2.Owner.ID = data.Owner.ID;
  1032. assert.ok(data.ACL === 'private' || data.ACL === 'default');
  1033. done();
  1034. });
  1035. });
  1036. });
  1037. test('putBucketAcl() header ACL:public-read', function (done, assert) {
  1038. cos.putBucketAcl({
  1039. Bucket: config.Bucket,
  1040. Region: config.Region,
  1041. ACL: 'public-read',
  1042. }, function (err, data) {
  1043. assert.ok(!err, 'putBucketAcl 成功');
  1044. cos.getBucketAcl({Bucket: config.Bucket, Region: config.Region}, function (err, data) {
  1045. assert.ok(data.ACL === 'public-read');
  1046. done();
  1047. });
  1048. });
  1049. });
  1050. test('putBucketAcl() header ACL:public-read-write', function (done, assert) {
  1051. cos.putBucketAcl({
  1052. Bucket: config.Bucket,
  1053. Region: config.Region,
  1054. ACL: 'public-read-write',
  1055. }, function (err, data) {
  1056. assert.ok(!err, 'putBucketAcl 成功');
  1057. cos.getBucketAcl({Bucket: config.Bucket, Region: config.Region}, function (err, data) {
  1058. assert.ok(data.ACL === 'public-read-write');
  1059. done();
  1060. });
  1061. });
  1062. });
  1063. test('putBucketAcl() header GrantRead:1001,1002', function (done, assert) {
  1064. var GrantRead = 'id="qcs::cam::uin/1001:uin/1001", id="qcs::cam::uin/1002:uin/1002"';
  1065. cos.putBucketAcl({
  1066. Bucket: config.Bucket,
  1067. Region: config.Region,
  1068. GrantRead: GrantRead,
  1069. }, function (err, data) {
  1070. assert.ok(!err, 'putBucketAcl 成功');
  1071. cos.getBucketAcl({Bucket: config.Bucket, Region: config.Region}, function (err, data) {
  1072. assert.ok(data.GrantRead = GrantRead);
  1073. done();
  1074. });
  1075. });
  1076. });
  1077. test('putBucketAcl() header GrantWrite:1001,1002', function (done, assert) {
  1078. var GrantWrite = 'id="qcs::cam::uin/1001:uin/1001", id="qcs::cam::uin/1002:uin/1002"';
  1079. cos.putBucketAcl({
  1080. Bucket: config.Bucket,
  1081. Region: config.Region,
  1082. GrantWrite: GrantWrite,
  1083. }, function (err, data) {
  1084. assert.ok(!err, 'putBucketAcl 成功');
  1085. cos.getBucketAcl({Bucket: config.Bucket, Region: config.Region}, function (err, data) {
  1086. assert.ok(data.GrantWrite = GrantWrite);
  1087. done();
  1088. });
  1089. });
  1090. });
  1091. test('putBucketAcl() header GrantFullControl:1001,1002', function (done, assert) {
  1092. var GrantFullControl = 'id="qcs::cam::uin/1001:uin/1001", id="qcs::cam::uin/1002:uin/1002"';
  1093. cos.putBucketAcl({
  1094. Bucket: config.Bucket,
  1095. Region: config.Region,
  1096. GrantFullControl: GrantFullControl,
  1097. }, function (err, data) {
  1098. assert.ok(!err, 'putBucketAcl 成功');
  1099. cos.getBucketAcl({Bucket: config.Bucket, Region: config.Region}, function (err, data) {
  1100. assert.ok(data.GrantFullControl = GrantFullControl);
  1101. done();
  1102. });
  1103. });
  1104. });
  1105. test('putBucketAcl() header ACL:public-read, GrantFullControl:1001,1002', function (done, assert) {
  1106. var GrantFullControl = 'id="qcs::cam::uin/1001:uin/1001", id="qcs::cam::uin/1002:uin/1002"';
  1107. cos.putBucketAcl({
  1108. Bucket: config.Bucket,
  1109. Region: config.Region,
  1110. GrantFullControl: GrantFullControl,
  1111. ACL: 'public-read',
  1112. }, function (err, data) {
  1113. assert.ok(!err, 'putBucketAcl 成功');
  1114. cos.getBucketAcl({Bucket: config.Bucket, Region: config.Region}, function (err, data) {
  1115. assert.ok(data.GrantFullControl = GrantFullControl);
  1116. assert.ok(data.ACL === 'public-read');
  1117. done();
  1118. });
  1119. });
  1120. });
  1121. test('putBucketAcl() xml', function (done, assert) {
  1122. cos.putBucketAcl({
  1123. Bucket: config.Bucket,
  1124. Region: config.Region,
  1125. AccessControlPolicy: AccessControlPolicy
  1126. }, function (err, data) {
  1127. assert.ok(!err, 'putBucketAcl 成功');
  1128. cos.getBucketAcl({Bucket: config.Bucket, Region: config.Region}, function (err, data) {
  1129. assert.ok(data.Grants.length === 1);
  1130. assert.ok(data.Grants[0] && data.Grants[0].Grantee.ID === 'qcs::cam::uin/10002:uin/10002', '设置 AccessControlPolicy ID 正确');
  1131. assert.ok(data.Grants[0] && data.Grants[0].Permission === 'READ', '设置 AccessControlPolicy Permission 正确');
  1132. done();
  1133. });
  1134. });
  1135. });
  1136. test('putBucketAcl() xml2', function (done, assert) {
  1137. cos.putBucketAcl({
  1138. Bucket: config.Bucket,
  1139. Region: config.Region,
  1140. AccessControlPolicy: AccessControlPolicy2,
  1141. }, function (err, data) {
  1142. assert.ok(!err, 'putBucketAcl 成功');
  1143. cos.getBucketAcl({Bucket: config.Bucket, Region: config.Region}, function (err, data) {
  1144. assert.ok(data.Grants.length === 1);
  1145. assert.ok(data.Grants[0] && data.Grants[0].Grantee.ID === 'qcs::cam::uin/10002:uin/10002');
  1146. assert.ok(data.Grants[0] && data.Grants[0].Permission === 'READ');
  1147. done();
  1148. });
  1149. });
  1150. });
  1151. test('putBucketAcl() decodeAcl', function (done, assert) {
  1152. cos.getBucketAcl({
  1153. Bucket: config.Bucket,
  1154. Region: config.Region
  1155. }, function (err, data) {
  1156. cos.putBucketAcl({
  1157. Bucket: config.Bucket,
  1158. Region: config.Region,
  1159. GrantFullControl: data.GrantFullControl,
  1160. GrantWrite: data.GrantWrite,
  1161. GrantRead: data.GrantRead,
  1162. ACL: data.ACL,
  1163. }, function (err, data) {
  1164. assert.ok(data);
  1165. done();
  1166. });
  1167. });
  1168. });
  1169. });
  1170. group('ObjectAcl', function () {
  1171. var AccessControlPolicy = {
  1172. "Owner": {
  1173. "ID": 'qcs::cam::uin/10001:uin/10001' // 10001 是 QQ 号
  1174. },
  1175. "Grants": [{
  1176. "Grantee": {
  1177. "ID": "qcs::cam::uin/10002:uin/10002", // 10002 是 QQ 号
  1178. },
  1179. "Permission": "READ"
  1180. }]
  1181. };
  1182. var AccessControlPolicy2 = {
  1183. "Owner": {
  1184. "ID": 'qcs::cam::uin/10001:uin/10001' // 10001 是 QQ 号
  1185. },
  1186. "Grant": {
  1187. "Grantee": {
  1188. "ID": "qcs::cam::uin/10002:uin/10002", // 10002 是 QQ 号
  1189. },
  1190. "Permission": "READ"
  1191. }
  1192. };
  1193. test('putObjectAcl() header ACL:private', function (done, assert) {
  1194. cos.putObject({
  1195. Bucket: config.Bucket,
  1196. Region: config.Region,
  1197. Key: '1.txt',
  1198. Body: 'hello!',
  1199. }, function (err, data) {
  1200. assert.ok(!err);
  1201. cos.putObjectAcl({
  1202. Bucket: config.Bucket,
  1203. Region: config.Region,
  1204. ACL: 'private',
  1205. Key: '1.txt',
  1206. }, function (err, data) {
  1207. assert.ok(!err, 'putObjectAcl 成功');
  1208. cos.getObjectAcl({
  1209. Bucket: config.Bucket,
  1210. Region: config.Region,
  1211. Key: '1.txt'
  1212. }, function (err, data) {
  1213. assert.ok(data.ACL = 'private');
  1214. AccessControlPolicy.Owner.ID = data.Owner.ID;
  1215. AccessControlPolicy2.Owner.ID = data.Owner.ID;
  1216. assert.ok(data.Grants.length === 1);
  1217. done();
  1218. });
  1219. });
  1220. });
  1221. });
  1222. test('putObjectAcl() header ACL:default', function (done, assert) {
  1223. cos.putObjectAcl({
  1224. Bucket: config.Bucket,
  1225. Region: config.Region,
  1226. ACL: 'default',
  1227. Key: '1.txt',
  1228. }, function (err, data) {
  1229. assert.ok(!err, 'putObjectAcl 成功');
  1230. cos.getObjectAcl({
  1231. Bucket: config.Bucket,
  1232. Region: config.Region,
  1233. Key: '1.txt'
  1234. }, function (err, data) {
  1235. assert.ok(data.ACL = 'default');
  1236. done();
  1237. });
  1238. });
  1239. });
  1240. test('putObjectAcl() header ACL:public-read', function (done, assert) {
  1241. cos.putObjectAcl({
  1242. Bucket: config.Bucket,
  1243. Region: config.Region,
  1244. ACL: 'public-read',
  1245. Key: '1.txt',
  1246. }, function (err, data) {
  1247. assert.ok(!err, 'putObjectAcl 成功');
  1248. cos.getObjectAcl({Bucket: config.Bucket, Region: config.Region, Key: '1.txt'}, function (err, data) {
  1249. assert.ok(data.ACL = 'public-read');
  1250. done();
  1251. });
  1252. });
  1253. });
  1254. // Object 不再支持修改写权限
  1255. // test('putObjectAcl() header ACL:public-read-write', function (done, assert) {
  1256. // cos.putObjectAcl({
  1257. // Bucket: config.Bucket,
  1258. // Region: config.Region,
  1259. // ACL: 'public-read-write',
  1260. // Key: '1.txt',
  1261. // }, function (err, data) {
  1262. // assert.ok(!err, 'putObjectAcl 成功');
  1263. // cos.getObjectAcl({Bucket: config.Bucket, Region: config.Region, Key: '1.txt'}, function (err, data) {
  1264. // assert.ok(data.ACL = 'public-read-write');
  1265. // done();
  1266. // });
  1267. // });
  1268. // });
  1269. test('putObjectAcl() header GrantRead:1001,1002', function (done, assert) {
  1270. var GrantRead = 'id="qcs::cam::uin/1001:uin/1001",id="qcs::cam::uin/1002:uin/1002"';
  1271. cos.putObjectAcl({
  1272. Bucket: config.Bucket,
  1273. Region: config.Region,
  1274. GrantRead: GrantRead,
  1275. Key: '1.txt',
  1276. }, function (err, data) {
  1277. assert.ok(!err, 'putObjectAcl 成功');
  1278. cos.getObjectAcl({Bucket: config.Bucket, Region: config.Region, Key: '1.txt'}, function (err, data) {
  1279. assert.ok(data.GrantRead = GrantRead);
  1280. done();
  1281. });
  1282. });
  1283. });
  1284. // Object 不再支持修改写权限
  1285. // test('putObjectAcl() header GrantWrite:1001,1002', function (done, assert) {
  1286. // var GrantWrite = 'id="qcs::cam::uin/1001:uin/1001", id="qcs::cam::uin/1002:uin/1002"';
  1287. // cos.putObjectAcl({
  1288. // Bucket: config.Bucket,
  1289. // Region: config.Region,
  1290. // GrantWrite: GrantWrite,
  1291. // Key: '1.txt',
  1292. // }, function (err, data) {
  1293. // assert.ok(!err, 'putObjectAcl 成功');
  1294. // cos.getObjectAcl({Bucket: config.Bucket, Region: config.Region, Key: '1.txt'}, function (err, data) {
  1295. // assert.ok(data.GrantWrite = GrantWrite);
  1296. // done();
  1297. // });
  1298. // });
  1299. // });
  1300. test('putObjectAcl() header GrantFullControl:1001,1002', function (done, assert) {
  1301. var GrantFullControl = 'id="qcs::cam::uin/1001:uin/1001", id="qcs::cam::uin/1002:uin/1002"';
  1302. cos.putObjectAcl({
  1303. Bucket: config.Bucket,
  1304. Region: config.Region,
  1305. GrantFullControl: GrantFullControl,
  1306. Key: '1.txt',
  1307. }, function (err, data) {
  1308. assert.ok(!err, 'putObjectAcl 成功');
  1309. cos.getObjectAcl({Bucket: config.Bucket, Region: config.Region, Key: '1.txt'}, function (err, data) {
  1310. assert.ok(data.GrantFullControl = GrantFullControl);
  1311. done();
  1312. });
  1313. });
  1314. });
  1315. test('putObjectAcl() header ACL:public-read, GrantRead:1001,1002', function (done, assert) {
  1316. var GrantFullControl = 'id="qcs::cam::uin/1001:uin/1001", id="qcs::cam::uin/1002:uin/1002"';
  1317. cos.putObjectAcl({
  1318. Bucket: config.Bucket,
  1319. Region: config.Region,
  1320. GrantFullControl: GrantFullControl,
  1321. ACL: 'public-read',
  1322. Key: '1.txt',
  1323. }, function (err, data) {
  1324. assert.ok(!err, 'putObjectAcl 成功');
  1325. cos.getObjectAcl({Bucket: config.Bucket, Region: config.Region, Key: '1.txt'}, function (err, data) {
  1326. assert.ok(data.GrantFullControl = GrantFullControl);
  1327. assert.ok(data.ACL = 'public-read');
  1328. done();
  1329. });
  1330. });
  1331. });
  1332. test('putObjectAcl() xml', function (done, assert) {
  1333. cos.putObjectAcl({
  1334. Bucket: config.Bucket,
  1335. Region: config.Region,
  1336. AccessControlPolicy: AccessControlPolicy,
  1337. Key: '1.txt',
  1338. }, function (err, data) {
  1339. assert.ok(!err, 'putObjectAcl 成功');
  1340. cos.getBucketAcl({Bucket: config.Bucket, Region: config.Region, Key: '1.txt'}, function (err, data) {
  1341. assert.ok(data.Grants.length === 1);
  1342. assert.ok(data.Grants[0] && data.Grants[0].Grantee.ID === 'qcs::cam::uin/10002:uin/10002', '设置 AccessControlPolicy ID 正确');
  1343. assert.ok(data.Grants[0] && data.Grants[0].Permission === 'READ', '设置 AccessControlPolicy Permission 正确');
  1344. done();
  1345. });
  1346. });
  1347. });
  1348. test('putObjectAcl() xml2', function (done, assert) {
  1349. cos.putObjectAcl({
  1350. Bucket: config.Bucket,
  1351. Region: config.Region,
  1352. AccessControlPolicy: AccessControlPolicy2,
  1353. Key: '1.txt',
  1354. }, function (err, data) {
  1355. assert.ok(!err, 'putObjectAcl 成功');
  1356. cos.getObjectAcl({
  1357. Bucket: config.Bucket,
  1358. Region: config.Region,
  1359. Key: '1.txt'
  1360. }, function (err, data) {
  1361. assert.ok(data.Grants.length === 1);
  1362. assert.ok(data.Grants[0] && data.Grants[0].Grantee.ID === 'qcs::cam::uin/10002:uin/10002', 'ID 正确');
  1363. assert.ok(data.Grants[0] && data.Grants[0].Permission === 'READ', 'Permission 正确');
  1364. done();
  1365. });
  1366. });
  1367. });
  1368. test('putObjectAcl() decodeAcl', function (done, assert) {
  1369. cos.getObjectAcl({
  1370. Bucket: config.Bucket,
  1371. Region: config.Region,
  1372. Key: '1.txt'
  1373. }, function (err, data) {
  1374. cos.putObjectAcl({
  1375. Bucket: config.Bucket,
  1376. Region: config.Region,
  1377. Key: '1.txt',
  1378. GrantFullControl: data.GrantFullControl,
  1379. GrantWrite: data.GrantWrite,
  1380. GrantRead: data.GrantRead,
  1381. ACL: data.ACL,
  1382. }, function (err, data) {
  1383. assert.ok(data);
  1384. done();
  1385. });
  1386. });
  1387. });
  1388. });
  1389. group('BucketCors', function () {
  1390. var CORSRules = [{
  1391. "AllowedOrigins": ["*"],
  1392. "AllowedMethods": ["GET", "POST", "PUT", "DELETE", "HEAD"],
  1393. "AllowedHeaders": ["*", 'test-' + Date.now().toString(36)],
  1394. "ExposeHeaders": [
  1395. 'etag',
  1396. 'date',
  1397. 'content-length',
  1398. 'expires',
  1399. 'cache-control',
  1400. 'content-disposition',
  1401. 'content-encoding',
  1402. 'x-cos-acl',
  1403. 'x-cos-version-id',
  1404. 'x-cos-request-id',
  1405. 'x-cos-delete-marker',
  1406. 'x-cos-server-side-encryption',
  1407. 'x-cos-storage-class',
  1408. 'x-cos-acl',
  1409. 'x-cos-meta-test',
  1410. ],
  1411. "MaxAgeSeconds": "5"
  1412. }];
  1413. var CORSRulesMulti = [{
  1414. "AllowedOrigins": ["*"],
  1415. "AllowedMethods": ["GET", "POST", "PUT", "DELETE", "HEAD"],
  1416. "AllowedHeaders": ["*"],
  1417. "ExposeHeaders": ["ETag", "Date", "Content-Length", "x-cos-acl", "x-cos-version-id", "x-cos-request-id", "x-cos-delete-marker", "x-cos-server-side-encryption"],
  1418. "MaxAgeSeconds": "5"
  1419. }, {
  1420. "AllowedOrigins": ["http://qq.com", "http://qcloud.com"],
  1421. "AllowedMethods": ["GET", "POST", "PUT", "DELETE", "HEAD"],
  1422. "AllowedHeaders": ["*"],
  1423. "ExposeHeaders": ["ETag", "Date", "Content-Length", "x-cos-acl", "x-cos-version-id", "x-cos-request-id", "x-cos-delete-marker", "x-cos-server-side-encryption"],
  1424. "MaxAgeSeconds": "5"
  1425. }];
  1426. test('putBucketCors() old', function (done, assert) {
  1427. CORSRules[0].AllowedHeaders[1] = 'test-' + Date.now().toString(36);
  1428. cos.putBucketCors({
  1429. Bucket: config.Bucket,
  1430. Region: config.Region,
  1431. CORSConfiguration: {
  1432. CORSRules: CORSRules
  1433. }
  1434. }, function (err, data) {
  1435. assert.ok(!err);
  1436. setTimeout(function () {
  1437. cos.getBucketCors({
  1438. Bucket: config.Bucket,
  1439. Region: config.Region
  1440. }, function (err, data) {
  1441. assert.ok(comparePlainObject(CORSRules, data.CORSRules));
  1442. done();
  1443. });
  1444. }, 2000);
  1445. });
  1446. });
  1447. test('putBucketCors() multi', function (done, assert) {
  1448. cos.putBucketCors({
  1449. Bucket: config.Bucket,
  1450. Region: config.Region,
  1451. CORSConfiguration: {
  1452. CORSRules: CORSRulesMulti
  1453. }
  1454. }, function (err, data) {
  1455. assert.ok(!err);
  1456. setTimeout(function () {
  1457. cos.getBucketCors({
  1458. Bucket: config.Bucket,
  1459. Region: config.Region
  1460. }, function (err, data) {
  1461. assert.ok(comparePlainObject(CORSRulesMulti, data.CORSRules));
  1462. done();
  1463. });
  1464. }, 2000);
  1465. });
  1466. });
  1467. test('putBucketCors() old', function (done, assert) {
  1468. CORSRules[0].AllowedHeaders[1] = 'test-' + Date.now().toString(36);
  1469. cos.putBucketCors({
  1470. Bucket: config.Bucket,
  1471. Region: config.Region,
  1472. CORSRules: CORSRules
  1473. }, function (err, data) {
  1474. assert.ok(!err);
  1475. setTimeout(function () {
  1476. cos.getBucketCors({
  1477. Bucket: config.Bucket,
  1478. Region: config.Region
  1479. }, function (err, data) {
  1480. assert.ok(comparePlainObject(CORSRules, data.CORSRules));
  1481. done();
  1482. });
  1483. }, 2000);
  1484. });
  1485. });
  1486. test('putBucketCors(),getBucketCors()', function (done, assert) {
  1487. CORSRules[0].AllowedHeaders = ['*'];
  1488. cos.putBucketCors({
  1489. Bucket: config.Bucket,
  1490. Region: config.Region,
  1491. CORSConfiguration: {
  1492. CORSRules: CORSRules
  1493. }
  1494. }, function (err, data) {
  1495. assert.ok(!err);
  1496. setTimeout(function () {
  1497. cos.getBucketCors({
  1498. Bucket: config.Bucket,
  1499. Region: config.Region
  1500. }, function (err, data) {
  1501. assert.ok(comparePlainObject(CORSRules, data.CORSRules));
  1502. done();
  1503. });
  1504. }, 2000);
  1505. });
  1506. });
  1507. });
  1508. group('BucketTagging', function () {
  1509. var Tags = [
  1510. {Key: "k1", Value: "v1"}
  1511. ];
  1512. var TagsMulti = [
  1513. {Key: "k1", Value: "v1"},
  1514. {Key: "k2", Value: "v2"},
  1515. ];
  1516. test('putBucketTagging(),getBucketTagging()', function (done, assert) {
  1517. Tags[0].Value = Date.now().toString(36);
  1518. cos.putBucketTagging({
  1519. Bucket: config.Bucket,
  1520. Region: config.Region,
  1521. Tagging: {
  1522. Tags: Tags
  1523. }
  1524. }, function (err, data) {
  1525. assert.ok(!err);
  1526. setTimeout(function () {
  1527. cos.getBucketTagging({
  1528. Bucket: config.Bucket,
  1529. Region: config.Region
  1530. }, function (err, data) {
  1531. assert.ok(comparePlainObject(Tags, data.Tags));
  1532. done();
  1533. });
  1534. }, 1000);
  1535. });
  1536. });
  1537. test('deleteBucketTagging()', function (done, assert) {
  1538. cos.deleteBucketTagging({
  1539. Bucket: config.Bucket,
  1540. Region: config.Region
  1541. }, function (err, data) {
  1542. assert.ok(!err);
  1543. setTimeout(function () {
  1544. cos.getBucketTagging({
  1545. Bucket: config.Bucket,
  1546. Region: config.Region
  1547. }, function (err, data) {
  1548. assert.ok(comparePlainObject([], data.Tags));
  1549. done();
  1550. });
  1551. }, 1000);
  1552. });
  1553. });
  1554. test('putBucketTagging() multi', function (done, assert) {
  1555. Tags[0].Value = Date.now().toString(36);
  1556. cos.putBucketTagging({
  1557. Bucket: config.Bucket,
  1558. Region: config.Region,
  1559. Tagging: {
  1560. Tags: TagsMulti
  1561. }
  1562. }, function (err, data) {
  1563. assert.ok(!err);
  1564. setTimeout(function () {
  1565. cos.getBucketTagging({
  1566. Bucket: config.Bucket,
  1567. Region: config.Region
  1568. }, function (err, data) {
  1569. assert.ok(comparePlainObject(TagsMulti, data.Tags));
  1570. done();
  1571. });
  1572. }, 1000);
  1573. });
  1574. });
  1575. });
  1576. group('BucketPolicy', function () {
  1577. var Prefix = Date.now().toString(36);
  1578. var Policy = {
  1579. "version": "2.0",
  1580. "principal": {"qcs": ["qcs::cam::uin/10001:uin/10001"]}, // 这里的 10001 是 QQ 号
  1581. "statement": [{
  1582. "effect": "allow",
  1583. "action": [
  1584. "name/cos:GetBucket",
  1585. "name/cos:PutObject",
  1586. "name/cos:PostObject",
  1587. "name/cos:PutObjectCopy",
  1588. "name/cos:InitiateMultipartUpload",
  1589. "name/cos:UploadPart",
  1590. "name/cos:UploadPartCopy",
  1591. "name/cos:CompleteMultipartUpload",
  1592. "name/cos:AbortMultipartUpload",
  1593. "name/cos:AppendObject"
  1594. ],
  1595. "resource": ["qcs::cos:" + config.Region + ":uid/" + AppId + ":" + BucketLongName + ".cos." + config.Region + ".myqcloud.com//" + AppId + "/" + BucketShortName + "/" + Prefix + "/*"] // 1250000000 是 appid
  1596. }]
  1597. };
  1598. test('putBucketPolicy(),getBucketPolicy()', function (done, assert) {
  1599. cos.putBucketPolicy({
  1600. Bucket: config.Bucket,
  1601. Region: config.Region,
  1602. Policy: Policy
  1603. }, function (err, data) {
  1604. assert.ok(!err);
  1605. cos.getBucketPolicy({
  1606. Bucket: config.Bucket,
  1607. Region: config.Region
  1608. }, function (err, data) {
  1609. assert.ok(Policy, data.Policy);
  1610. done();
  1611. });
  1612. });
  1613. });
  1614. test('putBucketPolicy() s3', function (done, assert) {
  1615. cos.putBucketPolicy({
  1616. Bucket: config.Bucket,
  1617. Region: config.Region,
  1618. Policy: JSON.stringify(Policy)
  1619. }, function (err, data) {
  1620. assert.ok(!err);
  1621. cos.getBucketPolicy({
  1622. Bucket: config.Bucket,
  1623. Region: config.Region
  1624. }, function (err, data) {
  1625. assert.ok(Policy, data.Policy);
  1626. done();
  1627. });
  1628. });
  1629. });
  1630. });
  1631. group('BucketLocation', function () {
  1632. test('getBucketLocation()', function (done, assert) {
  1633. cos.getBucketLocation({
  1634. Bucket: config.Bucket,
  1635. Region: config.Region
  1636. }, function (err, data) {
  1637. var map1 = {
  1638. 'tianjin': 'ap-beijing-1',
  1639. 'cn-south-2': 'ap-guangzhou-2',
  1640. 'cn-south': 'ap-guangzhou',
  1641. 'cn-east': 'ap-shanghai',
  1642. 'cn-southwest': 'ap-chengdu',
  1643. };
  1644. var map2 = {
  1645. 'ap-beijing-1': 'tianjin',
  1646. 'ap-guangzhou-2': 'cn-south-2',
  1647. 'ap-guangzhou': 'cn-south',
  1648. 'ap-shanghai': 'cn-east',
  1649. 'ap-chengdu': 'cn-southwest',
  1650. };
  1651. assert.ok(data.LocationConstraint === config.Region || data.LocationConstraint === map1[config.Region] ||
  1652. data.LocationConstraint === map2[config.Region]);
  1653. done();
  1654. });
  1655. });
  1656. });
  1657. group('BucketLifecycle', function () {
  1658. var Rules = [{
  1659. 'ID': '1',
  1660. 'Filter': {
  1661. 'Prefix': 'test_' + Date.now().toString(36),
  1662. },
  1663. 'Status': 'Enabled',
  1664. 'Transition': {
  1665. 'Date': '2018-07-29T16:00:00.000Z',
  1666. 'StorageClass': 'STANDARD_IA'
  1667. }
  1668. }];
  1669. var RulesMulti = [{
  1670. 'ID': '1',
  1671. 'Filter': {
  1672. 'Prefix': 'test1_' + Date.now().toString(36),
  1673. },
  1674. 'Status': 'Enabled',
  1675. 'Transition': {
  1676. 'Date': '2018-07-29T16:00:00.000Z',
  1677. 'StorageClass': 'STANDARD_IA'
  1678. }
  1679. }, {
  1680. 'ID': '2',
  1681. 'Filter': {
  1682. 'Prefix': 'test2_' + Date.now().toString(36),
  1683. },
  1684. 'Status': 'Enabled',
  1685. 'Transition': {
  1686. 'Date': '2018-07-29T16:00:00.000Z',
  1687. 'StorageClass': 'STANDARD_IA'
  1688. }
  1689. }];
  1690. test('deleteBucketLifecycle()', function (done, assert) {
  1691. cos.deleteBucketLifecycle({
  1692. Bucket: config.Bucket,
  1693. Region: config.Region
  1694. }, function (err, data) {
  1695. assert.ok(!err);
  1696. setTimeout(function () {
  1697. cos.getBucketLifecycle({
  1698. Bucket: config.Bucket,
  1699. Region: config.Region
  1700. }, function (err, data) {
  1701. assert.ok(comparePlainObject([], data.Rules));
  1702. done();
  1703. });
  1704. }, 2000);
  1705. });
  1706. });
  1707. test('putBucketLifecycle(),getBucketLifecycle()', function (done, assert) {
  1708. Rules[0].Filter.Prefix = 'test_' + Date.now().toString(36);
  1709. cos.putBucketLifecycle({
  1710. Bucket: config.Bucket,
  1711. Region: config.Region,
  1712. LifecycleConfiguration: {
  1713. Rules: Rules
  1714. }
  1715. }, function (err, data) {
  1716. assert.ok(!err);
  1717. setTimeout(function () {
  1718. cos.getBucketLifecycle({
  1719. Bucket: config.Bucket,
  1720. Region: config.Region
  1721. }, function (err, data) {
  1722. assert.ok(comparePlainObject(Rules, data && data.Rules));
  1723. done();
  1724. });
  1725. }, 2000);
  1726. });
  1727. });
  1728. test('putBucketLifecycle() multi', function (done, assert) {
  1729. Rules[0].Filter.Prefix = 'test_' + Date.now().toString(36);
  1730. cos.putBucketLifecycle({
  1731. Bucket: config.Bucket,
  1732. Region: config.Region,
  1733. LifecycleConfiguration: {
  1734. Rules: RulesMulti
  1735. }
  1736. }, function (err, data) {
  1737. assert.ok(!err);
  1738. setTimeout(function () {
  1739. cos.getBucketLifecycle({
  1740. Bucket: config.Bucket,
  1741. Region: config.Region
  1742. }, function (err, data) {
  1743. assert.ok(comparePlainObject(RulesMulti, data.Rules));
  1744. done();
  1745. });
  1746. }, 2000);
  1747. });
  1748. });
  1749. });
  1750. group('params check Region', function () {
  1751. test('params check', function (done, assert) {
  1752. cos.headBucket({
  1753. Bucket: config.Bucket,
  1754. Region: 'cos.ap-guangzhou'
  1755. }, function (err, data) {
  1756. assert.ok(err.error === 'param Region should not be start with "cos."');
  1757. done();
  1758. });
  1759. });
  1760. test('params check Region', function (done, assert) {
  1761. cos.headBucket({
  1762. Bucket: config.Bucket,
  1763. Region: 'gz'
  1764. }, function (err, data) {
  1765. assert.ok(err);
  1766. done();
  1767. });
  1768. });
  1769. });
  1770. group('Key 特殊字符处理', function () {
  1771. test('Key 特殊字符处理', function (done, assert) {
  1772. var Key = '中文→↓←→↖↗↙↘! $&\'()+,-.0123456789=@ABCDEFGHIJKLMNOPQRSTUV?WXYZ[]^_`abcdefghijklmnopqrstuvwxyz{}~.jpg';
  1773. cos.putObject({
  1774. Bucket: config.Bucket,
  1775. Region: config.Region,
  1776. Key: Key,
  1777. Body: 'hello',
  1778. }, function (err, data) {
  1779. assert.ok(!err);
  1780. cos.deleteObject({
  1781. Bucket: config.Bucket,
  1782. Region: config.Region,
  1783. Key: Key,
  1784. Body: 'hello',
  1785. }, function (err, data) {
  1786. assert.ok(!err);
  1787. cos.deleteMultipleObject({
  1788. Bucket: config.Bucket,
  1789. Region: config.Region,
  1790. Objects: {
  1791. Key: Key,
  1792. },
  1793. }, function (err, data) {
  1794. assert.ok(!err);
  1795. done();
  1796. });
  1797. });
  1798. });
  1799. });
  1800. });
  1801. group('Bucket 格式有误', function () {
  1802. test('Bucket 带有中文', function (done, assert) {
  1803. cos.headBucket({
  1804. Bucket: '中文-1250000000',
  1805. Region: config.Region,
  1806. }, function (err, data) {
  1807. assert.ok(err && err.error === 'Bucket should format as "test-1250000000".');
  1808. done();
  1809. });
  1810. });
  1811. test('Bucket 带有 /', function (done, assert) {
  1812. cos.headBucket({
  1813. Bucket: 'te/st-1250000000',
  1814. Region: config.Region,
  1815. }, function (err, data) {
  1816. assert.ok(err && err.error === 'Bucket should format as "test-1250000000".');
  1817. done();
  1818. });
  1819. });
  1820. test('Bucket 带有 .', function (done, assert) {
  1821. cos.headBucket({
  1822. Bucket: 'te.st-1250000000',
  1823. Region: config.Region,
  1824. }, function (err, data) {
  1825. assert.ok(err && err.error === 'Bucket should format as "test-1250000000".');
  1826. done();
  1827. });
  1828. });
  1829. test('Bucket 带有 :', function (done, assert) {
  1830. cos.headBucket({
  1831. Bucket: 'te:st-1250000000',
  1832. Region: config.Region,
  1833. }, function (err, data) {
  1834. assert.ok(err && err.error === 'Bucket should format as "test-1250000000".');
  1835. done();
  1836. });
  1837. });
  1838. });
  1839. group('Region 格式有误', function () {
  1840. test('Region 带有中文', function (done, assert) {
  1841. cos.headBucket({
  1842. Bucket: 'test-1250000000',
  1843. Region: '中文',
  1844. }, function (err, data) {
  1845. assert.ok(err && err.error === 'Region format error.');
  1846. done();
  1847. });
  1848. });
  1849. test('Region 带有 /', function (done, assert) {
  1850. cos.headBucket({
  1851. Bucket: 'test-1250000000',
  1852. Region: 'test/',
  1853. }, function (err, data) {
  1854. assert.ok(err && err.error === 'Region format error.');
  1855. done();
  1856. });
  1857. });
  1858. test('Region 带有 :', function (done, assert) {
  1859. cos.headBucket({
  1860. Bucket: 'te:st-1250000000',
  1861. Region: 'test:',
  1862. }, function (err, data) {
  1863. assert.ok(err && err.error === 'Region format error.');
  1864. done();
  1865. });
  1866. });
  1867. });
  1868. group('复制文件', function () {
  1869. test('sliceCopyFile() 正常分片复制', function (done, assert) {
  1870. var filename = '10m.zip';
  1871. var Key = '10mb.copy.zip';
  1872. var blob = util.createFile({size: 1024 * 1024 * 10});
  1873. var lastPercent;
  1874. cos.putObject({
  1875. Bucket: config.Bucket,
  1876. Region: config.Region,
  1877. Key: filename,
  1878. Body: blob,
  1879. }, function (err, data) {
  1880. cos.sliceCopyFile({
  1881. Bucket: config.Bucket,
  1882. Region: config.Region,
  1883. Key: Key,
  1884. CopySource: config.Bucket + '.cos.' + config.Region + '.myqcloud.com/' + filename,
  1885. SliceSize: 5 * 1024 * 1024,
  1886. onProgress: function (info) {
  1887. lastPercent = info.percent;
  1888. }
  1889. }, function (err, data) {
  1890. assert.ok(data && data.ETag, '成功进行分片复制');
  1891. done();
  1892. });
  1893. });
  1894. });
  1895. test('sliceCopyFile() 单片复制', function (done, assert) {
  1896. var filename = '10m.zip';
  1897. var Key = '10mb.copy.zip';
  1898. cos.sliceCopyFile({
  1899. Bucket: config.Bucket,
  1900. Region: config.Region,
  1901. Key: Key,
  1902. CopySource: config.Bucket + '.cos.' + config.Region + '.myqcloud.com/' + filename,
  1903. SliceSize: 10 * 1024 * 1024,
  1904. }, function (err, data) {
  1905. if (err) throw err;
  1906. assert.ok(data && data.ETag, '成功进行单片复制');
  1907. done();
  1908. });
  1909. });
  1910. });
  1911. group('putObject 中文 Content-MD5', function () {
  1912. var fileBlob = dataURItoUploadBody('data:text/plain;base64,5Lit5paH');
  1913. // 这里两个用户正式测试的时候需要给 putObject 计算并加上 Content-MD5 字段
  1914. test('putObject 中文文件内容 带 Content-MD5', function (done, assert) {
  1915. var Key = '中文.txt';
  1916. cos.putObject({
  1917. Bucket: config.Bucket,
  1918. Region: config.Region,
  1919. Key: Key,
  1920. Body: fileBlob,
  1921. }, function (err, data) {
  1922. assert.ok(data && data.ETag, '成功进行上传');
  1923. done();
  1924. });
  1925. });
  1926. test('putObject 中文字符串 带 Content-MD5', function (done, assert) {
  1927. var Key = '中文.txt';
  1928. cos.putObject({
  1929. Bucket: config.Bucket,
  1930. Region: config.Region,
  1931. Key: Key,
  1932. Body: '中文',
  1933. }, function (err, data) {
  1934. assert.ok(data && data.ETag, '成功进行上传');
  1935. done();
  1936. });
  1937. });
  1938. });
  1939. group('deleteMultipleObject Key 带中文字符', function () {
  1940. test('deleteMultipleObject Key 带中文字符', function (done, assert) {
  1941. cos.deleteMultipleObject({
  1942. Bucket: config.Bucket,
  1943. Region: config.Region,
  1944. Objects: [
  1945. {Key: '中文/中文.txt'},
  1946. {Key: '中文/中文.zip', VersionId: 'MTg0NDY3NDI1MzM4NzM0ODA2MTI'},
  1947. ]
  1948. }, function (err, data) {
  1949. assert.ok(!err, '成功进行批量删除');
  1950. done();
  1951. });
  1952. });
  1953. });
  1954. group('upload Content-Type', function () {
  1955. // putObject
  1956. test('putObject empty string Content-Type null -> text/plain', function (done, assert) {
  1957. cos.putObject({
  1958. Bucket: config.Bucket,
  1959. Region: config.Region,
  1960. Key: '1',
  1961. Body: '',
  1962. }, function (err, data) {
  1963. cos.headObject({
  1964. Bucket: config.Bucket,
  1965. Region: config.Region,
  1966. Key: '1',
  1967. }, function (err, data) {
  1968. assert.ok(data.headers['content-type'] === 'text/plain', 'Content-Type 正确');
  1969. done();
  1970. });
  1971. });
  1972. });
  1973. test('putObject string Content-Type null -> text/plain', function (done, assert) {
  1974. cos.putObject({
  1975. Bucket: config.Bucket,
  1976. Region: config.Region,
  1977. Key: '1.zip',
  1978. Body: '12345',
  1979. }, function (err, data) {
  1980. cos.headObject({
  1981. Bucket: config.Bucket,
  1982. Region: config.Region,
  1983. Key: '1.zip',
  1984. }, function (err, data) {
  1985. assert.ok(data.headers['content-type'] === 'text/plain', 'Content-Type 正确');
  1986. done();
  1987. });
  1988. });
  1989. });
  1990. test('putObject string Content-Type text/xml -> text/xml', function (done, assert) {
  1991. cos.putObject({
  1992. Bucket: config.Bucket,
  1993. Region: config.Region,
  1994. Key: '1.zip',
  1995. ContentType: 'text/xml',
  1996. Body: util.createFile({size: 1, type: 'text/html'}),
  1997. }, function (err, data) {
  1998. cos.headObject({
  1999. Bucket: config.Bucket,
  2000. Region: config.Region,
  2001. Key: '1.zip',
  2002. }, function (err, data) {
  2003. assert.ok(data.headers['content-type'] === 'text/xml', 'Content-Type 正确');
  2004. done();
  2005. });
  2006. });
  2007. });
  2008. test('putObject blob Content-Type text/xml -> text/xml', function (done, assert) {
  2009. cos.putObject({
  2010. Bucket: config.Bucket,
  2011. Region: config.Region,
  2012. Key: '1.zip',
  2013. ContentType: 'text/xml',
  2014. Body: util.createFile({size: 1, type: 'text/html'}),
  2015. }, function (err, data) {
  2016. cos.headObject({
  2017. Bucket: config.Bucket,
  2018. Region: config.Region,
  2019. Key: '1.zip',
  2020. }, function (err, data) {
  2021. assert.ok(data.headers['content-type'] === 'text/xml', 'Content-Type 正确');
  2022. done();
  2023. });
  2024. });
  2025. });
  2026. test('putObject blob Content-Type text/html -> text/html', function (done, assert) {
  2027. cos.putObject({
  2028. Bucket: config.Bucket,
  2029. Region: config.Region,
  2030. Key: '1.zip',
  2031. Body: util.createFile({size: 1, type: 'text/html'}),
  2032. }, function (err, data) {
  2033. cos.headObject({
  2034. Bucket: config.Bucket,
  2035. Region: config.Region,
  2036. Key: '1.zip',
  2037. }, function (err, data) {
  2038. assert.ok(data.headers['content-type'] === 'text/html', 'Content-Type 正确');
  2039. done();
  2040. });
  2041. });
  2042. });
  2043. test('putObject blob Content-Type null -> application/zip or application/octet-stream', function (done, assert) {
  2044. cos.putObject({
  2045. Bucket: config.Bucket,
  2046. Region: config.Region,
  2047. Key: '1.zip',
  2048. Body: util.createFile({size: 1}),
  2049. }, function (err, data) {
  2050. cos.headObject({
  2051. Bucket: config.Bucket,
  2052. Region: config.Region,
  2053. Key: '1.zip',
  2054. }, function (err, data) {
  2055. assert.ok(data.headers['content-type'] === 'application/zip', 'Content-Type 正确');
  2056. done();
  2057. });
  2058. });
  2059. });
  2060. test('putObject blob Content-Type null application/octet-stream', function (done, assert) {
  2061. cos.putObject({
  2062. Bucket: config.Bucket,
  2063. Region: config.Region,
  2064. Key: '1',
  2065. Body: util.createFile({size: 1}),
  2066. }, function (err, data) {
  2067. cos.headObject({
  2068. Bucket: config.Bucket,
  2069. Region: config.Region,
  2070. Key: '1',
  2071. }, function (err, data) {
  2072. assert.ok(data.headers['content-type'] === 'application/octet-stream', 'Content-Type 正确');
  2073. done();
  2074. });
  2075. });
  2076. });
  2077. test('putObject empty blob Content-Type null application/octet-stream', function (done, assert) {
  2078. cos.putObject({
  2079. Bucket: config.Bucket,
  2080. Region: config.Region,
  2081. Key: '1',
  2082. Body: util.createFile({size: 0}),
  2083. }, function (err, data) {
  2084. cos.headObject({
  2085. Bucket: config.Bucket,
  2086. Region: config.Region,
  2087. Key: '1',
  2088. }, function (err, data) {
  2089. assert.ok(data.headers['content-type'] === 'application/octet-stream', 'Content-Type 正确');
  2090. done();
  2091. });
  2092. });
  2093. });
  2094. // sliceUploadFile
  2095. test('sliceUploadFile string Content-Type null -> text/plain', function (done, assert) {
  2096. cos.sliceUploadFile({
  2097. Bucket: config.Bucket,
  2098. Region: config.Region,
  2099. Key: '1.zip',
  2100. Body: '12345',
  2101. }, function (err, data) {
  2102. cos.headObject({
  2103. Bucket: config.Bucket,
  2104. Region: config.Region,
  2105. Key: '1.zip',
  2106. }, function (err, data) {
  2107. assert.ok(data.headers['content-type'] === 'text/plain', 'Content-Type 正确');
  2108. done();
  2109. });
  2110. });
  2111. });
  2112. test('sliceUploadFile string Content-Type text/xml -> text/xml', function (done, assert) {
  2113. cos.sliceUploadFile({
  2114. Bucket: config.Bucket,
  2115. Region: config.Region,
  2116. Key: '1.zip',
  2117. ContentType: 'text/xml',
  2118. Body: util.createFile({size: 1, type: 'text/html'}),
  2119. }, function (err, data) {
  2120. cos.headObject({
  2121. Bucket: config.Bucket,
  2122. Region: config.Region,
  2123. Key: '1.zip',
  2124. }, function (err, data) {
  2125. assert.ok(data.headers['content-type'] === 'text/xml', 'Content-Type 正确');
  2126. done();
  2127. });
  2128. });
  2129. });
  2130. test('sliceUploadFile blob Content-Type text/xml -> text/xml', function (done, assert) {
  2131. cos.sliceUploadFile({
  2132. Bucket: config.Bucket,
  2133. Region: config.Region,
  2134. Key: '1.zip',
  2135. ContentType: 'text/xml',
  2136. Body: util.createFile({size: 1, type: 'text/html'}),
  2137. }, function (err, data) {
  2138. cos.headObject({
  2139. Bucket: config.Bucket,
  2140. Region: config.Region,
  2141. Key: '1.zip',
  2142. }, function (err, data) {
  2143. assert.ok(data.headers['content-type'] === 'text/xml', 'Content-Type 正确');
  2144. done();
  2145. });
  2146. });
  2147. });
  2148. test('sliceUploadFile blob Content-Type text/html -> text/html', function (done, assert) {
  2149. cos.sliceUploadFile({
  2150. Bucket: config.Bucket,
  2151. Region: config.Region,
  2152. Key: '1.zip',
  2153. Body: util.createFile({size: 1, type: 'text/html'}),
  2154. }, function (err, data) {
  2155. cos.headObject({
  2156. Bucket: config.Bucket,
  2157. Region: config.Region,
  2158. Key: '1.zip',
  2159. }, function (err, data) {
  2160. assert.ok(data.headers['content-type'] === 'text/html', 'Content-Type 正确');
  2161. done();
  2162. });
  2163. });
  2164. });
  2165. test('sliceUploadFile blob Content-Type null -> application/zip or application/octet-stream', function (done, assert) {
  2166. cos.sliceUploadFile({
  2167. Bucket: config.Bucket,
  2168. Region: config.Region,
  2169. Key: '1.zip',
  2170. Body: util.createFile({size: 1}),
  2171. }, function (err, data) {
  2172. cos.headObject({
  2173. Bucket: config.Bucket,
  2174. Region: config.Region,
  2175. Key: '1.zip',
  2176. }, function (err, data) {
  2177. var userAgent = navigator.userAgent || '';
  2178. var m = userAgent.match(/ TBS\/(\d{6}) /);
  2179. if (location.protocol === 'http:' && m && m[1].length <= 6 && m[1] < '044429') {
  2180. assert.ok(data.headers['content-type'] === 'application/octet-stream', 'Content-Type 正确');
  2181. } else {
  2182. assert.ok(data.headers['content-type'] === 'application/zip', 'Content-Type 正确');
  2183. }
  2184. done();
  2185. });
  2186. });
  2187. });
  2188. test('sliceUploadFile blob Content-Type null application/octet-stream', function (done, assert) {
  2189. cos.sliceUploadFile({
  2190. Bucket: config.Bucket,
  2191. Region: config.Region,
  2192. Key: '1',
  2193. Body: util.createFile({size: 1}),
  2194. }, function (err, data) {
  2195. cos.headObject({
  2196. Bucket: config.Bucket,
  2197. Region: config.Region,
  2198. Key: '1',
  2199. }, function (err, data) {
  2200. assert.ok(data.headers['content-type'] === 'application/octet-stream', 'Content-Type 正确');
  2201. done();
  2202. });
  2203. });
  2204. });
  2205. });
  2206. group('Cache-Control', function () {
  2207. // putObject
  2208. test('putObject Cache-Control: null -> Cache-Control: null or max-age=259200', function (done, assert) {
  2209. cos.putObject({
  2210. Bucket: config.Bucket,
  2211. Region: config.Region,
  2212. Key: '1mb.zip',
  2213. Body: '',
  2214. }, function (err, data) {
  2215. cos.headObject({
  2216. Bucket: config.Bucket,
  2217. Region: config.Region,
  2218. Key: '1mb.zip',
  2219. }, function (err, data) {
  2220. assert.ok(data.headers['cache-control'] === undefined || data.headers['cache-control'] === 'max-age=259200', 'cache-control 正确');
  2221. done();
  2222. });
  2223. });
  2224. });
  2225. test('putObject Cache-Control: max-age=7200 -> Cache-Control: max-age=7200', function (done, assert) {
  2226. cos.putObject({
  2227. Bucket: config.Bucket,
  2228. Region: config.Region,
  2229. Key: '1mb.zip',
  2230. Body: '',
  2231. CacheControl: 'max-age=7200',
  2232. }, function (err, data) {
  2233. cos.headObject({
  2234. Bucket: config.Bucket,
  2235. Region: config.Region,
  2236. Key: '1mb.zip',
  2237. }, function (err, data) {
  2238. assert.ok(data.headers['cache-control'] === 'max-age=7200', 'cache-control 正确');
  2239. done();
  2240. });
  2241. });
  2242. });
  2243. test('putObject Cache-Control: no-cache -> Cache-Control: no-cache', function (done, assert) {
  2244. cos.putObject({
  2245. Bucket: config.Bucket,
  2246. Region: config.Region,
  2247. Key: '1mb.zip',
  2248. Body: '',
  2249. CacheControl: 'no-cache',
  2250. }, function (err, data) {
  2251. cos.headObject({
  2252. Bucket: config.Bucket,
  2253. Region: config.Region,
  2254. Key: '1mb.zip',
  2255. }, function (err, data) {
  2256. assert.ok(data.headers['cache-control'] === 'no-cache' || data.headers['cache-control'] === 'no-cache, max-age=259200', 'cache-control 正确');
  2257. done();
  2258. });
  2259. });
  2260. });
  2261. // sliceUploadFile
  2262. test('sliceUploadFile Cache-Control: null -> Cache-Control: null or max-age=259200', function (done, assert) {
  2263. cos.sliceUploadFile({
  2264. Bucket: config.Bucket,
  2265. Region: config.Region,
  2266. Key: '1mb.zip',
  2267. Body: '',
  2268. }, function (err, data) {
  2269. cos.headObject({
  2270. Bucket: config.Bucket,
  2271. Region: config.Region,
  2272. Key: '1mb.zip',
  2273. }, function (err, data) {
  2274. assert.ok(data.headers['cache-control'] === undefined || data.headers['cache-control'] === 'max-age=259200', 'cache-control 正确');
  2275. done();
  2276. });
  2277. });
  2278. });
  2279. test('sliceUploadFile Cache-Control: max-age=7200 -> Cache-Control: max-age=7200', function (done, assert) {
  2280. cos.sliceUploadFile({
  2281. Bucket: config.Bucket,
  2282. Region: config.Region,
  2283. Key: '1mb.zip',
  2284. Body: '',
  2285. CacheControl: 'max-age=7200',
  2286. }, function (err, data) {
  2287. cos.headObject({
  2288. Bucket: config.Bucket,
  2289. Region: config.Region,
  2290. Key: '1mb.zip',
  2291. }, function (err, data) {
  2292. assert.ok(data.headers['cache-control'] === 'max-age=7200', 'cache-control 正确');
  2293. done();
  2294. });
  2295. });
  2296. });
  2297. test('sliceUploadFile Cache-Control: no-cache -> Cache-Control: no-cache', function (done, assert) {
  2298. cos.sliceUploadFile({
  2299. Bucket: config.Bucket,
  2300. Region: config.Region,
  2301. Key: '1mb.zip',
  2302. Body: '',
  2303. CacheControl: 'no-cache',
  2304. }, function (err, data) {
  2305. cos.headObject({
  2306. Bucket: config.Bucket,
  2307. Region: config.Region,
  2308. Key: '1mb.zip',
  2309. }, function (err, data) {
  2310. assert.ok(data.headers['cache-control'] === 'no-cache' || data.headers['cache-control'] === 'no-cache, max-age=259200', 'cache-control 正确');
  2311. done();
  2312. });
  2313. });
  2314. });
  2315. });