12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845 |
- // config 替换成自己的桶信息
- var config = {
- Bucket: 'test-1300555317',
- Region: 'ap-beijing',
- Uin: '100011913628',
- };
- QUnit.log(function(details) {
- console.log("[test-log]" + JSON.stringify(details));
- });
- QUnit.done(function(details) {
- console.log("[test-result]" + JSON.stringify(details));
- });
- var util = {
- createFile: function (options) {
- var buffer = new ArrayBuffer(options.size || 0);
- var arr = new Uint8Array(buffer);
- for (var i = 0; i < arr.length; i++) {
- arr[i] = 0;
- }
- var opt = {};
- options.type && (opt.type = options.type);
- var blob = new Blob([buffer], options);
- return blob;
- },
- str2blob: function (str) {
- var size = str.length;
- var buffer = new ArrayBuffer(size || 0);
- var arr = new Uint8Array(buffer);
- for (var i = 0; i < arr.length; i++) {
- arr[i] = str[i];
- }
- var blob = new Blob([buffer]);
- return blob;
- }
- };
- function camSafeUrlEncode(str) {
- return encodeURIComponent(str)
- .replace(/!/g, '%21')
- .replace(/'/g, '%27')
- .replace(/\(/g, '%28')
- .replace(/\)/g, '%29')
- .replace(/\*/g, '%2A');
- }
- var getAuthorization = function (options, callback) {
- // 格式一、(推荐)后端通过获取临时密钥给到前端,前端计算签名
- // 服务端 JS 和 PHP 例子:https://github.com/tencentyun/cos-js-sdk-v5/blob/master/server/
- // 服务端其他语言参考 COS STS SDK :https://github.com/tencentyun/qcloud-cos-sts-sdk
- // var url = '../server/sts.php';
- var url = '/sts';
- var xhr = new XMLHttpRequest();
- xhr.open('GET', url, true);
- xhr.onload = function (e) {
- try {
- var data = JSON.parse(e.target.responseText);
- var credentials = data.credentials;
- } catch (e) {
- }
- callback({
- TmpSecretId: credentials.tmpSecretId,
- TmpSecretKey: credentials.tmpSecretKey,
- SecurityToken: credentials.sessionToken,
- ExpiredTime: data.expiredTime, // SDK 在 ExpiredTime 时间前,不会再次调用 getAuthorization
- });
- };
- xhr.send();
- // // 格式二、(推荐)【细粒度控制权限】后端通过获取临时密钥给到前端,前端只有相同请求才重用临时密钥,后端可以通过 Scope 细粒度控制权限
- // // 服务端例子:https://github.com/tencentyun/qcloud-cos-sts-sdk/edit/master/scope.md
- // // var url = '../server/sts.php';
- // var url = '/sts-scope';
- // var xhr = new XMLHttpRequest();
- // xhr.open('POST', url, true);
- // xhr.setRequestHeader('Content-Type', 'application/json');
- // xhr.onload = function (e) {
- // try {
- // var data = JSON.parse(e.target.responseText);
- // var credentials = data.credentials;
- // } catch (e) {
- // }
- // callback({
- // TmpSecretId: credentials.tmpSecretId,
- // TmpSecretKey: credentials.tmpSecretKey,
- // SecurityToken: credentials.sessionToken,
- // ExpiredTime: data.expiredTime,
- // ScopeLimit: true, // 设为 true 可限制密钥只在相同请求可重用,默认不限制一直可重用,细粒度控制权限需要设为 true
- // });
- // };
- // xhr.send(JSON.stringify(options.Scope));
- // // 格式三、(不推荐,分片上传权限不好控制)前端每次请求前都需要通过 getAuthorization 获取签名,后端使用固定密钥或临时密钥计算签名返回给前端
- // // 服务端获取签名,请参考对应语言的 COS SDK:https://cloud.tencent.com/document/product/436/6474
- // // 注意:这种有安全风险,后端需要通过 method、pathname 严格控制好权限,比如不允许 put / 等
- // var method = (options.Method || 'get').toLowerCase();
- // var query = options.Query || {};
- // var headers = options.Headers || {};
- // var pathname = options.Pathname || '/';
- // // var url = 'http://127.0.0.1:3000/auth';
- // var url = '../server/auth.php';
- // var xhr = new XMLHttpRequest();
- // var data = {
- // method: method,
- // pathname: pathname,
- // query: query,
- // headers: headers,
- // };
- // xhr.open('POST', url, true);
- // xhr.setRequestHeader('content-type', 'application/json');
- // xhr.onload = function (e) {
- // try {
- // var data = JSON.parse(e.target.responseText);
- // } catch (e) {
- // }
- // callback({
- // Authorization: data.authorization,
- // // SecurityToken: data.sessionToken, // 如果使用临时密钥,需要把 sessionToken 传给 SecurityToken
- // });
- // };
- // xhr.send(JSON.stringify(data));
- // // 格式四、(不推荐,适用于前端调试,避免泄露密钥)前端使用固定密钥计算签名,通过 COS.getAuthorization 静态方法计算
- // var authorization = COS.getAuthorization({
- // SecretId: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', // 可传固定密钥或者临时密钥
- // SecretKey: 'xxx', // 可传固定密钥或者临时密钥
- // Method: options.Method,
- // Pathname: options.Pathname,
- // Query: options.Query,
- // Headers: options.Headers,
- // Expires: 900,
- // });
- // callback({
- // Authorization: authorization,
- // // SecurityToken: credentials.sessionToken, // 如果使用临时密钥,需要传 SecurityToken
- // });
- // // 格式五、(不推荐,适用于前端调试,避免泄露密钥)前端使用固定密钥计算签名,通过 cos-auth.js 计算
- // var auth = CosAuth({
- // Version: '4.0',
- // SecretId: 'xxx',
- // SecretKey: 'xxx',
- // Bucket: config.Bucket,
- // Expires: 900,
- // Pathname: '/' + (options.Headers && options.Headers['x-cos-copy-source'] ? '' : options.Key),
- // });
- // callback({Authorization: auth});
- };
- var dataURItoUploadBody = function (dataURI) {
- var byteString = atob(dataURI.split(',')[1]);
- var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
- var ab = new ArrayBuffer(byteString.length);
- var ia = new Uint8Array(ab);
- for (var i = 0; i < byteString.length; i++) {
- ia[i] = byteString.charCodeAt(i);
- }
- return new Blob([ab], {type: mimeString});
- };
- var createFileSync = function (size) {
- return util.createFile({size: size});
- };
- var group = function (name, fn) {
- return fn();
- };
- var test = function (name, fn) {
- QUnit.test(name, function (assert) {
- return new Promise(function (done) {
- fn(done, assert);
- });
- });
- };
- var request = function (opt, callback) {
- var i;
- var xhr = new XMLHttpRequest();
- xhr.open('GET', opt.url, true);
- if (opt.headers) {
- for (i in opt.headers) {
- if (opt.headers.hasOwnProperty(i)) {
- xhr.setRequestHeader(i, opt.headers[i]);
- }
- }
- }
- xhr.onload = function (e) {
- callback(null, {statusCode: xhr.status}, xhr.responseText);
- };
- xhr.onerror = function (e) {
- callback('error', {statusCode: xhr.status});
- };
- xhr.send();
- };
- var cos = new COS({
- // 必选参数
- getAuthorization: getAuthorization,
- // 可选参数
- FileParallelLimit: 3, // 控制文件上传并发数
- ChunkParallelLimit: 3, // 控制单个文件下分片上传并发数
- ChunkSize: 1024 * 1024, // 控制分片大小,单位 B
- ProgressInterval: 1, // 控制 onProgress 回调的间隔
- ChunkRetryTimes: 3, // 控制文件切片后单片上传失败后重试次数
- UploadCheckContentMd5: true, // 上传过程计算 Content-MD5
- ForceSignHost: false,
- });
- var AppId = config.AppId;
- var Bucket = config.Bucket;
- var BucketShortName = Bucket;
- var BucketLongName = Bucket + '-' + AppId;
- var TaskId;
- var match = config.Bucket.match(/^(.+)-(\d+)$/);
- if (match) {
- BucketLongName = config.Bucket;
- BucketShortName = match[1];
- AppId = match[2];
- }
- function comparePlainObject(a, b) {
- if (Object.keys(a).length !== Object.keys(b).length) {
- return false;
- }
- for (var key in a) {
- if (typeof a[key] === 'object' && typeof b[key] === 'object') {
- if (!comparePlainObject(a[key], b[key])) {
- return false;
- }
- } else if (a[key] != b[key]) {
- return false;
- }
- }
- return true;
- }
- function prepareBigObject(needHeaders) {
- return new Promise(function (resolve, reject) {
- // 创建测试文件
- var filename = name || 'bigger.zip';
- var content = util.createFile({size: 1024 * 1024 * 10});
- var put = function () {
- // 调用方法
- var params = {
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- Body: content,
- ContentLength: content.length,
- };
- if (needHeaders) {
- params.ContentType = 'text/html';
- params.CacheControl = 'max-age=7200';
- params.ContentDisposition = 'inline;filename=hello.jpg';
- params.Expires = (new Date()).toGMTString();
- params.Headers = {
- 'x-cos-meta-test': 'xxx'
- };
- }
- cos.putObject(params, function (err, data) {
- err ? reject(err) : resolve()
- });
- };
- put();
- });
- }
- function prepareBucket() {
- return new Promise(function (resolve, reject) {
- resolve();
- });
- }
- group('mock readAsBinaryString', function () {
- test('mock readAsBinaryString', function (done, assert) {
- FileReader.prototype._readAsBinaryString = FileReader.prototype.readAsBinaryString;
- FileReader.prototype.readAsBinaryString = false;
- var filename = '10m.zip';
- var blob = util.createFile({size: 1024 * 1024 * 10});
- var paused = false;
- cos.sliceUploadFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- Body: blob,
- onTaskReady: function (taskId) {
- TaskId = taskId;
- },
- onProgress: function (info) {
- if (!paused && info.percent > 0.6) {
- cos.cancelTask(TaskId);
- var hasProgress = false;
- cos.sliceUploadFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- Body: blob,
- onTaskReady: function (taskId) {
- TaskId = taskId;
- },
- onProgress: function (info) {
- if (info.percent === 0) return;
- assert.ok(info.percent > 0.3, '分片续传成功');
- cos.cancelTask(TaskId);
- FileReader.prototype.readAsBinaryString = FileReader.prototype._readAsBinaryString;
- delete FileReader.prototype._readAsBinaryString;
- done();
- }
- }, function (err) {
- if (hasProgress) {
- assert.ok(false, '分片续传');
- done();
- }
- });
- }
- }
- });
- });
- });
- group('getAuth()', function () {
- test('getAuth()', function (done, assert) {
- var content = Date.now().toString();
- var key = '1.txt';
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: key,
- Body: content,
- }, function (err, data) {
- cos.options.getAuthorization({
- Method: 'get',
- Key: key,
- Scope: [{
- action: 'GetObject',
- bucket: config.Bucket,
- region: config.Region,
- prefix: key,
- }],
- }, function (AuthData) {
- if (typeof AuthData === 'string') {
- AuthData = {Authorization: AuthData};
- }
- if (!AuthData.Authorization) {
- AuthData.Authorization = COS.getAuthorization({
- SecretId: AuthData.TmpSecretId,
- SecretKey: AuthData.TmpSecretKey,
- Method: 'get',
- Key: key,
- SystemClockOffset: cos.options.SystemClockOffset,
- });
- }
- var link = 'http://' + config.Bucket + '.cos.' + config.Region + '.myqcloud.com' + '/' +
- camSafeUrlEncode(key).replace(/%2F/g, '/') + '?' + AuthData.Authorization +
- (AuthData.SecurityToken ? '&x-cos-security-token=' + AuthData.SecurityToken : '');
- request({
- url: link,
- }, function (err, response, body) {
- assert.ok(response.statusCode === 200);
- assert.ok(body === content);
- done();
- });
- });
- });
- });
- });
- group('getObjectUrl()', function () {
- test('getObjectUrl()', function (done, assert) {
- var content = Date.now().toString();
- var key = '1.txt';
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: key,
- Body: content,
- }, function (err, data) {
- cos.getObjectUrl({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: key,
- }, function (err, data) {
- request({
- url: data.Url,
- }, function (err, response, body) {
- assert.ok(!err, '文件获取出错');
- assert.ok(response.statusCode === 200, '获取文件 200');
- assert.ok(body.toString() === content, '通过获取签名能正常获取文件');
- done();
- });
- });
- });
- });
- });
- group('auth check', function () {
- test('auth check', function (done, assert) {
- cos.getBucket({
- Bucket: config.Bucket,
- Region: config.Region,
- Prefix: 'aksjhdlash sajlhj!@#$%^&*()_+=-[]{}\';:"/.<>?.,??sadasd#/.,/~`',
- Headers: {
- 'x-cos-test': 'aksjhdlash sajlhj!@#$%^&*()_+=-[]{}\';:\"/.<>?.,??sadasd#/.,/~`',
- },
- }, function (err, data) {
- assert.ok(!err);
- done();
- });
- });
- });
- group('getBucket(),listObjectVersions', function () {
- test('正常获取 bucket 里的文件列表', function (done, assert) {
- prepareBucket().then(function () {
- cos.getBucket({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(data.Name === BucketLongName);
- assert.ok(data.Contents.constructor, Array);
- done();
- });
- }).catch(function () {
- assert.ok(false);
- done();
- });
- });
- test('正常获取 bucket 里的文件版本列表', function (done, assert) {
- prepareBucket().then(function () {
- cos.listObjectVersions({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(data.Name === BucketLongName);
- assert.ok(data.Versions.constructor === Array);
- done();
- });
- }).catch(function () {
- assert.ok(false);
- done();
- });
- });
- });
- group('putObject(),cancelTask()', function () {
- test('putObject(),cancelTask()', function (done, assert) {
- var filename = '10m.zip';
- var alive = false;
- var canceled = false;
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- Body: util.createFile({size: 1024 * 1024 * 10}),
- onTaskReady: function (taskId) {
- TaskId = taskId;
- },
- onProgress: function (info) {
- alive = true;
- if (!canceled) {
- cos.cancelTask(TaskId);
- alive = false;
- canceled = true;
- setTimeout(function () {
- assert.ok(!alive, '取消上传已经生效');
- done();
- }, 1200);
- }
- }
- }, function (err, data) {
- alive = true;
- });
- });
- });
- group('sliceUploadFile() 完整上传文件', function () {
- test('sliceUploadFile() 完整上传文件', function (done, assert) {
- var lastPercent;
- var filename = '3m.zip';
- var fileSize = 1024 * 1024 * 3;
- var blob = createFileSync(fileSize)
- cos.abortUploadTask({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- Level: 'file',
- }, function (err, data) {
- cos.sliceUploadFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- Body: blob,
- onTaskReady: function (taskId) {
- TaskId = taskId;
- },
- onProgress: function (info) {
- lastPercent = info.percent;
- }
- }, function (err, data) {
- assert.ok(data.ETag.length > 0);
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- }, function (err, data) {
- assert.ok(data && data.headers && data.headers.etag && data.headers.etag.length > 0, '文件已上传成功');
- assert.ok(data && data.headers && parseInt(data.headers['content-length'] || 0) === fileSize, '文件大小一致');
- done();
- });
- });
- });
- });
- test('sliceUploadFile(),pauseTask(),restartTask()', function (done, assert) {
- var filename = '10m.zip';
- var blob = util.createFile({size: 1024 * 1024 * 10});
- var paused = false;
- var restarted = false;
- cos.abortUploadTask({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- Level: 'file',
- }, function (err, data) {
- var TaskId;
- cos.sliceUploadFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- Body: blob,
- onTaskReady: function (taskId) {
- TaskId = taskId;
- },
- onProgress: function (info) {
- if (!paused && info.percent > 0.6) {
- cos.pauseTask(TaskId);
- paused = true;
- setTimeout(function () {
- restarted = true;
- cos.restartTask(TaskId);
- }, 1000);
- }
- if (paused && restarted) {
- if (info.percent === 0) return;
- assert.ok(info.percent > 0.3, '暂停和重试成功');
- cos.cancelTask(TaskId);
- done();
- }
- }
- }, function (err, data) {
- paused = true;
- });
- });
- });
- test('sliceUploadFile(),cancelTask(),restartTask()', function (done, assert) {
- var filename = '10m.zip';
- var blob = util.createFile({size: 1024 * 1024 * 3});
- var paused = false;
- cos.abortUploadTask({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- Level: 'file',
- }, function (err, data) {
- var TaskId;
- cos.sliceUploadFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- Body: blob,
- onTaskReady: function (taskId) {
- TaskId = taskId;
- },
- onProgress: function (info) {
- if (!paused && info.percent > 0.6) {
- cos.cancelTask(TaskId);
- setTimeout(function () {
- cos.sliceUploadFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- Body: blob,
- }, function (err, data) {
- assert.ok(!err);
- done();
- });
- }, 10);
- }
- }
- }, function (err, data) {
- });
- });
- });
- test('sliceUploadFile(),cancelTask()', function (done, assert) {
- var filename = '3m.zip';
- var alive = false;
- var canceled = false;
- cos.sliceUploadFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- Body: util.createFile({size: 1024 * 1024 * 3}),
- onTaskReady: function (taskId) {
- TaskId = taskId;
- },
- onProgress: function (info) {
- alive = true;
- if (!canceled) {
- cos.cancelTask(TaskId);
- alive = false;
- canceled = true;
- setTimeout(function () {
- assert.ok(!alive, '取消上传已经生效');
- done();
- }, 1200);
- }
- }
- }, function (err, data) {
- alive = true;
- });
- });
- });
- group('abortUploadTask()', function () {
- test('abortUploadTask(),Level=task', function (done, assert) {
- var filename = '1m.zip';
- cos.multipartInit({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- }, function (err, data) {
- cos.abortUploadTask({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- Level: 'task',
- UploadId: data.UploadId,
- }, function (err, data) {
- var nameExist = false;
- data.successList.forEach(function (item) {
- if (filename === item.Key) {
- nameExist = true;
- }
- });
- assert.ok(data.successList.length >= 1, '成功取消单个分片任务');
- assert.ok(nameExist, '成功取消单个分片任务');
- done();
- });
- });
- });
- test('abortUploadTask(),Level=file', function (done, assert) {
- var filename = '1m.zip';
- var blob = util.createFile({size: 1024 * 1024});
- cos.sliceUploadFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- Body: blob,
- onTaskReady: function (taskId) {
- TaskId = taskId;
- },
- onProgress: function (info) {
- cos.cancelTask(TaskId);
- cos.abortUploadTask({
- Bucket: config.Bucket,
- Region: config.Region,
- Level: 'file',
- Key: filename,
- }, function (err, data) {
- assert.ok(data.successList.length >= 1, '成功舍弃单个文件下的所有分片任务');
- assert.ok(data.successList[0] && data.successList[0].Key === filename, '成功舍弃单个文件的所有分片任务');
- done();
- });
- }
- });
- });
- test('abortUploadTask(),Level=bucket', function (done, assert) {
- var filename = '1m.zip';
- var blob = util.createFile({size: 1024 * 1024 * 10});
- cos.sliceUploadFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- Body: blob,
- onTaskReady: function (taskId) {
- TaskId = taskId;
- },
- onProgress: function (info) {
- cos.cancelTask(TaskId);
- cos.abortUploadTask({
- Bucket: config.Bucket,
- Region: config.Region,
- Level: 'bucket',
- }, function (err, data) {
- var nameExist = false;
- data.successList.forEach(function (item) {
- if (filename === item.Key) {
- nameExist = true;
- }
- });
- assert.ok(data.successList.length >= 1, '成功舍弃Bucket下所有分片任务');
- assert.ok(nameExist, '成功舍弃Bucket下所有分片任务');
- done();
- });
- }
- });
- });
- });
- group('headBucket()', function () {
- test('headBucket()', function (done, assert) {
- cos.headBucket({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(data, '正常获取 head bucket');
- done();
- });
- });
- test('headBucket() not exist', function (done, assert) {
- cos.headBucket({
- Bucket: config.Bucket + Date.now().toString(36),
- Region: config.Region
- }, function (err, data) {
- assert.ok(err, 'bucket 不存在');
- done();
- });
- });
- test('deleteBucket()', function (done, assert) {
- cos.deleteBucket({
- Bucket: config.Bucket + Date.now().toString(36),
- Region: config.Region
- }, function (err, data) {
- assert.ok(err, '正常获取 head bucket');
- done();
- });
- });
- test('getBucket()', function (done, assert) {
- cos.getBucket({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(data.Name === BucketLongName, '能列出 bucket');
- assert.ok(data.Contents.constructor === Array, '正常获取 bucket 里的文件列表');
- done();
- });
- });
- });
- group('putObject()', function () {
- var buf = new ArrayBuffer(8);
- var arr = new Uint8Array(buf);
- [0x89, 0xe8,0xaf,0xb4,0x2e,0x70,0x72,0x70,0x72].forEach(function (v, i) {
- arr[i] = v;
- });
- test('putObject()', function (done, assert) {
- var filename = '1.txt';
- var getObjectETag = function (callback) {
- setTimeout(function () {
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- }, function (err, data) {
- callback(data && data.headers && data.headers.etag);
- });
- }, 2000);
- };
- var content = Date.now().toString();
- var lastPercent = 0;
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- Body: util.str2blob(content),
- onProgress: function (info) {
- lastPercent = info.percent;
- },
- }, function (err, data) {
- if (err) throw err;
- assert.ok(data && data.ETag, 'putObject 有返回 ETag');
- getObjectETag(function (ETag) {
- assert.ok(data.ETag === ETag, 'Blob 创建 object');
- done();
- });
- });
- });
- test('putObject(),string', function (done, assert) {
- var filename = '1.txt';
- var content = '中文_' + Date.now().toString(36);
- var lastPercent = 0;
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- Body: content,
- onProgress: function (info) {
- lastPercent = info.percent;
- },
- }, function (err, data) {
- if (err) throw err;
- var ETag = data && data.ETag;
- assert.ok(ETag, 'putObject 有返回 ETag');
- cos.getObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- }, function (err, data) {
- assert.ok(data.Body && data.Body === content && (data.headers && data.headers.etag) === ETag);
- done();
- });
- });
- });
- test('putObject(),string,empty', function (done, assert) {
- var content = '';
- var lastPercent = 0;
- var Key = '1.txt';
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- Body: content,
- onProgress: function (info) {
- lastPercent = info.percent;
- },
- }, function (err, data) {
- if (err) throw err;
- var ETag = data && data.ETag;
- assert.ok(ETag, 'putObject 有返回 ETag');
- cos.getObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- }, function (err, data) {
- assert.ok(data.Body === content && (data.headers && data.headers.etag) === ETag);
- done();
- });
- });
- });
- test('putObject(),特殊二进制字符 ArrayBuffer md5', function (done, assert) {
- var content = '';
- var lastPercent = 0;
- var Key = '1.mp4';
- var blob = new Blob([buf]);
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- Body: blob,
- onProgress: function (info) {
- lastPercent = info.percent;
- },
- }, function (err, data) {
- if (err) throw err;
- var ETag = data && data.ETag;
- assert.ok(ETag, 'putObject 有返回 ETag');
- cos.getObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- BodyType: 'arraybuffer',
- }, function (err, data) {
- var arr = new Uint8Array(data.Body);
- var isSame = arr.every(function (v, i) {
- return v === arr[i];
- });
- assert.ok(isSame && (data.headers && data.headers.etag) === ETag);
- done();
- });
- });
- });
- test('putObject(),特殊二进制字符 Blob md5', function (done, assert) {
- var content = '';
- var lastPercent = 0;
- var Key = '1.mp4';
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- Body: buf,
- onProgress: function (info) {
- lastPercent = info.percent;
- },
- }, function (err, data) {
- if (err) throw err;
- var ETag = data && data.ETag;
- assert.ok(ETag, 'putObject 有返回 ETag');
- cos.getObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- BodyType: 'arraybuffer',
- }, function (err, data) {
- var arr = new Uint8Array(data.Body);
- var isSame = arr.every(function (v, i) {
- return v === arr[i];
- });
- assert.ok(isSame && (data.headers && data.headers.etag) === ETag);
- done();
- });
- });
- });
- test('putObject(),特殊二进制字符 中文 string md5', function (done, assert) {
- var Key = '1.txt';
- var content = '中文';
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- Body: content,
- }, function (err, data) {
- if (err) throw err;
- var ETag = data && data.ETag;
- assert.ok(ETag, 'putObject 有返回 ETag');
- cos.getObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- }, function (err, data) {
- var isSame = content === data.Body;
- assert.ok(isSame && (data.headers && data.headers.etag) === ETag);
- done();
- });
- });
- });
- test('putObject(),特殊二进制字符 unescape string md5', function (done, assert) {
- var Key = '1.txt';
- var content = unescape(encodeURIComponent('中文'));
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- Body: content,
- }, function (err, data) {
- if (err) throw err;
- var ETag = data && data.ETag;
- assert.ok(ETag, 'putObject 有返回 ETag');
- cos.getObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- }, function (err, data) {
- var isSame = content === data.Body;
- assert.ok(isSame && (data.headers && data.headers.etag) === ETag);
- done();
- });
- });
- });
- });
- group('getObject()', function () {
- test('getObject() body', function (done, assert) {
- var key = '1.txt';
- var content = Date.now().toString();
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: key,
- Body: content,
- }, function (err, data) {
- cos.getObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: key
- }, function (err, data) {
- if (err) throw err;
- var objectContent = data.Body.toString();
- assert.ok(data.headers['content-length'] === '' + content.length);
- assert.ok(objectContent === content);
- done();
- });
- });
- });
- test('getObject() DataType blob', function (done, assert) {
- var key = '1.txt';
- var content = Date.now().toString();
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: key,
- Body: content,
- }, function (err, data) {
- cos.getObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: key,
- DataType: 'blob',
- }, function (err, data) {
- if (err) throw err;
- assert.ok(data.Body instanceof Blob);
- assert.ok(data.headers['content-length'] === '' + content.length);
- done();
- });
- });
- });
- test('getObject() DataType arraybuffer', function (done, assert) {
- var key = '1.txt';
- var content = Date.now().toString();
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: key,
- Body: content,
- }, function (err, data) {
- cos.getObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: key,
- DataType: 'arraybuffer',
- }, function (err, data) {
- if (err) throw err;
- assert.ok(data.Body instanceof ArrayBuffer);
- assert.ok(data.headers['content-length'] === '' + content.length);
- done();
- });
- });
- });
- });
- group('Key 特殊字符', function () {
- test('Key 特殊字符', function (done, assert) {
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '(!\'*) "#$%&+,-./0123456789:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
- Body: Date.now().toString()
- }, function (err, data) {
- if (err) throw err;
- assert.ok(data, 'putObject 特殊字符的 Key 能通过');
- done();
- });
- });
- });
- group('putObjectCopy() 1', function () {
- test('putObjectCopy() 1', function (done, assert) {
- var content = Date.now().toString(36);
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.txt',
- Body: content,
- }, function (err, data) {
- var ETag = data.ETag;
- cos.deleteObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.copy.txt',
- }, function (err, data) {
- cos.putObjectCopy({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.copy.txt',
- CopySource: BucketLongName + '.cos.' + config.Region + '.myqcloud.com/1.txt',
- }, function (err, data) {
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.copy.txt',
- }, function (err, data) {
- assert.ok(data.headers && data.headers.etag === ETag, '成功复制文件');
- done();
- });
- });
- });
- });
- });
- });
- group('putObjectCopy()', function () {
- var filename = '1.txt';
- test('正常复制 object', function (done, assert) {
- cos.putObjectCopy({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.copy.txt',
- CopySource: config.Bucket + '.cos.' + config.Region + '.myqcloud.com/' + filename,
- }, function (err, data) {
- assert.ok(!err);
- assert.ok(data.ETag.length > 0);
- done();
- });
- });
- test('捕获 object 异常', function (done, assert) {
- var errFileName = '12345.txt';
- cos.putObjectCopy({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.copy.txt',
- CopySource: config.Bucket + '.cos.' + config.Region + '.myqcloud.com/' + errFileName,
- }, function (err, data) {
- assert.equal(true, err.statusCode === 404);
- assert.equal(true, err.error.Code === 'NoSuchKey')
- done();
- });
- });
- });
- group('sliceCopyFile()', function () {
- var filename = 'bigger.zip';
- var Key = 'bigger.copy.zip';
- test('正常分片复制 object', function (done, assert) {
- prepareBigObject(true).then(function () {
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- }, function (err, data1) {
- if (err) throw err;
- cos.sliceCopyFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- CopySource: config.Bucket + '.cos.' + config.Region + '.myqcloud.com/'+ filename,
- CopySliceSize: 5 * 1024 * 1024,
- },function (err, data) {
- if (err) throw err;
- assert.ok(data.ETag.length > 0);
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- }, function (err, data2) {
- if (err) throw err;
- delete data1.VersionId;
- delete data2.VersionId;
- delete data1.headers['x-cos-request-id'];
- delete data2.headers['x-cos-request-id'];
- delete data1.headers['x-cos-version-id'];
- delete data2.headers['x-cos-version-id'];
- delete data1.headers['x-cos-replication-status'];
- delete data2.headers['x-cos-replication-status'];
- delete data1.headers['last-modified'];
- delete data2.headers['last-modified'];
- delete data1.headers['date'];
- delete data2.headers['date'];
- delete data1.headers['etag'];
- delete data2.headers['etag'];
- delete data1.ETag;
- delete data2.ETag;
- delete data1.RequestId;
- delete data2.RequestId;
- assert.ok(comparePlainObject(data1, data2));
- done();
- });
- });
- });
- }).catch(function () {
- assert.ok(false);
- done();
- });
- });
- test('单片复制 object', function (done, assert) {
- setTimeout(function () {
- prepareBigObject(true).then(function () {
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- }, function (err, data1) {
- if (err) throw err;
- cos.sliceCopyFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- CopySource: config.Bucket + '.cos.' + config.Region + '.myqcloud.com/' + filename,
- SliceSize: 10 * 1024 * 1024,
- }, function (err, data) {
- if (err) throw err;
- assert.ok(data.ETag.length > 0);
- setTimeout(function () {
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- }, function (err, data2) {
- if (err) throw err;
- delete data1.VersionId;
- delete data2.VersionId;
- delete data1.headers['x-cos-request-id'];
- delete data2.headers['x-cos-request-id'];
- delete data1.headers['x-cos-version-id'];
- delete data2.headers['x-cos-version-id'];
- delete data1.headers['x-cos-replication-status'];
- delete data2.headers['x-cos-replication-status'];
- delete data1.headers['last-modified'];
- delete data2.headers['last-modified'];
- delete data1.headers['date'];
- delete data2.headers['date'];
- delete data1.ETag;
- delete data2.ETag;
- delete data1.RequestId;
- delete data2.RequestId;
- assert.ok(comparePlainObject(data1, data2));
- done();
- });
- }, 2000);
- });
- });
- }).catch(function () {
- assert.ok(false);
- done();
- });
- }, 2000);
- });
- test('CopySource nor found', function (done, assert) {
- cos.sliceCopyFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- CopySource: config.Bucket + '.cos.' + config.Region + '.myqcloud.com/' + Date.now(),
- }, function (err, data) {
- assert.ok(err);
- done();
- });
- });
- test('复制归档文件', function (done, assert) {
- var sourceKey = Date.now().toString(36);
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: sourceKey,
- StorageClass: 'ARCHIVE',
- }, function () {
- cos.sliceCopyFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- CopySource: config.Bucket + '.cos.' + config.Region + '.myqcloud.com/' + sourceKey,
- }, function (err, data) {
- assert.ok(err);
- done();
- });
- });
- });
- });
- group('deleteMultipleObject', function () {
- test('deleteMultipleObject()', function (done, assert) {
- var content = Date.now().toString(36);
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.txt',
- Body: content,
- }, function (err, data) {
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '2.txt',
- Body: content,
- }, function (err, data) {
- cos.deleteMultipleObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Objects: [
- {Key: '1.txt'},
- {Key: '2.txt'},
- ],
- }, function (err, data) {
- assert.ok(data.Deleted.length === 2);
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.txt',
- }, function (err, data) {
- assert.ok(err.statusCode === 404, '1.txt 删除成功');
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '2.txt',
- }, function (err, data) {
- assert.ok(err.statusCode === 404, '2.txt 删除成功');
- done();
- });
- });
- });
- });
- });
- });
- });
- group('BucketAcl', function () {
- var AccessControlPolicy = {
- "Owner": {
- "ID": 'qcs::cam::uin/10001:uin/10001' // 10001 是 QQ 号
- },
- "Grants": [{
- "Grantee": {
- "ID": "qcs::cam::uin/10002:uin/10002", // 10002 是 QQ 号
- },
- "Permission": "READ"
- }]
- };
- var AccessControlPolicy2 = {
- "Owner": {
- "ID": 'qcs::cam::uin/10001:uin/10001' // 10001 是 QQ 号
- },
- "Grant": {
- "Grantee": {
- "ID": "qcs::cam::uin/10002:uin/10002", // 10002 是 QQ 号
- },
- "Permission": "READ"
- }
- };
- test('putBucketAcl() header ACL:private', function (done, assert) {
- cos.putBucketAcl({
- Bucket: config.Bucket,
- Region: config.Region,
- ACL: 'private'
- }, function (err, data) {
- assert.ok(!err, 'putBucketAcl 成功');
- cos.getBucketAcl({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- AccessControlPolicy.Owner.ID = data.Owner.ID;
- AccessControlPolicy2.Owner.ID = data.Owner.ID;
- assert.ok(data.ACL === 'private' || data.ACL === 'default');
- done();
- });
- });
- });
- test('putBucketAcl() header ACL:public-read', function (done, assert) {
- cos.putBucketAcl({
- Bucket: config.Bucket,
- Region: config.Region,
- ACL: 'public-read',
- }, function (err, data) {
- assert.ok(!err, 'putBucketAcl 成功');
- cos.getBucketAcl({Bucket: config.Bucket, Region: config.Region}, function (err, data) {
- assert.ok(data.ACL === 'public-read');
- done();
- });
- });
- });
- test('putBucketAcl() header ACL:public-read-write', function (done, assert) {
- cos.putBucketAcl({
- Bucket: config.Bucket,
- Region: config.Region,
- ACL: 'public-read-write',
- }, function (err, data) {
- assert.ok(!err, 'putBucketAcl 成功');
- cos.getBucketAcl({Bucket: config.Bucket, Region: config.Region}, function (err, data) {
- assert.ok(data.ACL === 'public-read-write');
- done();
- });
- });
- });
- test('putBucketAcl() header GrantRead:1001,1002', function (done, assert) {
- var GrantRead = 'id="qcs::cam::uin/1001:uin/1001", id="qcs::cam::uin/1002:uin/1002"';
- cos.putBucketAcl({
- Bucket: config.Bucket,
- Region: config.Region,
- GrantRead: GrantRead,
- }, function (err, data) {
- assert.ok(!err, 'putBucketAcl 成功');
- cos.getBucketAcl({Bucket: config.Bucket, Region: config.Region}, function (err, data) {
- assert.ok(data.GrantRead = GrantRead);
- done();
- });
- });
- });
- test('putBucketAcl() header GrantWrite:1001,1002', function (done, assert) {
- var GrantWrite = 'id="qcs::cam::uin/1001:uin/1001", id="qcs::cam::uin/1002:uin/1002"';
- cos.putBucketAcl({
- Bucket: config.Bucket,
- Region: config.Region,
- GrantWrite: GrantWrite,
- }, function (err, data) {
- assert.ok(!err, 'putBucketAcl 成功');
- cos.getBucketAcl({Bucket: config.Bucket, Region: config.Region}, function (err, data) {
- assert.ok(data.GrantWrite = GrantWrite);
- done();
- });
- });
- });
- test('putBucketAcl() header GrantFullControl:1001,1002', function (done, assert) {
- var GrantFullControl = 'id="qcs::cam::uin/1001:uin/1001", id="qcs::cam::uin/1002:uin/1002"';
- cos.putBucketAcl({
- Bucket: config.Bucket,
- Region: config.Region,
- GrantFullControl: GrantFullControl,
- }, function (err, data) {
- assert.ok(!err, 'putBucketAcl 成功');
- cos.getBucketAcl({Bucket: config.Bucket, Region: config.Region}, function (err, data) {
- assert.ok(data.GrantFullControl = GrantFullControl);
- done();
- });
- });
- });
- test('putBucketAcl() header ACL:public-read, GrantFullControl:1001,1002', function (done, assert) {
- var GrantFullControl = 'id="qcs::cam::uin/1001:uin/1001", id="qcs::cam::uin/1002:uin/1002"';
- cos.putBucketAcl({
- Bucket: config.Bucket,
- Region: config.Region,
- GrantFullControl: GrantFullControl,
- ACL: 'public-read',
- }, function (err, data) {
- assert.ok(!err, 'putBucketAcl 成功');
- cos.getBucketAcl({Bucket: config.Bucket, Region: config.Region}, function (err, data) {
- assert.ok(data.GrantFullControl = GrantFullControl);
- assert.ok(data.ACL === 'public-read');
- done();
- });
- });
- });
- test('putBucketAcl() xml', function (done, assert) {
- cos.putBucketAcl({
- Bucket: config.Bucket,
- Region: config.Region,
- AccessControlPolicy: AccessControlPolicy
- }, function (err, data) {
- assert.ok(!err, 'putBucketAcl 成功');
- cos.getBucketAcl({Bucket: config.Bucket, Region: config.Region}, function (err, data) {
- assert.ok(data.Grants.length === 1);
- assert.ok(data.Grants[0] && data.Grants[0].Grantee.ID === 'qcs::cam::uin/10002:uin/10002', '设置 AccessControlPolicy ID 正确');
- assert.ok(data.Grants[0] && data.Grants[0].Permission === 'READ', '设置 AccessControlPolicy Permission 正确');
- done();
- });
- });
- });
- test('putBucketAcl() xml2', function (done, assert) {
- cos.putBucketAcl({
- Bucket: config.Bucket,
- Region: config.Region,
- AccessControlPolicy: AccessControlPolicy2,
- }, function (err, data) {
- assert.ok(!err, 'putBucketAcl 成功');
- cos.getBucketAcl({Bucket: config.Bucket, Region: config.Region}, function (err, data) {
- assert.ok(data.Grants.length === 1);
- assert.ok(data.Grants[0] && data.Grants[0].Grantee.ID === 'qcs::cam::uin/10002:uin/10002');
- assert.ok(data.Grants[0] && data.Grants[0].Permission === 'READ');
- done();
- });
- });
- });
- test('putBucketAcl() decodeAcl', function (done, assert) {
- cos.getBucketAcl({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- cos.putBucketAcl({
- Bucket: config.Bucket,
- Region: config.Region,
- GrantFullControl: data.GrantFullControl,
- GrantWrite: data.GrantWrite,
- GrantRead: data.GrantRead,
- ACL: data.ACL,
- }, function (err, data) {
- assert.ok(data);
- done();
- });
- });
- });
- });
- group('ObjectAcl', function () {
- var AccessControlPolicy = {
- "Owner": {
- "ID": 'qcs::cam::uin/10001:uin/10001' // 10001 是 QQ 号
- },
- "Grants": [{
- "Grantee": {
- "ID": "qcs::cam::uin/10002:uin/10002", // 10002 是 QQ 号
- },
- "Permission": "READ"
- }]
- };
- var AccessControlPolicy2 = {
- "Owner": {
- "ID": 'qcs::cam::uin/10001:uin/10001' // 10001 是 QQ 号
- },
- "Grant": {
- "Grantee": {
- "ID": "qcs::cam::uin/10002:uin/10002", // 10002 是 QQ 号
- },
- "Permission": "READ"
- }
- };
- test('putObjectAcl() header ACL:private', function (done, assert) {
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.txt',
- Body: 'hello!',
- }, function (err, data) {
- assert.ok(!err);
- cos.putObjectAcl({
- Bucket: config.Bucket,
- Region: config.Region,
- ACL: 'private',
- Key: '1.txt',
- }, function (err, data) {
- assert.ok(!err, 'putObjectAcl 成功');
- cos.getObjectAcl({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.txt'
- }, function (err, data) {
- assert.ok(data.ACL = 'private');
- AccessControlPolicy.Owner.ID = data.Owner.ID;
- AccessControlPolicy2.Owner.ID = data.Owner.ID;
- assert.ok(data.Grants.length === 1);
- done();
- });
- });
- });
- });
- test('putObjectAcl() header ACL:default', function (done, assert) {
- cos.putObjectAcl({
- Bucket: config.Bucket,
- Region: config.Region,
- ACL: 'default',
- Key: '1.txt',
- }, function (err, data) {
- assert.ok(!err, 'putObjectAcl 成功');
- cos.getObjectAcl({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.txt'
- }, function (err, data) {
- assert.ok(data.ACL = 'default');
- done();
- });
- });
- });
- test('putObjectAcl() header ACL:public-read', function (done, assert) {
- cos.putObjectAcl({
- Bucket: config.Bucket,
- Region: config.Region,
- ACL: 'public-read',
- Key: '1.txt',
- }, function (err, data) {
- assert.ok(!err, 'putObjectAcl 成功');
- cos.getObjectAcl({Bucket: config.Bucket, Region: config.Region, Key: '1.txt'}, function (err, data) {
- assert.ok(data.ACL = 'public-read');
- done();
- });
- });
- });
- // Object 不再支持修改写权限
- // test('putObjectAcl() header ACL:public-read-write', function (done, assert) {
- // cos.putObjectAcl({
- // Bucket: config.Bucket,
- // Region: config.Region,
- // ACL: 'public-read-write',
- // Key: '1.txt',
- // }, function (err, data) {
- // assert.ok(!err, 'putObjectAcl 成功');
- // cos.getObjectAcl({Bucket: config.Bucket, Region: config.Region, Key: '1.txt'}, function (err, data) {
- // assert.ok(data.ACL = 'public-read-write');
- // done();
- // });
- // });
- // });
- test('putObjectAcl() header GrantRead:1001,1002', function (done, assert) {
- var GrantRead = 'id="qcs::cam::uin/1001:uin/1001",id="qcs::cam::uin/1002:uin/1002"';
- cos.putObjectAcl({
- Bucket: config.Bucket,
- Region: config.Region,
- GrantRead: GrantRead,
- Key: '1.txt',
- }, function (err, data) {
- assert.ok(!err, 'putObjectAcl 成功');
- cos.getObjectAcl({Bucket: config.Bucket, Region: config.Region, Key: '1.txt'}, function (err, data) {
- assert.ok(data.GrantRead = GrantRead);
- done();
- });
- });
- });
- // Object 不再支持修改写权限
- // test('putObjectAcl() header GrantWrite:1001,1002', function (done, assert) {
- // var GrantWrite = 'id="qcs::cam::uin/1001:uin/1001", id="qcs::cam::uin/1002:uin/1002"';
- // cos.putObjectAcl({
- // Bucket: config.Bucket,
- // Region: config.Region,
- // GrantWrite: GrantWrite,
- // Key: '1.txt',
- // }, function (err, data) {
- // assert.ok(!err, 'putObjectAcl 成功');
- // cos.getObjectAcl({Bucket: config.Bucket, Region: config.Region, Key: '1.txt'}, function (err, data) {
- // assert.ok(data.GrantWrite = GrantWrite);
- // done();
- // });
- // });
- // });
- test('putObjectAcl() header GrantFullControl:1001,1002', function (done, assert) {
- var GrantFullControl = 'id="qcs::cam::uin/1001:uin/1001", id="qcs::cam::uin/1002:uin/1002"';
- cos.putObjectAcl({
- Bucket: config.Bucket,
- Region: config.Region,
- GrantFullControl: GrantFullControl,
- Key: '1.txt',
- }, function (err, data) {
- assert.ok(!err, 'putObjectAcl 成功');
- cos.getObjectAcl({Bucket: config.Bucket, Region: config.Region, Key: '1.txt'}, function (err, data) {
- assert.ok(data.GrantFullControl = GrantFullControl);
- done();
- });
- });
- });
- test('putObjectAcl() header ACL:public-read, GrantRead:1001,1002', function (done, assert) {
- var GrantFullControl = 'id="qcs::cam::uin/1001:uin/1001", id="qcs::cam::uin/1002:uin/1002"';
- cos.putObjectAcl({
- Bucket: config.Bucket,
- Region: config.Region,
- GrantFullControl: GrantFullControl,
- ACL: 'public-read',
- Key: '1.txt',
- }, function (err, data) {
- assert.ok(!err, 'putObjectAcl 成功');
- cos.getObjectAcl({Bucket: config.Bucket, Region: config.Region, Key: '1.txt'}, function (err, data) {
- assert.ok(data.GrantFullControl = GrantFullControl);
- assert.ok(data.ACL = 'public-read');
- done();
- });
- });
- });
- test('putObjectAcl() xml', function (done, assert) {
- cos.putObjectAcl({
- Bucket: config.Bucket,
- Region: config.Region,
- AccessControlPolicy: AccessControlPolicy,
- Key: '1.txt',
- }, function (err, data) {
- assert.ok(!err, 'putObjectAcl 成功');
- cos.getBucketAcl({Bucket: config.Bucket, Region: config.Region, Key: '1.txt'}, function (err, data) {
- assert.ok(data.Grants.length === 1);
- assert.ok(data.Grants[0] && data.Grants[0].Grantee.ID === 'qcs::cam::uin/10002:uin/10002', '设置 AccessControlPolicy ID 正确');
- assert.ok(data.Grants[0] && data.Grants[0].Permission === 'READ', '设置 AccessControlPolicy Permission 正确');
- done();
- });
- });
- });
- test('putObjectAcl() xml2', function (done, assert) {
- cos.putObjectAcl({
- Bucket: config.Bucket,
- Region: config.Region,
- AccessControlPolicy: AccessControlPolicy2,
- Key: '1.txt',
- }, function (err, data) {
- assert.ok(!err, 'putObjectAcl 成功');
- cos.getObjectAcl({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.txt'
- }, function (err, data) {
- assert.ok(data.Grants.length === 1);
- assert.ok(data.Grants[0] && data.Grants[0].Grantee.ID === 'qcs::cam::uin/10002:uin/10002', 'ID 正确');
- assert.ok(data.Grants[0] && data.Grants[0].Permission === 'READ', 'Permission 正确');
- done();
- });
- });
- });
- test('putObjectAcl() decodeAcl', function (done, assert) {
- cos.getObjectAcl({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.txt'
- }, function (err, data) {
- cos.putObjectAcl({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.txt',
- GrantFullControl: data.GrantFullControl,
- GrantWrite: data.GrantWrite,
- GrantRead: data.GrantRead,
- ACL: data.ACL,
- }, function (err, data) {
- assert.ok(data);
- done();
- });
- });
- });
- });
- group('BucketCors', function () {
- var CORSRules = [{
- "AllowedOrigins": ["*"],
- "AllowedMethods": ["GET", "POST", "PUT", "DELETE", "HEAD"],
- "AllowedHeaders": ["*", 'test-' + Date.now().toString(36)],
- "ExposeHeaders": [
- 'etag',
- 'date',
- 'content-length',
- 'expires',
- 'cache-control',
- 'content-disposition',
- 'content-encoding',
- 'x-cos-acl',
- 'x-cos-version-id',
- 'x-cos-request-id',
- 'x-cos-delete-marker',
- 'x-cos-server-side-encryption',
- 'x-cos-storage-class',
- 'x-cos-acl',
- 'x-cos-meta-test',
- 'x-cos-tagging-count',
- ],
- "MaxAgeSeconds": "5"
- }];
- var CORSRulesMulti = [{
- "AllowedOrigins": ["*"],
- "AllowedMethods": ["GET", "POST", "PUT", "DELETE", "HEAD"],
- "AllowedHeaders": ["*"],
- "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"],
- "MaxAgeSeconds": "5"
- }, {
- "AllowedOrigins": ["http://qq.com", "http://qcloud.com"],
- "AllowedMethods": ["GET", "POST", "PUT", "DELETE", "HEAD"],
- "AllowedHeaders": ["*"],
- "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"],
- "MaxAgeSeconds": "5"
- }];
- test('putBucketCors() old CORSConfiguration', function (done, assert) {
- CORSRules[0].AllowedHeaders[1] = 'test-' + Date.now().toString(36);
- cos.putBucketCors({
- Bucket: config.Bucket,
- Region: config.Region,
- CORSConfiguration: {
- CORSRules: CORSRules
- }
- }, function (err, data) {
- assert.ok(!err);
- setTimeout(function () {
- cos.getBucketCors({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(comparePlainObject(CORSRules, data.CORSRules));
- done();
- });
- }, 2000);
- });
- });
- test('putBucketCors() multi', function (done, assert) {
- cos.putBucketCors({
- Bucket: config.Bucket,
- Region: config.Region,
- CORSConfiguration: {
- CORSRules: CORSRulesMulti
- }
- }, function (err, data) {
- assert.ok(!err);
- setTimeout(function () {
- cos.getBucketCors({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(comparePlainObject(CORSRulesMulti, data.CORSRules));
- done();
- });
- }, 2000);
- });
- });
- test('putBucketCors() old CORSRules', function (done, assert) {
- CORSRules[0].AllowedHeaders[1] = 'test-' + Date.now().toString(36);
- cos.putBucketCors({
- Bucket: config.Bucket,
- Region: config.Region,
- CORSRules: CORSRules
- }, function (err, data) {
- assert.ok(!err);
- setTimeout(function () {
- cos.getBucketCors({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(comparePlainObject(CORSRules, data.CORSRules));
- done();
- });
- }, 2000);
- });
- });
- test('putBucketCors(),getBucketCors()', function (done, assert) {
- CORSRules[0].AllowedHeaders = ['*'];
- cos.putBucketCors({
- Bucket: config.Bucket,
- Region: config.Region,
- CORSConfiguration: {
- CORSRules: CORSRules
- }
- }, function (err, data) {
- assert.ok(!err);
- setTimeout(function () {
- cos.getBucketCors({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(comparePlainObject(CORSRules, data.CORSRules));
- done();
- });
- }, 2000);
- });
- });
- });
- group('BucketTagging', function () {
- var Tags = [
- {Key: "k1", Value: "v1"}
- ];
- var TagsMulti = [
- {Key: "k1", Value: "v1"},
- {Key: "k2", Value: "v2"},
- ];
- test('putBucketTagging(),getBucketTagging()', function (done, assert) {
- Tags[0].Value = Date.now().toString(36);
- cos.putBucketTagging({
- Bucket: config.Bucket,
- Region: config.Region,
- Tagging: {
- Tags: Tags
- }
- }, function (err, data) {
- assert.ok(!err);
- setTimeout(function () {
- cos.getBucketTagging({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(comparePlainObject(Tags, data.Tags));
- done();
- });
- }, 1000);
- });
- });
- test('deleteBucketTagging()', function (done, assert) {
- cos.deleteBucketTagging({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(!err);
- setTimeout(function () {
- cos.getBucketTagging({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(comparePlainObject([], data.Tags));
- done();
- });
- }, 1000);
- });
- });
- test('putBucketTagging() multi', function (done, assert) {
- Tags[0].Value = Date.now().toString(36);
- cos.putBucketTagging({
- Bucket: config.Bucket,
- Region: config.Region,
- Tagging: {
- Tags: TagsMulti
- }
- }, function (err, data) {
- assert.ok(!err);
- setTimeout(function () {
- cos.getBucketTagging({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(comparePlainObject(TagsMulti, data.Tags));
- done();
- });
- }, 1000);
- });
- });
- });
- group('BucketPolicy', function () {
- var Prefix = Date.now().toString(36);
- var Policy = {
- "version": "2.0",
- "principal": {"qcs": ["qcs::cam::uin/10001:uin/10001"]}, // 这里的 10001 是 QQ 号
- "statement": [{
- "effect": "allow",
- "action": [
- "name/cos:GetBucket",
- "name/cos:PutObject",
- "name/cos:PostObject",
- "name/cos:PutObjectCopy",
- "name/cos:InitiateMultipartUpload",
- "name/cos:UploadPart",
- "name/cos:UploadPartCopy",
- "name/cos:CompleteMultipartUpload",
- "name/cos:AbortMultipartUpload",
- "name/cos:AppendObject"
- ],
- "resource": ["qcs::cos:" + config.Region + ":uid/" + AppId + ":" + BucketLongName + "//" + AppId + "/" + BucketShortName + "/" + Prefix + "/*"] // 1250000000 是 appid
- }]
- };
- var getRes = function (s) {
- var t = s && s[0];
- var res = t && t.resource && t.resource[0];
- return res;
- };
- test('putBucketPolicy(),getBucketPolicy()', function (done, assert) {
- cos.putBucketPolicy({
- Bucket: config.Bucket,
- Region: config.Region,
- Policy: Policy
- }, function (err, data) {
- assert.ok(!err);
- cos.getBucketPolicy({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(getRes(Policy.statement) === getRes(data.Policy.Statement));
- done();
- });
- });
- });
- test('putBucketPolicy() s3', function (done, assert) {
- cos.putBucketPolicy({
- Bucket: config.Bucket,
- Region: config.Region,
- Policy: JSON.stringify(Policy)
- }, function (err, data) {
- assert.ok(!err);
- cos.getBucketPolicy({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(getRes(Policy.statement) === getRes(data.Policy.Statement));
- done();
- });
- });
- });
- test('deleteBucketPolicy()', function (done, assert) {
- cos.deleteBucketPolicy({
- Bucket: config.Bucket,
- Region: config.Region,
- Policy: JSON.stringify(Policy)
- }, function (err, data) {
- assert.ok(!err);
- cos.getBucketPolicy({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(err.ErrorStatus === 'Policy Not Found');
- done();
- });
- });
- });
- });
- group('BucketLocation', function () {
- test('getBucketLocation()', function (done, assert) {
- cos.getBucketLocation({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- var map1 = {
- 'tianjin': 'ap-beijing-1',
- 'cn-south-2': 'ap-guangzhou-2',
- 'cn-south': 'ap-guangzhou',
- 'cn-east': 'ap-shanghai',
- 'cn-southwest': 'ap-chengdu',
- };
- var map2 = {
- 'ap-beijing-1': 'tianjin',
- 'ap-guangzhou-2': 'cn-south-2',
- 'ap-guangzhou': 'cn-south',
- 'ap-shanghai': 'cn-east',
- 'ap-chengdu': 'cn-southwest',
- };
- assert.ok(data.LocationConstraint === config.Region || data.LocationConstraint === map1[config.Region] ||
- data.LocationConstraint === map2[config.Region]);
- done();
- });
- });
- });
- group('BucketLifecycle', function () {
- var Rules = [{
- 'ID': '1',
- 'Filter': {
- 'Prefix': 'test_' + Date.now().toString(36),
- },
- 'Status': 'Enabled',
- 'Transition': {
- 'Date': '2018-07-29T16:00:00.000Z',
- 'StorageClass': 'STANDARD_IA'
- }
- }];
- var RulesMulti = [{
- 'ID': '1',
- 'Filter': {
- 'Prefix': 'test1_' + Date.now().toString(36),
- },
- 'Status': 'Enabled',
- 'Transition': {
- 'Date': '2018-07-29T16:00:00.000Z',
- 'StorageClass': 'STANDARD_IA'
- }
- }, {
- 'ID': '2',
- 'Filter': {
- 'Prefix': 'test2_' + Date.now().toString(36),
- },
- 'Status': 'Enabled',
- 'Transition': {
- 'Date': '2018-07-29T16:00:00.000Z',
- 'StorageClass': 'STANDARD_IA'
- }
- }];
- test('deleteBucketLifecycle()', function (done, assert) {
- cos.deleteBucketLifecycle({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(!err);
- setTimeout(function () {
- cos.getBucketLifecycle({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(comparePlainObject([], data.Rules));
- done();
- });
- }, 2000);
- });
- });
- test('putBucketLifecycle(),getBucketLifecycle()', function (done, assert) {
- Rules[0].Filter.Prefix = 'test_' + Date.now().toString(36);
- cos.putBucketLifecycle({
- Bucket: config.Bucket,
- Region: config.Region,
- LifecycleConfiguration: {
- Rules: Rules
- }
- }, function (err, data) {
- assert.ok(!err);
- setTimeout(function () {
- cos.getBucketLifecycle({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(comparePlainObject(Rules, data && data.Rules));
- done();
- });
- }, 2000);
- });
- });
- test('putBucketLifecycle() multi', function (done, assert) {
- Rules[0].Filter.Prefix = 'test_' + Date.now().toString(36);
- cos.putBucketLifecycle({
- Bucket: config.Bucket,
- Region: config.Region,
- LifecycleConfiguration: {
- Rules: RulesMulti
- }
- }, function (err, data) {
- assert.ok(!err);
- setTimeout(function () {
- cos.getBucketLifecycle({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(comparePlainObject(RulesMulti, data.Rules));
- done();
- });
- }, 2000);
- });
- });
- });
- group('BucketWebsite', function () {
- var RoutingRules = [{
- Condition: {
- HttpErrorCodeReturnedEquals: "404"
- },
- Redirect: {
- Protocol: "https",
- ReplaceKeyWith: "404.html"
- }
- }, {
- Condition: {
- KeyPrefixEquals: "docs/"
- },
- Redirect: {
- Protocol: "https",
- ReplaceKeyPrefixWith: "documents/"
- }
- }, {
- Condition: {
- KeyPrefixEquals: "img/"
- },
- Redirect: {
- Protocol: "https",
- ReplaceKeyWith: "picture.jpg"
- }
- }];
- var WebsiteConfiguration = {
- IndexDocument: {
- Suffix: "index.html"
- },
- RedirectAllRequestsTo: {
- Protocol: "https"
- },
- ErrorDocument: {
- Key: "error.html"
- },
- };
- test('putBucketWebsite(),getBucketWebsite()', function (done, assert) {
- cos.putBucketWebsite({
- Bucket: config.Bucket,
- Region: config.Region,
- WebsiteConfiguration: WebsiteConfiguration
- }, function (err, data) {
- assert.ok(!err);
- setTimeout(function () {
- cos.getBucketWebsite({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(comparePlainObject(WebsiteConfiguration, data.WebsiteConfiguration));
- done();
- });
- }, 2000);
- });
- });
- test('putBucketWebsite() multi RoutingRules', function (done, assert) {
- WebsiteConfiguration.RoutingRules = RoutingRules;
- cos.putBucketWebsite({
- Bucket: config.Bucket,
- Region: config.Region,
- WebsiteConfiguration: WebsiteConfiguration
- }, function (err, data) {
- assert.ok(!err);
- setTimeout(function () {
- cos.getBucketWebsite({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(comparePlainObject(WebsiteConfiguration, data.WebsiteConfiguration));
- done();
- });
- }, 2000);
- });
- });
- test('deleteBucketWebsite()', function (done, assert) {
- cos.deleteBucketWebsite({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(!err);
- setTimeout(function () {
- cos.getBucketWebsite({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(comparePlainObject({}, data.WebsiteConfiguration));
- done();
- });
- }, 2000);
- });
- });
- });
- group('BucketDomain', function () {
- var DomainRule = [{
- Status: "DISABLED",
- Name: "www.testDomain1.com",
- Type: "REST"
- },
- {
- Status: "DISABLED",
- Name: "www.testDomain2.com",
- Type: "WEBSITE"
- }];
- test('putBucketDomain(),getBucketDomain()', function (done, assert) {
- cos.putBucketDomain({
- Bucket: config.Bucket,
- Region: config.Region,
- DomainRule: DomainRule
- }, function (err, data) {
- assert.ok(!err);
- setTimeout(function () {
- cos.getBucketDomain({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(comparePlainObject(DomainRule, data.DomainRule));
- done();
- });
- }, 2000);
- });
- });
- // test('putBucketDomain() multi', function (done, assert) {
- // cos.putBucketDomain({
- // Bucket: config.Bucket,
- // Region: config.Region,
- // DomainRule: DomainRuleMulti
- // }, function (err, data) {
- // assert.ok(!err);
- // setTimeout(function () {
- // cos.getBucketDomain({
- // Bucket: config.Bucket,
- // Region: config.Region
- // }, function (err, data) {
- // assert.ok(comparePlainObject(DomainRuleMulti, data.DomainRule));
- // done();
- // });
- // }, 2000);
- // });
- // });
- test('deleteBucketDomain()', function (done, assert) {
- cos.deleteBucketDomain({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(!err);
- setTimeout(function () {
- cos.getBucketDomain({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(comparePlainObject([], data.DomainRule));
- done();
- });
- }, 2000);
- });
- });
- });
- group('params check Region', function () {
- test('params check', function (done, assert) {
- cos.headBucket({
- Bucket: config.Bucket,
- Region: 'cos.ap-guangzhou'
- }, function (err, data) {
- assert.ok(err.message === 'param Region should not be start with "cos."');
- done();
- });
- });
- test('params check Region', function (done, assert) {
- cos.headBucket({
- Bucket: config.Bucket,
- Region: 'gz'
- }, function (err, data) {
- assert.ok(err);
- done();
- });
- });
- });
- group('Key 特殊字符处理', function () {
- test('Key 特殊字符处理', function (done, assert) {
- var Key = '中文→↓←→↖↗↙↘! $&\'()+,-.0123456789=@ABCDEFGHIJKLMNOPQRSTUV?WXYZ[]^_`abcdefghijklmnopqrstuvwxyz{}~.jpg';
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- Body: 'hello',
- }, function (err, data) {
- assert.ok(!err);
- cos.deleteObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- Body: 'hello',
- }, function (err, data) {
- assert.ok(!err);
- cos.deleteMultipleObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Objects: {
- Key: Key,
- },
- }, function (err, data) {
- assert.ok(!err);
- done();
- });
- });
- });
- });
- });
- group('Bucket 格式有误', function () {
- test('Bucket 带有中文', function (done, assert) {
- cos.headBucket({
- Bucket: '中文-1250000000',
- Region: config.Region,
- }, function (err, data) {
- assert.ok(err && err.message === 'Bucket should format as "test-1250000000".');
- done();
- });
- });
- test('Bucket 带有 /', function (done, assert) {
- cos.headBucket({
- Bucket: 'te/st-1250000000',
- Region: config.Region,
- }, function (err, data) {
- assert.ok(err && err.message === 'Bucket should format as "test-1250000000".');
- done();
- });
- });
- test('Bucket 带有 .', function (done, assert) {
- cos.headBucket({
- Bucket: 'te.st-1250000000',
- Region: config.Region,
- }, function (err, data) {
- assert.ok(err && err.message === 'Bucket should format as "test-1250000000".');
- done();
- });
- });
- test('Bucket 带有 :', function (done, assert) {
- cos.headBucket({
- Bucket: 'te:st-1250000000',
- Region: config.Region,
- }, function (err, data) {
- assert.ok(err && err.message === 'Bucket should format as "test-1250000000".');
- done();
- });
- });
- });
- group('Region 格式有误', function () {
- test('Region 带有中文', function (done, assert) {
- cos.headBucket({
- Bucket: 'test-1250000000',
- Region: '中文',
- }, function (err, data) {
- assert.ok(err && err.message === 'Region format error.');
- done();
- });
- });
- test('Region 带有 /', function (done, assert) {
- cos.headBucket({
- Bucket: 'test-1250000000',
- Region: 'test/',
- }, function (err, data) {
- assert.ok(err && err.message === 'Region format error.');
- done();
- });
- });
- test('Region 带有 :', function (done, assert) {
- cos.headBucket({
- Bucket: 'test-1250000000',
- Region: 'test:',
- }, function (err, data) {
- assert.ok(err && err.message === 'Region format error.');
- done();
- });
- });
- });
- group('复制文件', function () {
- test('sliceCopyFile() 正常分片复制', function (done, assert) {
- var filename = '10m.zip';
- var Key = '10mb.copy.zip';
- var blob = util.createFile({size: 1024 * 1024 * 10});
- var lastPercent;
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- Body: blob,
- }, function (err, data) {
- cos.sliceCopyFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- CopySource: config.Bucket + '.cos.' + config.Region + '.myqcloud.com/' + filename,
- SliceSize: 5 * 1024 * 1024,
- onProgress: function (info) {
- lastPercent = info.percent;
- }
- }, function (err, data) {
- assert.ok(data && data.ETag, '成功进行分片复制');
- done();
- });
- });
- });
- test('sliceCopyFile() 单片复制', function (done, assert) {
- var filename = '10m.zip';
- var Key = '10mb.copy.zip';
- cos.sliceCopyFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- CopySource: config.Bucket + '.cos.' + config.Region + '.myqcloud.com/' + filename,
- SliceSize: 10 * 1024 * 1024,
- }, function (err, data) {
- if (err) throw err;
- assert.ok(data && data.ETag, '成功进行单片复制');
- done();
- });
- });
- });
- group('putObject 中文 Content-MD5', function () {
- var fileBlob = dataURItoUploadBody('data:text/plain;base64,5Lit5paH');
- // 这里两个用户正式测试的时候需要给 putObject 计算并加上 Content-MD5 字段
- test('putObject 中文文件内容 带 Content-MD5', function (done, assert) {
- var Key = '中文.txt';
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- Body: fileBlob,
- }, function (err, data) {
- assert.ok(data && data.ETag, '成功进行上传');
- done();
- });
- });
- test('putObject 中文字符串 带 Content-MD5', function (done, assert) {
- var Key = '中文.txt';
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- Body: '中文',
- }, function (err, data) {
- assert.ok(data && data.ETag, '成功进行上传');
- done();
- });
- });
- });
- group('deleteMultipleObject Key 带中文字符', function () {
- test('deleteMultipleObject Key 带中文字符', function (done, assert) {
- cos.deleteMultipleObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Objects: [
- {Key: '中文/中文.txt'},
- {Key: '中文/中文.zip', VersionId: 'MTg0NDY3NDI1MzM4NzM0ODA2MTI'},
- {Key: unescape(encodeURIComponent('中文'))},
- {Key: unescape('%e8%af%b4%2e%70%72%70%72')},
- ]
- }, function (err, data) {
- assert.ok(!err, '成功进行批量删除');
- done();
- });
- });
- });
- group('upload Content-Type', function () {
- // putObject
- test('putObject empty string Content-Type null -> text/plain', function (done, assert) {
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1',
- Body: '',
- }, function (err, data) {
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1',
- }, function (err, data) {
- assert.ok(data.headers['content-type'] === 'text/plain', 'Content-Type 正确');
- done();
- });
- });
- });
- test('putObject string Content-Type null -> text/plain', function (done, assert) {
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.zip',
- Body: '12345',
- }, function (err, data) {
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.zip',
- }, function (err, data) {
- assert.ok(data.headers['content-type'] === 'text/plain', 'Content-Type 正确');
- done();
- });
- });
- });
- test('putObject string Content-Type text/xml -> text/xml', function (done, assert) {
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.zip',
- ContentType: 'text/xml',
- Body: util.createFile({size: 1, type: 'text/html'}),
- }, function (err, data) {
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.zip',
- }, function (err, data) {
- assert.ok(data.headers['content-type'] === 'text/xml', 'Content-Type 正确');
- done();
- });
- });
- });
- test('putObject blob Content-Type text/xml -> text/xml', function (done, assert) {
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.zip',
- ContentType: 'text/xml',
- Body: util.createFile({size: 1, type: 'text/html'}),
- }, function (err, data) {
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.zip',
- }, function (err, data) {
- assert.ok(data.headers['content-type'] === 'text/xml', 'Content-Type 正确');
- done();
- });
- });
- });
- test('putObject blob Content-Type text/html -> text/html', function (done, assert) {
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.zip',
- Body: util.createFile({size: 1, type: 'text/html'}),
- }, function (err, data) {
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.zip',
- }, function (err, data) {
- assert.ok(data.headers['content-type'] === 'text/html', 'Content-Type 正确');
- done();
- });
- });
- });
- test('putObject blob Content-Type null -> application/zip or application/octet-stream', function (done, assert) {
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.zip',
- Body: util.createFile({size: 1}),
- }, function (err, data) {
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.zip',
- }, function (err, data) {
- assert.ok(data.headers['content-type'] === 'application/zip', 'Content-Type 正确');
- done();
- });
- });
- });
- test('putObject blob Content-Type null application/octet-stream', function (done, assert) {
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1',
- Body: util.createFile({size: 1}),
- }, function (err, data) {
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1',
- }, function (err, data) {
- assert.ok(data.headers['content-type'] === 'application/octet-stream', 'Content-Type 正确');
- done();
- });
- });
- });
- test('putObject empty blob Content-Type null application/octet-stream', function (done, assert) {
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1',
- Body: util.createFile({size: 0}),
- }, function (err, data) {
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1',
- }, function (err, data) {
- assert.ok(data.headers['content-type'] === 'application/octet-stream', 'Content-Type 正确');
- done();
- });
- });
- });
- // sliceUploadFile
- test('sliceUploadFile string Content-Type null -> text/plain', function (done, assert) {
- cos.sliceUploadFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.zip',
- Body: '12345',
- }, function (err, data) {
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.zip',
- }, function (err, data) {
- assert.ok(data.headers['content-type'] === 'text/plain', 'Content-Type 正确');
- done();
- });
- });
- });
- test('sliceUploadFile string Content-Type text/xml -> text/xml', function (done, assert) {
- cos.sliceUploadFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.zip',
- ContentType: 'text/xml',
- Body: util.createFile({size: 1, type: 'text/html'}),
- }, function (err, data) {
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.zip',
- }, function (err, data) {
- assert.ok(data.headers['content-type'] === 'text/xml', 'Content-Type 正确');
- done();
- });
- });
- });
- test('sliceUploadFile blob Content-Type text/xml -> text/xml', function (done, assert) {
- cos.sliceUploadFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.zip',
- ContentType: 'text/xml',
- Body: util.createFile({size: 1, type: 'text/html'}),
- }, function (err, data) {
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.zip',
- }, function (err, data) {
- assert.ok(data.headers['content-type'] === 'text/xml', 'Content-Type 正确');
- done();
- });
- });
- });
- test('sliceUploadFile blob Content-Type text/html -> text/html', function (done, assert) {
- cos.sliceUploadFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.zip',
- Body: util.createFile({size: 1, type: 'text/html'}),
- }, function (err, data) {
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.zip',
- }, function (err, data) {
- assert.ok(data.headers['content-type'] === 'text/html', 'Content-Type 正确');
- done();
- });
- });
- });
- test('sliceUploadFile blob Content-Type null -> application/zip or application/octet-stream', function (done, assert) {
- cos.sliceUploadFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.zip',
- Body: util.createFile({size: 1}),
- }, function (err, data) {
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.zip',
- }, function (err, data) {
- var userAgent = navigator.userAgent || '';
- var m = userAgent.match(/ TBS\/(\d{6}) /);
- if (location.protocol === 'http:' && m && m[1].length <= 6 && m[1] < '044429') {
- assert.ok(data.headers['content-type'] === 'application/octet-stream', 'Content-Type 正确');
- } else {
- assert.ok(data.headers['content-type'] === 'application/zip', 'Content-Type 正确');
- }
- done();
- });
- });
- });
- test('sliceUploadFile blob Content-Type null application/octet-stream', function (done, assert) {
- cos.sliceUploadFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1',
- Body: util.createFile({size: 1}),
- }, function (err, data) {
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1',
- }, function (err, data) {
- assert.ok(data.headers['content-type'] === 'application/octet-stream', 'Content-Type 正确');
- done();
- });
- });
- });
- });
- group('Cache-Control', function (val) {
- var isNormalCacheControl = function (val) {
- return val === undefined
- || val === 'no-cache'
- || val === 'max-age=259200'
- // || val === 'no-cache, max-age=259200' // IE 10
- // || val === 'no-cache, max-age=7200' // firefox
- };
- // putObject
- test('putObject Cache-Control: null -> Cache-Control: null or max-age=259200', function (done, assert) {
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1mb.zip',
- Body: '',
- }, function (err, data) {
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1mb.zip',
- }, function (err, data) {
- assert.ok(isNormalCacheControl(data.headers['cache-control']), 'cache-control 正确');
- done();
- });
- });
- });
- test('putObject Cache-Control: max-age=7200 -> Cache-Control: max-age=7200', function (done, assert) {
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1mb.zip',
- Body: '',
- CacheControl: 'max-age=7200',
- }, function (err, data) {
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1mb.zip',
- }, function (err, data) {
- assert.ok(data.headers['cache-control'] === 'max-age=7200', 'cache-control 正确');
- done();
- });
- });
- });
- test('putObject Cache-Control: no-cache -> Cache-Control: no-cache', function (done, assert) {
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1mb.zip',
- Body: '',
- CacheControl: 'no-cache',
- }, function (err, data) {
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1mb.zip',
- }, function (err, data) {
- assert.ok(data.headers['cache-control'] === 'no-cache' || data.headers['cache-control'] === 'no-cache, max-age=259200', 'cache-control 正确');
- done();
- });
- });
- });
- // sliceUploadFile
- test('sliceUploadFile Cache-Control: null -> Cache-Control: null or max-age=259200', function (done, assert) {
- cos.sliceUploadFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1mb.zip',
- Body: '',
- }, function (err, data) {
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1mb.zip',
- }, function (err, data) {
- assert.ok(data.headers['cache-control'] === undefined || data.headers['cache-control'] === 'max-age=259200' || data.headers['cache-control'] === 'no-cache, max-age=259200', 'cache-control 正确');
- done();
- });
- });
- });
- test('sliceUploadFile Cache-Control: max-age=7200 -> Cache-Control: max-age=7200', function (done, assert) {
- cos.sliceUploadFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1mb.zip',
- Body: '',
- CacheControl: 'max-age=7200',
- }, function (err, data) {
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1mb.zip',
- }, function (err, data) {
- assert.ok(data.headers['cache-control'] === 'max-age=7200', 'cache-control 正确');
- done();
- });
- });
- });
- test('sliceUploadFile Cache-Control: no-cache -> Cache-Control: no-cache', function (done, assert) {
- cos.sliceUploadFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1mb.zip',
- Body: '',
- CacheControl: 'no-cache',
- }, function (err, data) {
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1mb.zip',
- }, function (err, data) {
- assert.ok(data.headers['cache-control'] === 'no-cache' || data.headers['cache-control'] === 'no-cache, max-age=259200', 'cache-control 正确');
- done();
- });
- });
- });
- });
- group('BucketLogging', function () {
- var TargetBucket = config.Bucket;
- var TargetPrefix = 'bucket-logging-prefix' + Date.now().toString(36) + '/';
- var BucketLoggingStatus = {
- LoggingEnabled: {
- TargetBucket: TargetBucket,
- TargetPrefix: TargetPrefix
- }
- };
- test('putBucketLogging(), getBucketLogging()', function (done, assert) {
- cos.putBucketLogging({
- Bucket: config.Bucket,
- Region: config.Region,
- BucketLoggingStatus: BucketLoggingStatus
- }, function (err, data) {
- assert.ok(!err);
- cos.getBucketLogging({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(comparePlainObject(BucketLoggingStatus, data.BucketLoggingStatus));
- done();
- });
- });
- });
- test('putBucketLogging() 删除 logging 配置', function (done, assert) {
- cos.putBucketLogging({
- Bucket: config.Bucket,
- Region: config.Region,
- BucketLoggingStatus: ''
- }, function (err, data) {
- assert.ok(!err);
- cos.getBucketLogging({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(data.BucketLoggingStatus === '');
- done();
- });
- });
- });
- });
- group('BucketInventory', function () {
- var TargetBucket = config.Bucket;
- var InventoryConfiguration = {
- Id: 'inventory_test',
- IsEnabled: 'true',
- Destination: {
- COSBucketDestination: {
- Format: 'CSV',
- AccountId: config.Uin,
- Bucket: 'qcs::cos:' + config.Region + '::' + TargetBucket,
- Prefix: 'inventory_prefix_1',
- Encryption: {
- SSECOS: ''
- }
- }
- },
- Schedule: {
- Frequency: 'Daily'
- },
- Filter: {
- Prefix: 'myPrefix'
- },
- IncludedObjectVersions: 'All',
- OptionalFields: [
- 'Size'
- ]
- };
- var InventoryConfigurationNoEncryption = {
- Id: 'inventory_test',
- IsEnabled: 'true',
- Destination: {
- COSBucketDestination: {
- Format: 'CSV',
- AccountId: config.Uin,
- Bucket: 'qcs::cos:' + config.Region + '::' + TargetBucket,
- Prefix: 'inventory_prefix_1'
- }
- },
- Schedule: {
- Frequency: 'Daily'
- },
- Filter: {
- Prefix: 'myPrefix'
- },
- IncludedObjectVersions: 'All',
- OptionalFields: [
- 'Size'
- ]
- };
- test('putBucketInventory(), getBucketInventory()', function (done, assert) {
- cos.putBucketInventory({
- Bucket: config.Bucket,
- Region: config.Region,
- Id: InventoryConfiguration.Id,
- InventoryConfiguration: InventoryConfiguration
- }, function (err, data) {
- assert.ok(!err);
- cos.getBucketInventory({
- Bucket: config.Bucket,
- Region: config.Region,
- Id: InventoryConfiguration.Id
- }, function (err, data) {
- assert.ok(comparePlainObject(InventoryConfiguration, data.InventoryConfiguration));
- done();
- });
- });
- });
- test('listBucketInventory()', function (done, assert) {
- cos.listBucketInventory({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- var targetInventory;
- data.InventoryConfigurations.forEach(function (item) {
- if (item.Id === InventoryConfiguration.Id) {
- targetInventory = item;
- }
- });
- assert.ok(comparePlainObject(InventoryConfiguration, targetInventory));
- assert.ok(data.IsTruncated === 'false' || data.IsTruncated === 'true');
- done();
- });
- });
- test('putBucketInventory() 不设置 SSECOS', function (done, assert) {
- cos.putBucketInventory({
- Bucket: config.Bucket,
- Region: config.Region,
- Id: InventoryConfigurationNoEncryption.Id,
- InventoryConfiguration: InventoryConfigurationNoEncryption
- }, function (err, data) {
- assert.ok(!err);
- cos.getBucketInventory({
- Bucket: config.Bucket,
- Region: config.Region,
- Id: InventoryConfigurationNoEncryption.Id
- }, function (err, data) {
- assert.ok(comparePlainObject(InventoryConfigurationNoEncryption, data.InventoryConfiguration));
- done();
- });
- });
- });
- test('deleteBucketInventory()', function (done, assert) {
- cos.deleteBucketInventory({
- Bucket: config.Bucket,
- Region: config.Region,
- Id: InventoryConfiguration.Id
- }, function (err, data) {
- assert.ok(!err);
- cos.getBucketInventory({
- Bucket: config.Bucket,
- Region: config.Region,
- Id: InventoryConfiguration.Id
- }, function (err, data) {
- assert.ok(err && err.statusCode === 404);
- done();
- });
- });
- });
- });
- var tagging2str = function (obj) {
- var arr = [];
- obj.forEach(function (v) {
- arr.push(v.Key + '=' + encodeURIComponent(v.Value));
- })
- return arr.join('&');
- }
- group('上传带 tagging', function () {
- var Tags = [
- {Key: "k1", Value: "v1"},
- {Key: "k2", Value: "v2"},
- ];
- var key = '1.txt';
- test('putObject 带 x-cos-tagging', function (done, assert) {
- Tags[0].Value = Date.now().toString(36);
- var tagStr = tagging2str(Tags);
- // 调用方法
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: key,
- Body: 'hello!',
- Headers: {
- 'x-cos-tagging': tagStr,
- },
- }, function (err1, data1) {
- cos.headObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: key,
- }, function (err2, data2) {
- var taggingCount = data2 && data2.headers['x-cos-tagging-count'];
- assert.ok(taggingCount === '2', '返回 x-cos-tagging-count: ' + taggingCount);
- cos.getObjectTagging({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: key,
- }, function (err3, data3) {
- assert.ok(comparePlainObject(Tags, data3.Tags));
- done();
- });
- });
- });
- });
- // test('sliceUploadFile 带 x-cos-tagging', function (done, assert) {
- // Tags[0].Value = Date.now().toString(36);
- // var tagStr = tagging2str(Tags);
- // // 调用方法
- // cos.sliceUploadFile({
- // Bucket: config.Bucket,
- // Region: config.Region,
- // Key: key,
- // Body: 'hello!',
- // Headers: {
- // 'x-cos-tagging': tagStr,
- // },
- // }, function (err1, data1) {
- // cos.headObject({
- // Bucket: config.Bucket,
- // Region: config.Region,
- // Key: key,
- // }, function (err2, data2) {
- // var taggingCount = data2 && data2.headers['x-cos-tagging-count'];
- // assert.ok(taggingCount === '1', '返回 x-cos-tagging-count: ' + taggingCount);
- // cos.getObjectTagging({
- // Bucket: config.Bucket,
- // Region: config.Region,
- // Key: key,
- // }, function (err3, data3) {
- // assert.ok(data3 && data3.Tags && comparePlainObject(Tags, data3.Tags));
- // done();
- // });
- // });
- // });
- // });
- });
- group('ObjectTagging', function () {
- var key = '1.txt';
- var Tags = [
- {Key: "k1", Value: "v1"},
- {Key: "k2", Value: "v2"},
- ];
- test('putObjectTagging(),getObjectTagging()', function (done, assert) {
- Tags[0].Value = Date.now().toString(36);
- cos.putObjectTagging({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: key,
- Tagging: {
- Tags: Tags
- },
- }, function (err, data) {
- assert.ok(!err);
- setTimeout(function () {
- cos.getObjectTagging({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: key,
- }, function (err, data) {
- assert.ok(comparePlainObject(Tags, data.Tags));
- done();
- });
- }, 1000);
- });
- });
- test('deleteObjectTagging()', function (done, assert) {
- cos.deleteObjectTagging({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: key,
- }, function (err, data) {
- assert.ok(!err);
- setTimeout(function () {
- cos.getObjectTagging({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: key,
- }, function (err, data) {
- assert.ok(comparePlainObject([], data.Tags));
- done();
- });
- }, 1000);
- });
- });
- });
- group('getBucketAccelerate', function () {
- test('putBucketAccelerate(),getBucketAccelerate() Enabled', function (done, assert) {
- cos.putBucketAccelerate({
- Bucket: config.Bucket,
- Region: config.Region,
- AccelerateConfiguration: {
- Status: 'Enabled', // Suspended、Enabled
- },
- }, function (err, data) {
- assert.ok(!err);
- setTimeout(function () {
- cos.getBucketAccelerate({
- Bucket: config.Bucket,
- Region: config.Region,
- }, function (err2, data2) {
- assert.ok(data2 && data2.AccelerateConfiguration && data2.AccelerateConfiguration.Status === 'Enabled');
- done();
- });
- }, 2000);
- });
- });
- test('putBucketAccelerate(),getBucketAccelerate() Suspended', function (done, assert) {
- cos.putBucketAccelerate({
- Bucket: config.Bucket,
- Region: config.Region,
- AccelerateConfiguration: {
- Status: 'Suspended', // Suspended、Enabled
- },
- }, function (err, data) {
- assert.ok(!err);
- setTimeout(function () {
- cos.getBucketAccelerate({
- Bucket: config.Bucket,
- Region: config.Region,
- }, function (err2, data2) {
- assert.ok(data2 && data2.AccelerateConfiguration && data2.AccelerateConfiguration.Status === 'Suspended');
- done();
- });
- }, 1000);
- });
- });
- });
- group('Promise', function () {
- test('headBucket callback', function (done, assert) {
- var res = cos.headBucket({
- Bucket: config.Bucket,
- Region: config.Region,
- }, function (err, data) {
- assert.ok(!err && data);
- done();
- });
- assert.ok(!res);
- });
- test('Promise() getObjectUrl', function (done, assert) {
- var res = cos.getObjectUrl({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '123.txt',
- });
- assert.ok(!res.then);
- done();
- });
- test('Promise() headBucket', function (done, assert) {
- cos.headBucket({
- Bucket: config.Bucket,
- Region: config.Region,
- }).then(function (data) {
- assert.ok(data);
- done();
- }).catch(function () {
- assert.ok(false);
- done();
- });
- });
- test('headBucket callback', function (done, assert) {
- var res = cos.headBucket({
- Bucket: config.Bucket,
- Region: config.Region,
- }, function (err, data) {
- assert.ok(!err && data);
- done();
- });
- assert.ok(!res);
- });
- test('Promise() headBucket error', function (done, assert) {
- cos.headBucket({
- Bucket: config.Bucket,
- Region: config.Region + '/',
- }).then(function (data) {
- assert.ok(!data);
- done();
- }).catch(function (err) {
- assert.ok(err && err.message === 'Region format error.');
- done();
- });
- });
- });
- group('Query 的键值带有特殊字符', function () {
- test('getAuth() 特殊字符', function (done, assert) {
- var content = Date.now().toString();
- var key = '1.txt';
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: key,
- Body: content,
- }, function (err, data) {
- var str = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM,./;\'[]\\-=0987654321`~!@#$%^&*()_+{}|":>?<';
- var qs = {};
- qs[str] = str;
- cos.options.getAuthorization({
- Method: 'get',
- Key: key,
- Scope: [{
- action: 'GetObject',
- bucket: config.Bucket,
- region: config.Region,
- prefix: key,
- }],
- Query: qs,
- }, function (AuthData) {
- if (typeof AuthData === 'string') {
- AuthData = {Authorization: AuthData};
- }
- if (!AuthData.Authorization) {
- AuthData.Authorization = COS.getAuthorization({
- SecretId: AuthData.TmpSecretId,
- SecretKey: AuthData.TmpSecretKey,
- Method: 'get',
- Key: key,
- SystemClockOffset: cos.options.SystemClockOffset,
- });
- }
- var link = 'http://' + config.Bucket + '.cos.' + config.Region + '.myqcloud.com' + '/' +
- camSafeUrlEncode(key).replace(/%2F/g, '/') + '?' + AuthData.Authorization +
- (AuthData.SecurityToken ? '&x-cos-security-token=' + AuthData.SecurityToken : '') +
- '&' + camSafeUrlEncode(str) + '=' + camSafeUrlEncode(str);
- request({
- method: 'GET',
- url: link,
- }, function (err, response, body) {
- assert.ok(response.statusCode === 200);
- assert.ok(body === content);
- done();
- });
- });
- });
- });
- test('getAuth() 特殊字符 ?sign=', function (done, assert) {
- var content = Date.now().toString();
- var key = '1.txt';
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: key,
- Body: content,
- }, function (err, data) {
- var str = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM,./;\'[]\\-=0987654321`~!@#$%^&*()_+{}|":>?<';
- var qs = {};
- qs[str] = str;
- cos.options.getAuthorization({
- Method: 'get',
- Key: key,
- Scope: [{
- action: 'GetObject',
- bucket: config.Bucket,
- region: config.Region,
- prefix: key,
- }],
- Query: qs,
- }, function (AuthData) {
- if (typeof AuthData === 'string') {
- AuthData = {Authorization: AuthData};
- }
- if (!AuthData.Authorization) {
- AuthData.Authorization = COS.getAuthorization({
- SecretId: AuthData.TmpSecretId,
- SecretKey: AuthData.TmpSecretKey,
- Method: 'get',
- Key: key,
- SystemClockOffset: cos.options.SystemClockOffset,
- });
- }
- var link = 'http://' + config.Bucket + '.cos.' + config.Region + '.myqcloud.com' + '/' +
- camSafeUrlEncode(key).replace(/%2F/g, '/') +
- '?sign=' + camSafeUrlEncode(AuthData.Authorization) +
- (AuthData.SecurityToken ? '&x-cos-security-token=' + AuthData.SecurityToken : '') +
- '&' + camSafeUrlEncode(str) + '=' + camSafeUrlEncode(str);
- request({
- method: 'GET',
- url: link,
- }, function (err, response, body) {
- assert.ok(response.statusCode === 200);
- assert.ok(body === content);
- done();
- });
- });
- });
- });
- });
- group('selectObjectContent(),selectObjectContentStream()', function () {
- var key = '1.json';
- var selectJsonOpt = {
- Bucket: config.Bucket,
- Region: config.Region,
- Key: key,
- SelectType: 2,
- SelectRequest: {
- Expression: "Select * from COSObject",
- ExpressionType: "SQL",
- InputSerialization: {JSON: {Type: "DOCUMENT",},},
- OutputSerialization: {JSON: {RecordDelimiter: "\n"},},
- RequestProgress: {Enabled: "FALSE"}
- },
- };
- test('selectObjectContent', function (done, assert) {
- var time = Date.now();
- var content = `{"a":123,"b":"中文${time}","c":{"d":456}}`;
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: key,
- Body: content,
- }, function (err, data) {
- cos.selectObjectContent(selectJsonOpt, function (err, data) {
- assert.ok(data.Payload === content + '\n');
- done();
- });
- });
- });
- test('selectObjectContent', function (done, assert) {
- var time = Date.now();
- var content = `{"a":123,"b":"${time}","c":{"d":456}`;
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: key,
- Body: content,
- }, function (err, data) {
- var bufList = [];
- cos.selectObjectContent(selectJsonOpt, function (err, data) {
- assert.ok(err);
- done();
- });
- });
- });
- });
- group('BucketReplication', function () {
- var prepared = false;
- var repBucket = config.Bucket.replace(/^(.*)(-\d+)$/, '$1-replication$2')
- var repBucketName = repBucket.replace(/(-\d+)$/, '')
- var repRegion = 'ap-chengdu';
- var prepareBucket = function (callback) {
- cos.putBucket({
- Bucket: repBucket,
- Region: repRegion,
- }, function (err, data) {
- cos.putBucketVersioning({
- Bucket: config.Bucket,
- Region: config.Region,
- VersioningConfiguration: {
- Status: 'Enabled'
- }
- }, function (err, data) {
- cos.putBucketVersioning({
- Bucket: repBucket,
- Region: repRegion,
- VersioningConfiguration: {
- Status: 'Enabled'
- }
- }, function (err, data) {
- prepared = true
- callback();
- });
- });
- });
- };
- test('putBucketReplication();getBucketReplication()', function (done, assert) {
- var ruleId = Date.now().toString(36);
- prepareBucket(function () {
- cos.putBucketReplication({
- Bucket: config.Bucket,
- Region: config.Region,
- ReplicationConfiguration: {
- Role: "qcs::cam::uin/10001:uin/10001",
- Rules: [{
- ID: ruleId,
- Status: "Enabled",
- Prefix: "sync/",
- Destination: {
- Bucket: `qcs:id/0:cos:${repRegion}:appid/${AppId}:${repBucketName}`,
- }
- }]
- }
- }, function (err, data) {
- assert.ok(!err);
- cos.getBucketReplication({
- Bucket: config.Bucket,
- Region: config.Region,
- }, function (err, data) {
- assert.ok(data.ReplicationConfiguration.Rules[0].ID === ruleId);
- done();
- });
- });
- });
- });
- test('deleteBucketReplication()', function (done, assert) {
- cos.deleteBucketReplication({
- Bucket: config.Bucket,
- Region: config.Region,
- VersioningConfiguration: {
- Status: 'Suspended'
- }
- }, function (err, data) {
- assert.ok(!err);
- setTimeout(function () {
- cos.getBucketReplication({
- Bucket: config.Bucket,
- Region: config.Region,
- }, function (err, data) {
- assert.ok(err && err.statusCode === 404);
- done();
- });
- }, 2000);
- });
- });
- });
- group('putBucketVersioning(),getBucketVersioning()', function () {
- test('Enabled', function (done, assert) {
- cos.deleteBucketReplication({
- Bucket: config.Bucket,
- Region: config.Region,
- VersioningConfiguration: {
- Status: "Enabled"
- }
- }, function (err, data) {
- cos.putBucketVersioning({
- Bucket: config.Bucket,
- Region: config.Region,
- VersioningConfiguration: {
- Status: "Enabled"
- }
- }, function (err, data) {
- setTimeout(function () {
- cos.getBucketVersioning({
- Bucket: config.Bucket,
- Region: config.Region,
- }, function (err, data) {
- assert.ok(data.VersioningConfiguration.Status === 'Enabled');
- done();
- });
- }, 2000);
- });
- });
- });
- test('Suspended', function (done, assert) {
- cos.putBucketVersioning({
- Bucket: config.Bucket,
- Region: config.Region,
- VersioningConfiguration: {
- Status: 'Suspended'
- }
- }, function (err, data) {
- assert.ok(!err);
- setTimeout(function () {
- cos.getBucketVersioning({
- Bucket: config.Bucket,
- Region: config.Region,
- }, function (err, data) {
- console.log(data.VersioningConfiguration.Status);
- assert.ok(data.VersioningConfiguration.Status === 'Suspended');
- done();
- });
- }, 2000);
- });
- });
- });
- group('BucketOrigin', function () {
- test('putBucketOrigin(),getBucketOrigin()', function (done, assert) {
- var prefix = Date.now().toString(36) + '/';
- cos.putBucketOrigin({
- Bucket: config.Bucket,
- Region: config.Region,
- OriginRule: [{
- OriginType: 'Mirror',
- OriginCondition: {HTTPStatusCode: 404, Prefix: ''},
- OriginParameter: {
- Protocol: 'HTTP',
- FollowQueryString: 'true',
- HttpHeader: {
- NewHttpHeader: {
- Header: [{
- Key: 'a',
- Value: 'a'
- }]
- }
- },
- FollowRedirection: 'true',
- HttpRedirectCode: ['301', '302']
- },
- OriginInfo: {
- HostInfo: {HostName: 'qq.com'},
- FileInfo: {
- PrefixConfiguration: {Prefix: prefix},
- SuffixConfiguration: {Suffix: '.jpg'}
- }
- },
- RulePriority: 1
- }]
- }, function (err, data) {
- assert.ok(!err);
- cos.getBucketOrigin({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(data.OriginRule[0].OriginInfo.FileInfo.PrefixConfiguration.Prefix === prefix);
- done();
- });
- });
- });
- test('deleteBucketOrigin()', function (done, assert) {
- cos.deleteBucketOrigin({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(!err);
- setTimeout(function () {
- cos.getBucketOrigin({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(err);
- done();
- });
- }, 2000);
- });
- });
- });
- group('BucketReferer', function () {
- test('putBucketReferer(),getBucketReferer()', function (done, assert) {
- var conf = {
- Status: 'Enabled',
- RefererType: 'White-List',
- DomainList: {
- Domains: [
- Date.now().toString(36) + '.qq.com',
- '*.qcloud.com',
- ]
- },
- EmptyReferConfiguration: 'Allow',
- };
- cos.putBucketReferer({
- Bucket: config.Bucket,
- Region: config.Region,
- RefererConfiguration: conf
- }, function (err, data) {
- assert.ok(!err);
- setTimeout(function () {
- cos.getBucketReferer({
- Bucket: config.Bucket,
- Region: config.Region
- }, function (err, data) {
- assert.ok(comparePlainObject(conf, data.RefererConfiguration));
- done();
- });
- }, 2000);
- });
- });
- });
- group('restoreObject()', function () {
- test('restoreObject()', function (done, assert) {
- cos.putObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.jpg',
- Body: '123',
- StorageClass: 'ARCHIVE'
- }, function (err, data) {
- assert.ok(!err);
- cos.restoreObject({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: '1.jpg',
- RestoreRequest: {
- Days: 1,
- CASJobParameters: {
- Tier: 'Expedited'
- }
- },
- }, function (err, data) {
- assert.ok(data && Math.floor(data.statusCode / 100) === 2);
- done();
- });
- });
- });
- });
- group('uploadFile()', function () {
- // 高级上传
- test('uploadFile() 高级上传', function (done, assert) {
- var filename = '3m.zip';
- var blob = util.createFile({size: 1024 * 1024 * 3});
- cos.uploadFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- Body: blob,
- }, function (err, data) {
- console.log(data);
- assert.ok(!err);
- done();
- });
- });
- test('uploadFile() 高级上传内容为空', function (done, assert) {
- var filename = '3m.zip';
- cos.uploadFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- Body: '',
- }, function (err, data) {
- assert.ok(!err);
- done();
- });
- });
- test('uploadFile() 高级上传 大于5mb则分块上传', function (done, assert) {
- var filename = '3m.zip';
- var blob = util.createFile({size: 1024 * 1024 * 3});
- cos.uploadFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- Body: blob,
- SliceSize: 1024 * 1024 * 5,
- }, function (err, data) {
- assert.ok(!err);
- done();
- });
- });
- });
- group('uploadFiles()', function () {
- test('uploadFiles()', function (done, assert) {
- var filename = '1.zip';
- cos.uploadFiles({
- files: [{
- Bucket: config.Bucket,
- Region: config.Region,
- Key: filename,
- Body: '123456',
- }],
- }, function (err, data) {
- assert.ok(!data.files.error);
- done();
- });
- });
- });
- group('multipartAbort()', function () {
- test('multipartAbort()', function (done, assert) {
- var Key = '1.jpg'
- cos.multipartInit({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- }, function (err, data) {
- assert.ok(!err);
- var UploadId = data.UploadId;
- cos.multipartAbort({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- UploadId: UploadId,
- }, function (err, data) {
- assert.ok(!err);
- cos.multipartListPart({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- UploadId: UploadId,
- }, function (err, data) {
- assert.ok(err);
- done();
- });
- });
- });
- });
- });
- group('sliceUploadFile() 续传', function () {
- test('multipartAbort()', function (done, assert) {
- var Key = '3.zip'
- cos.multipartInit({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- }, function (err, data) {
- assert.ok(!err);
- var UploadId = data.UploadId;
- cos.multipartUpload({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- UploadId: UploadId,
- PartNumber: 1,
- Body: util.createFile({size: 1024 * 1024}),
- }, function (err, data) {
- assert.ok(!err);
- cos.sliceUploadFile({
- Bucket: config.Bucket,
- Region: config.Region,
- Key: Key,
- Body: util.createFile({size: 1024 * 1024 * 3}),
- ChunkSize: 1024 * 1024,
- }, function (err, data) {
- assert.ok(data);
- done();
- });
- });
- });
- });
- });
- group('appendObject', function () {
- test('appendObject()', function (done, assert) {
- cos.headObject({
- Bucket: config.Bucket, // Bucket 格式:test-1250000000
- Region: config.Region,
- Key: 'append.txt', /* 必须 */
- }, function(err, data) {
- assert.ok(!err);
- if (err) return console.log(err);
- // 首先取到要追加的文件当前长度,即需要上送的Position
- var position = data.headers['content-length'];
- cos.appendObject({
- Bucket: config.Bucket, // Bucket 格式:test-1250000000
- Region: config.Region,
- Key: 'append.txt', /* 必须 */
- Body: '66666',
- Position: position,
- },
- function(err, data) {
- assert.ok(!err);
- done();
- })
- });
- });
- });
- group('数据万象', function () {
- test('describeMediaBuckets()', function (done, assert) {
- var host = 'ci.' + config.Region + '.myqcloud.com';
- var url = 'https://' + host + '/mediabucket';
- cos.request({
- Bucket: config.Bucket,
- Region: config.Region,
- Method: 'GET',
- Key: 'mediabucket', /** 固定值,必须 */
- Url: url,
- Query: {
- pageNumber: '1', /** 第几页,非必须 */
- pageSize: '10', /** 每页个数,非必须 */
- // regions: 'ap-chengdu', /** 地域信息,例如'ap-beijing',支持多个值用逗号分隔如'ap-shanghai,ap-beijing',非必须 */
- // bucketNames: 'test-1250000000', /** 存储桶名称,精确搜索,例如'test-1250000000',支持多个值用逗号分隔如'test1-1250000000,test2-1250000000',非必须 */
- // bucketName: 'test', /** 存储桶名称前缀,前缀搜索,例如'test',支持多个值用逗号分隔如'test1,test2',非必须 */
- }
- }, function (err, data) {
- assert.ok(!err);
- done();
- });
- });
- test('getMediaInfo()', function (done, assert) {
- cos.request({
- Bucket: config.Bucket,
- Region: config.Region,
- Method: 'GET',
- Key: 'test.mp4',
- Query: {
- 'ci-process': 'videoinfo' /** 固定值,必须 */
- }
- }, function (err, data) {
- assert.ok(!err);
- done();
- });
- });
- test('getSnapshot()', function (done, assert) {
- cos.request({
- Bucket: config.Bucket,
- Region: config.Region,
- Method: 'GET',
- Key: 'test.mp4',
- Query: {
- 'ci-process': 'snapshot', /** 固定值,必须 */
- time: 1, /** 截图的时间点,单位为秒,必须 */
- // width: 0, /** 截图的宽,非必须 */
- // height: 0, /** 截图的高,非必须 */
- // format: 'jpg', /** 截图的格式,支持 jpg 和 png,默认 jpg,非必须 */
- // rotate: 'auto', /** 图片旋转方式,默认为'auto',非必须 */
- // mode: 'exactframe', /** 截帧方式,默认为'exactframe',非必须 */
- },
- RawBody: true,
- },
- function(err, data){
- assert.ok(!err);
- done();
- });
- });
- });
|