| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185 |
- // Copyright (c) 2017, Tencent Inc.
- // All rights reserved.
- //
- // Author: sevenyou <sevenyou@tencent.com>
- // Created: 07/18/17
- // Description:
- #include <stdlib.h>
- #include <sys/stat.h>
- #include <iostream>
- #include <map>
- #include <string>
- #include <thread>
- #include <vector>
- #include "cos_api.h"
- #include "cos_sys_config.h"
- #include "util/auth_tool.h"
- #include "Poco/TaskManager.h"
- using namespace qcloud_cos;
- void PrintResult(const qcloud_cos::CosResult& result,
- const qcloud_cos::BaseResp& resp) {
- if (result.IsSucc()) {
- std::cout << resp.DebugString() << std::endl;
- } else {
- std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
- std::cout << "HttpStatus=" << result.GetHttpStatus() << std::endl;
- std::cout << "ErrorCode=" << result.GetErrorCode() << std::endl;
- std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
- std::cout << "ResourceAddr=" << result.GetResourceAddr() << std::endl;
- std::cout << "XCosRequestId=" << result.GetXCosRequestId() << std::endl;
- std::cout << "XCosTraceId=" << result.GetXCosTraceId() << std::endl;
- }
- }
- void GetService(qcloud_cos::CosAPI& cos) {
- qcloud_cos::GetServiceReq req;
- qcloud_cos::GetServiceResp resp;
- req.AddParam("max-keys", "5");
- qcloud_cos::CosResult result = cos.GetService(req, &resp);
- std::cout << "===================GetService====================="
- << std::endl;
- PrintResult(result, resp);
- const qcloud_cos::Owner& owner = resp.GetOwner();
- const std::vector<qcloud_cos::Bucket>& buckets = resp.GetBuckets();
- std::cout << "owner.m_id=" << owner.m_id
- << ", owner.display_name=" << owner.m_display_name << std::endl;
- for (std::vector<qcloud_cos::Bucket>::const_iterator itr = buckets.begin();
- itr != buckets.end(); ++itr) {
- const qcloud_cos::Bucket& bucket = *itr;
- std::cout << "Bucket name=" << bucket.m_name
- << ", location=" << bucket.m_location
- << ", create_date=" << bucket.m_create_date << std::endl;
- }
- std::cout << "========================================================="
- << std::endl;
- }
- void HeadBucket(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
- qcloud_cos::HeadBucketReq req(bucket_name);
- qcloud_cos::HeadBucketResp resp;
- qcloud_cos::CosResult result = cos.HeadBucket(req, &resp);
- std::cout << "===================HeadBucketResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "=========================================================="
- << std::endl;
- }
- void DeleteBucket(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
- qcloud_cos::DeleteBucketReq req(bucket_name);
- qcloud_cos::DeleteBucketResp resp;
- qcloud_cos::CosResult result = cos.DeleteBucket(req, &resp);
- std::cout << "===================DeleteBucketResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================="
- << std::endl;
- }
- void PutBucket(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
- qcloud_cos::PutBucketReq req(bucket_name);
- //创建MAZ存储桶使用下方进行设置
- //req.SetMAZBucket();
- qcloud_cos::PutBucketResp resp;
- qcloud_cos::CosResult result = cos.PutBucket(req, &resp);
- std::cout << "===================PutBucketResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================="
- << std::endl;
- }
- void GetBucket(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
- qcloud_cos::GetBucketReq req(bucket_name);
- qcloud_cos::GetBucketResp resp;
- qcloud_cos::CosResult result = cos.GetBucket(req, &resp);
- std::cout << "===================GetBucketResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================="
- << std::endl;
- }
- void PutBucketVersioning(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- qcloud_cos::PutBucketVersioningReq req(bucket_name);
- req.SetStatus(true);
- qcloud_cos::PutBucketVersioningResp resp;
- qcloud_cos::CosResult result = cos.PutBucketVersioning(req, &resp);
- std::cout
- << "===================PutBucketVersioningResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- void GetBucketVersioning(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- qcloud_cos::GetBucketVersioningReq req(bucket_name);
- qcloud_cos::GetBucketVersioningResp resp;
- qcloud_cos::CosResult result = cos.GetBucketVersioning(req, &resp);
- std::cout << "Bucket status= " << resp.GetStatus() << std::endl;
- std::cout
- << "===================GetBucketVersioningResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- void PutBucketReplication(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- qcloud_cos::PutBucketReplicationReq req(bucket_name);
- req.SetRole("qcs::cam::uin/12345:uin/12345"); // 替换为用户 Role
- qcloud_cos::ReplicationRule rule(
- "", "qcs:id/0:cos:ap-xxx:appid/xxxxx:bucketname", "",
- "", true);// 替换为用户 rule
- req.AddReplicationRule(rule);
- qcloud_cos::PutBucketReplicationResp resp;
- qcloud_cos::CosResult result = cos.PutBucketReplication(req, &resp);
- std::cout
- << "===================PutBucketReplicationResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- void GetBucketReplication(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- qcloud_cos::GetBucketReplicationReq req(bucket_name);
- qcloud_cos::GetBucketReplicationResp resp;
- qcloud_cos::CosResult result = cos.GetBucketReplication(req, &resp);
- std::cout
- << "===================GetBucketReplicationResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- void DeleteBucketReplication(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- qcloud_cos::DeleteBucketReplicationReq req(bucket_name);
- qcloud_cos::DeleteBucketReplicationResp resp;
- qcloud_cos::CosResult result = cos.DeleteBucketReplication(req, &resp);
- std::cout << "===================DeleteBucketReplicationResponse============="
- "========"
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- void PutBucketLifecycle(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- qcloud_cos::PutBucketLifecycleReq req(bucket_name);
- {
- qcloud_cos::LifecycleRule rule;
- rule.SetIsEnable(true);
- rule.SetId("lifecycle_rule00");
- qcloud_cos::LifecycleFilter filter;
- filter.SetPrefix("sevenyou_e1");
- rule.SetFilter(filter);
- qcloud_cos::LifecycleTransition transition;
- transition.SetDays(30);
- transition.SetStorageClass("Standard_IA");
- rule.AddTransition(transition);
- req.AddRule(rule);
- }
- qcloud_cos::PutBucketLifecycleResp resp;
- qcloud_cos::CosResult result = cos.PutBucketLifecycle(req, &resp);
- std::cout
- << "===================PutBucketLifecycleResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- void GetBucketLifecycle(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- qcloud_cos::GetBucketLifecycleReq req(bucket_name);
- qcloud_cos::GetBucketLifecycleResp resp;
- qcloud_cos::CosResult result = cos.GetBucketLifecycle(req, &resp);
- const std::vector<qcloud_cos::LifecycleRule>& rules = resp.GetRules();
- for (size_t idx = 0; idx != rules.size(); ++idx) {
- std::cout << "id = " << rules[idx].GetId() << std::endl;
- }
- std::cout
- << "===================GetBucketLifecycleResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- void DeleteBucketLifecycle(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- qcloud_cos::DeleteBucketLifecycleReq req(bucket_name);
- qcloud_cos::DeleteBucketLifecycleResp resp;
- qcloud_cos::CosResult result = cos.DeleteBucketLifecycle(req, &resp);
- std::cout
- << "===================DeleteBucketLifecycleResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- void PutBucketACL(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
- // 1. 设置ACL配置(通过Body,
- // 设置ACL可以通过Body、Header两种方式,但只能二选一,否则会有冲突)
- {
- qcloud_cos::PutBucketACLReq req(bucket_name);
- qcloud_cos::Owner owner = {"qcs::cam::uin/xxx:uin/xxx",
- "qcs::cam::uin/xxx:uin/xxx"};
- qcloud_cos::Grant grant;
- req.SetOwner(owner);
- grant.m_grantee.m_type = "Group";
- grant.m_grantee.m_uri = "http://cam.qcloud.com/groups/global/AllUsers";
- grant.m_perm = "READ";
- req.AddAccessControlList(grant);
- qcloud_cos::PutBucketACLResp resp;
- qcloud_cos::CosResult result = cos.PutBucketACL(req, &resp);
- std::cout << "===================PutBucketACLResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "============================================================="
- "======="
- << std::endl;
- }
- #if 0
- // 2. 设置ACL配置(通过Header, 设置ACL可以通过Body、Header两种方式,但只能二选一,否则会有冲突)
- {
- qcloud_cos::PutBucketACLReq req(bucket_name);
- req.SetXCosAcl("public-read");
- qcloud_cos::PutBucketACLResp resp;
- qcloud_cos::CosResult result = cos.PutBucketACL(req, &resp);
- std::cout << "===================PutBucketACLResponse=====================" << std::endl;
- PrintResult(result, resp);
- std::cout << "====================================================================" << std::endl;
- }
- #endif
- }
- void GetBucketACL(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
- qcloud_cos::GetBucketACLReq req(bucket_name);
- qcloud_cos::GetBucketACLResp resp;
- qcloud_cos::CosResult result = cos.GetBucketACL(req, &resp);
- std::vector<qcloud_cos::Grant> grants = resp.GetAccessControlList();
- for (size_t idx = 0; idx < grants.size(); ++idx) {
- std::cout << "Type is " << grants[idx].m_grantee.m_type << std::endl;
- }
- std::cout << "===================GetBucketACLResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- void GetBucketPolicy(qcloud_cos::CosAPI& cos,const std::string& bucket_name) {
- qcloud_cos::GetBucketPolicyReq req(bucket_name);
- qcloud_cos::GetBucketPolicyResp resp;
- qcloud_cos::CosResult result = cos.GetBucketPolicy(req, &resp);
- std::cout << resp.GetPolicy() << std::endl;
- std::cout << "===================GetBucketPolicy====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- void PutBucketPolicy(qcloud_cos::CosAPI& cos,const std::string& bucket_name) {
- qcloud_cos::PutBucketPolicyReq req(bucket_name);
- qcloud_cos::PutBucketPolicyResp resp;
- std::string bucket_policy =
- " {"
- " \"Statement\": ["
- " {"
- " \"Principal\": {"
- " \"qcs\": ["
- " \"qcs::cam::uin/xxx:uin/xxxx\"" //替换成您想授予权限的账户 uin / 子用户 uin
- " ]\n"
- " },\n"
- " \"Effect\": \"allow\","
- " \"Action\": ["
- " \"cos:PutObject\""
- " ],\n"
- " \"Resource\": [" //这里改成允许的路径前缀,可以根据自己网站的用户登录态判断允许上传的具体路径,例子: a.jpg 或者 a/* 或者 * (使用通配符*存在重大安全风险, 请谨慎评估使用)
- " \"qcs::cos:ap-guangzhou:uid/appid:backetname-appid/exampleobject\""
- " ],\n"
- " \"Condition\": {"
- " \"string_equal\": {"
- " \"cos:x-cos-mime-limit\": \"image/jpeg\""
- " }"
- " }"
- " }"
- " ],"
- " \"Version\": \"2.0\""
- " }";
- std::cout << bucket_policy << std::endl;
- req.SetBody(bucket_policy);
- qcloud_cos::CosResult result = cos.PutBucketPolicy(req, &resp);
- std::cout << "===================PutBucketPolicy====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- void DeleteBucketPolicy(qcloud_cos::CosAPI& cos,const std::string& bucket_name) {
- qcloud_cos::DeleteBucketPolicyReq req(bucket_name);
- qcloud_cos::DeleteBucketPolicyResp resp;
- qcloud_cos::CosResult result = cos.DeleteBucketPolicy(req, &resp);
- std::cout << "===================DeleteBucketPolicy====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- void PutBucketCORS(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
- qcloud_cos::PutBucketCORSReq req(bucket_name);
- qcloud_cos::CORSRule rule;
- rule.m_id = "123";
- rule.m_allowed_headers.push_back("x-cos-meta-test");
- rule.m_allowed_origins.push_back("http://www.qq.com");
- rule.m_allowed_origins.push_back("http://www.qcloud.com");
- rule.m_allowed_methods.push_back("PUT");
- rule.m_allowed_methods.push_back("GET");
- rule.m_max_age_secs = "600";
- rule.m_expose_headers.push_back("x-cos-expose");
- req.AddRule(rule);
- qcloud_cos::PutBucketCORSResp resp;
- qcloud_cos::CosResult result = cos.PutBucketCORS(req, &resp);
- std::cout << "===================PutBucketCORSResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- void GetBucketCORS(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
- qcloud_cos::GetBucketCORSReq req(bucket_name);
- qcloud_cos::GetBucketCORSResp resp;
- qcloud_cos::CosResult result = cos.GetBucketCORS(req, &resp);
- std::cout << "===================GetBucketCORSResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- void DeleteBucketCORS(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
- qcloud_cos::DeleteBucketCORSReq req(bucket_name);
- qcloud_cos::DeleteBucketCORSResp resp;
- qcloud_cos::CosResult result = cos.DeleteBucketCORS(req, &resp);
- std::cout
- << "===================DeleteBucketCORSResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- void PutObjectByFile(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name,
- const std::string& file_path) {
- qcloud_cos::PutObjectByFileReq req(bucket_name, object_name, file_path);
- // req.AddParam("versionId", "MTg0NDY3NDI1NjQ4NjUyMjQ1MTA");
- qcloud_cos::PutObjectByFileResp resp;
- qcloud_cos::CosResult result = cos.PutObject(req, &resp);
- std::cout << "===================PutObjectResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================="
- << std::endl;
- }
- // 限速上传对象
- void PutObjectByFileLimitTraffic(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name,
- const std::string& object_name,
- const std::string& file_path,
- const uint64_t traffic_limit) {
- qcloud_cos::PutObjectByFileReq req(bucket_name, object_name, file_path);
- req.SetTrafficLimit(traffic_limit);
- qcloud_cos::PutObjectByFileResp resp;
- qcloud_cos::CosResult result = cos.PutObject(req, &resp);
- std::cout << "===================PutObjectFileLimitTrafficResponse==========="
- "=========="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================="
- << std::endl;
- }
- void PutObjectByStream(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- std::istringstream iss("put object");
- qcloud_cos::PutObjectByStreamReq req(bucket_name, "test/sevenyou_10m", iss);
- qcloud_cos::PutObjectByStreamResp resp;
- qcloud_cos::CosResult result = cos.PutObject(req, &resp);
- std::cout << "===================PutObjectResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================="
- << std::endl;
- }
- void HeadObject(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name) {
- qcloud_cos::HeadObjectReq req(bucket_name, object_name);
- // req.AddParam("versionId", "MTg0NDY3NDI1NjQ4NjUyMjQ1MTA");
- qcloud_cos::HeadObjectResp resp;
- qcloud_cos::CosResult result = cos.HeadObject(req, &resp);
- std::cout << "===================HeadObjectResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "=========================================================="
- << std::endl;
- }
- // 下载对象到指定文件
- void GetObjectByFile(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name,
- const std::string& file_path) {
- qcloud_cos::GetObjectByFileReq req(bucket_name, object_name, file_path);
- // req.AddParam("versionId", "MTg0NDY3NDI1NjQwODU3NzU2MTA");
- qcloud_cos::GetObjectByFileResp resp;
- qcloud_cos::CosResult result = cos.GetObject(req, &resp);
- std::cout << "===================GetObjectResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================="
- << std::endl;
- }
- // 限速下载对象
- void GetObjectByFileLimitTraffic(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name,
- const std::string& object_name,
- const std::string& file_path,
- const uint64_t traffic_limit) {
- qcloud_cos::GetObjectByFileReq req(bucket_name, object_name, file_path);
- req.SetTrafficLimit(traffic_limit);
- qcloud_cos::GetObjectByFileResp resp;
- qcloud_cos::CosResult result = cos.GetObject(req, &resp);
- std::cout << "===================GetObjectByFileLimitTrafficResponse========="
- "============"
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================="
- << std::endl;
- }
- void GetObjectByStream(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name) {
- std::ostringstream os;
- qcloud_cos::GetObjectByStreamReq req(bucket_name, object_name, os);
- qcloud_cos::GetObjectByStreamResp resp;
- qcloud_cos::CosResult result = cos.GetObject(req, &resp);
- std::cout << "===================GetObjectResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================="
- << std::endl;
- std::cout << os.str() << std::endl;
- }
- // 多线程下载对象
- void MultiGetObject(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name,
- const std::string& file_path) {
- qcloud_cos::MultiGetObjectReq req(bucket_name, object_name, file_path);
- qcloud_cos::MultiGetObjectResp resp;
- qcloud_cos::CosResult result = cos.MultiGetObject(req, &resp);
- std::cout << "===================GetObjectResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================="
- << std::endl;
- }
- // 多线程限速下载对象
- void MultiGetObjectLimitTraffic(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name,
- const std::string& object_name,
- const std::string& file_path,
- const uint64_t traffic_limit) {
- qcloud_cos::MultiGetObjectReq req(bucket_name, object_name, file_path);
- req.SetTrafficLimit(traffic_limit);
- qcloud_cos::MultiGetObjectResp resp;
- qcloud_cos::CosResult result = cos.MultiGetObject(req, &resp);
- std::cout << "===================MultiGetObjectLimitTrafficResponse=========="
- "==========="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================="
- << std::endl;
- }
- // 初始化分块上传
- void InitMultiUpload(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name, std::string* upload_id) {
- qcloud_cos::InitMultiUploadReq req(bucket_name, object_name);
- qcloud_cos::InitMultiUploadResp resp;
- qcloud_cos::CosResult result = cos.InitMultiUpload(req, &resp);
- std::cout << "=====================InitMultiUpload====================="<< std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================="<< std::endl;
- *upload_id = resp.GetUploadId();
- }
- // 上传分块
- void UploadPartData(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name,
- const std::string& upload_id, std::fstream& is,
- uint64_t number, std::string* etag) {
- qcloud_cos::UploadPartDataReq req(bucket_name, object_name, upload_id, is);
- req.SetPartNumber(number);
- qcloud_cos::UploadPartDataResp resp;
- qcloud_cos::CosResult result = cos.UploadPartData(req, &resp);
- *etag = resp.GetEtag();
- std::cout << "======================UploadPartData====================="<< std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================="<< std::endl;
- }
- // 限速上传分块
- void UploadPartDataLimitTraffic(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name,
- const std::string& object_name,
- const std::string& upload_id, std::fstream& is,
- uint64_t number, std::string* etag,
- const uint64_t traffic_limit) {
- qcloud_cos::UploadPartDataReq req(bucket_name, object_name, upload_id, is);
- req.SetPartNumber(number);
- req.SetTrafficLimit(traffic_limit);
- qcloud_cos::UploadPartDataResp resp;
- qcloud_cos::CosResult result = cos.UploadPartData(req, &resp);
- *etag = resp.GetEtag();
- std::cout << "======================UploadPartDataLimitTrafficResponse======="<< std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================="<< std::endl;
- }
- void AbortMultiUpload(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name,
- const std::string& upload_id) {
- qcloud_cos::AbortMultiUploadReq req(bucket_name, object_name, upload_id);
- qcloud_cos::AbortMultiUploadResp resp;
- qcloud_cos::CosResult result = cos.AbortMultiUpload(req, &resp);
- std::cout << "======================AbortUploadPart=====================";
- PrintResult(result, resp);
- std::cout << "=========================================================";
- }
- void CompleteMultiUpload(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name,
- const std::string& object_name,
- const std::string& upload_id,
- const std::vector<std::string>& etags,
- const std::vector<uint64_t>& numbers) {
- qcloud_cos::CompleteMultiUploadReq req(bucket_name, object_name, upload_id);
- qcloud_cos::CompleteMultiUploadResp resp;
- req.AddParam("versionId", "MTg0NDY3NDI1NjQ4NjUyMjQ1MTA");
- req.SetEtags(etags);
- req.SetPartNumbers(numbers);
- qcloud_cos::CosResult result = cos.CompleteMultiUpload(req, &resp);
- std::cout << "===================Complete============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 多线程上传对象
- void MultiUploadObject(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name,
- const std::string& local_file) {
- qcloud_cos::MultiPutObjectReq req(bucket_name, object_name, local_file);
- req.SetRecvTimeoutInms(1000 * 60);
- qcloud_cos::MultiPutObjectResp resp;
- qcloud_cos::CosResult result = cos.MultiPutObject(req, &resp);
- if (result.IsSucc()) {
- std::cout << "MultiUpload Succ." << std::endl;
- std::cout << resp.GetLocation() << std::endl;
- std::cout << resp.GetKey() << std::endl;
- std::cout << resp.GetBucket() << std::endl;
- std::cout << resp.GetEtag() << std::endl;
- } else {
- std::cout << "MultiUpload Fail." << std::endl;
- // 获取具体失败在哪一步
- std::string resp_tag = resp.GetRespTag();
- if ("Init" == resp_tag) {
- // print result
- } else if ("Upload" == resp_tag) {
- // print result
- } else if ("Complete" == resp_tag) {
- // print result
- }
- }
- std::cout
- << "===================MultiUploadObject============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- #ifdef _WIN32
- // 上传宽字符文件
- void MultiUploadObjectWideChar(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- std::string local_file = "./안녕하십니까.mp4";
- std::string object_name = "안녕하십니까.mp4";
- qcloud_cos::MultiPutObjectReq req(bucket_name, object_name, local_file);
- req.SetWideCharPath(); // 需要调用该函数
- req.SetRecvTimeoutInms(1000 * 60);
- qcloud_cos::MultiPutObjectResp resp;
- qcloud_cos::CosResult result = cos.MultiPutObject(req, &resp);
- if (result.IsSucc()) {
- std::cout << "MultiUploadObjectWideChar Succ." << std::endl;
- std::cout << resp.GetLocation() << std::endl;
- std::cout << resp.GetKey() << std::endl;
- std::cout << resp.GetBucket() << std::endl;
- std::cout << resp.GetEtag() << std::endl;
- } else {
- std::cout << "MultiUploadObjectWideChar Fail." << std::endl;
- }
- std::cout << "===================MultiUploadObjectWideChar==================="
- "=========="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- #endif
- //限速多线程上传
- void MultiUploadObjectLimitTraffic(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name,
- const std::string& object_name,
- const std::string& local_file,
- const uint64_t traffic_limit) {
- qcloud_cos::MultiPutObjectReq req(bucket_name, object_name, local_file);
- req.SetTrafficLimit(traffic_limit);
- req.SetRecvTimeoutInms(1000 * 60);
- qcloud_cos::MultiPutObjectResp resp;
- qcloud_cos::CosResult result = cos.MultiPutObject(req, &resp);
- if (result.IsSucc()) {
- std::cout << "MultiUpload Succ." << std::endl;
- std::cout << resp.GetLocation() << std::endl;
- std::cout << resp.GetKey() << std::endl;
- std::cout << resp.GetBucket() << std::endl;
- std::cout << resp.GetEtag() << std::endl;
- } else {
- std::string resp_tag = resp.GetRespTag();
- std::cout << "MultiUpload Fail, respond tag " << resp_tag << std::endl;
- }
- std::cout << "===================MultiUpload============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- void ListParts(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name, const std::string& upload_id) {
- qcloud_cos::ListPartsReq req(bucket_name, object_name, upload_id);
- req.SetMaxParts(1);
- req.SetPartNumberMarker("1");
- qcloud_cos::ListPartsResp resp;
- qcloud_cos::CosResult result = cos.ListParts(req, &resp);
- std::cout << "===================ListParts=====================" << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- //用户自行调用初始化、上传分块、完成分块接口进行分块上传
- void MultiUpload(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- std::string upload_id;
- std::string object_name = "test/sevenyou_e2_1102_north_multi";
- std::vector<uint64_t> numbers;
- std::vector<std::string> etags;
- std::string etag1 = "", etag2 = "";
- InitMultiUpload(cos, bucket_name, object_name, &upload_id);
- std::fstream is1("./temp/testfilebig");
- UploadPartData(cos, bucket_name, object_name, upload_id, is1, 1, &etag1);
- numbers.push_back(1);
- etags.push_back(etag1);
- is1.close();
- ListParts(cos, bucket_name, object_name, upload_id);
- // AbortMultiUpload(cos, bucket_name, object_name, upload_id);
- std::fstream is2("./temp/testfilebig");
- UploadPartData(cos, bucket_name, object_name, upload_id, is2, 2,
- &etag2);
- numbers.push_back(2);
- etags.push_back(etag2);
- is2.close();
- CompleteMultiUpload(cos, bucket_name, object_name, upload_id, etags, numbers);
- }
- void PutObjectACL(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name) {
- // 1 设置ACL配置(通过Body,
- // 设置ACL可以通过Body、Header两种方式,但只能二选一,否则会有冲突)
- {
- qcloud_cos::PutObjectACLReq req(bucket_name, object_name);
- qcloud_cos::Owner owner = {"qcs::cam::uin/xxx:uin/xxx",
- "qcs::cam::uin/xxx:uin/xxx"};
- qcloud_cos::Grant grant;
- req.SetOwner(owner);
- grant.m_grantee.m_type = "CanonicalUser";
- grant.m_grantee.m_id = "qcs::cam::uin/xxx:uin/xxx";
- grant.m_perm = "READ";
- req.AddAccessControlList(grant);
- qcloud_cos::PutObjectACLResp resp;
- qcloud_cos::CosResult result = cos.PutObjectACL(req, &resp);
- std::cout << "===================PutObjectACLResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "============================================================="
- "======="
- << std::endl;
- }
- // 2 设置ACL配置(通过Header,
- // 设置ACL可以通过Body、Header两种方式,但只能二选一,否则会有冲突)
- {
- qcloud_cos::PutObjectACLReq req(bucket_name, object_name);
- req.SetXCosAcl("default");
- qcloud_cos::PutObjectACLResp resp;
- qcloud_cos::CosResult result = cos.PutObjectACL(req, &resp);
- std::cout << "===================PutObjectACLResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "============================================================="
- "======="
- << std::endl;
- }
- }
- void GetObjectACL(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name) {
- qcloud_cos::GetObjectACLReq req(bucket_name, object_name);
- qcloud_cos::GetObjectACLResp resp;
- qcloud_cos::CosResult result = cos.GetObjectACL(req, &resp);
- std::cout << "===================GetObjectACLResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- // 为已存在的 Object 设置标签(Tag)
- void PutObjectTagging(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name) {
- qcloud_cos::PutObjectTaggingReq req(bucket_name, object_name);
- qcloud_cos::PutObjectTaggingResp resp;
- std::vector<Tag> tagset;
- Tag tag1;
- tag1.SetKey("age");
- tag1.SetValue("19");
- Tag tag2;
- tag2.SetKey("name");
- tag2.SetValue("xiaoming");
- Tag tag3;
- tag3.SetKey("sex");
- tag3.SetValue("male");
- tagset.push_back(tag1);
- tagset.push_back(tag2);
- tagset.push_back(tag3);
- req.SetTagSet(tagset);
- qcloud_cos::CosResult result = cos.PutObjectTagging(req, &resp);
- std::cout << "===================PutBucketTagging====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- //查询指定Object的标签
- void GetObjectTagging(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name) {
- qcloud_cos::GetObjectTaggingReq req(bucket_name, object_name);
- qcloud_cos::GetObjectTaggingResp resp;
- qcloud_cos::CosResult result = cos.GetObjectTagging(req, &resp);
- std::cout << "===================GetObjectTagging====================="
- << std::endl;
- std::vector<Tag> tagset = resp.GetTagSet();
- for (std::vector<Tag>::iterator it = tagset.begin(); it != tagset.end();
- ++it) {
- std::cout << it->GetKey() << ":" << it->GetValue() << std::endl;
- }
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- //删除指定Object的标签
- void DeleteObjectTagging(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name) {
- qcloud_cos::DeleteObjectTaggingReq req(bucket_name, object_name);
- qcloud_cos::DeleteObjectTaggingResp resp;
- qcloud_cos::CosResult result = cos.DeleteObjectTagging(req, &resp);
- std::cout << "===================DeleteObjectTagging====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- void GeneratePresignedUrl(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
- qcloud_cos::GeneratePresignedUrlReq req(bucket_name, "seven_50M.tmp",
- qcloud_cos::HTTP_GET); std::string presigned_url = cos.GeneratePresignedUrl(req);
- std::cout << "===================GeneratePresignedUrl====================="
- << std::endl;
- std::cout << "Presigend Url=[" <<presigned_url << "]" << std::endl;
- std::cout
- << "===================================================================="
- << std::endl;
- }
- void GetBucketLocation(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
- std::cout << "===================GetBucketLocation====================="
- << std::endl;
- std::cout << "Bucket=" << bucket_name << ", Location=" <<
- cos.GetBucketLocation(bucket_name) << std::endl;
- std::cout
- << "===================================================================="
- << std::endl;
- }
- void IsObjectExist(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
- std::cout << "===================IsObjectExist====================="
- << std::endl;
- std::cout << (cos.IsObjectExist(bucket_name, "abcdefg") ? "true" : "false") << std::endl;
- std::cout << (cos.IsObjectExist(bucket_name, "/test/test1") ? "true" : "false") << std::endl;
- std::cout
- << "===================================================================="
- << std::endl;
- }
- void IsBucketExist(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
- std::cout << "===================IsBucketExist====================="
- << std::endl;
- std::cout << (cos.IsBucketExist("abcdefg") ? "true" : "false") << std::endl;
- std::cout << (cos.IsBucketExist(bucket_name) ? "true" : "false") << std::endl;
- std::cout
- << "===================================================================="
- << std::endl;
- }
- void PutObjectCopy(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name, const std::string& source) {
- qcloud_cos::PutObjectCopyReq req(bucket_name, object_name);
- req.SetXCosCopySource(source);
- qcloud_cos::PutObjectCopyResp resp;
- qcloud_cos::CosResult result = cos.PutObjectCopy(req, &resp);
- std::cout << "===================PutObjectCopyResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- void DeleteObject(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name) {
- qcloud_cos::DeleteObjectReq req(bucket_name, object_name);
- qcloud_cos::DeleteObjectResp resp;
- qcloud_cos::CosResult result = cos.DeleteObject(req, &resp);
- std::cout << "===================DeleteObjectResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================="
- << std::endl;
- }
- void DeleteObjects(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::vector<ObjectVersionPair>& objects) {
- qcloud_cos::DeleteObjectsReq req(bucket_name, objects);
- qcloud_cos::DeleteObjectsResp resp;
- qcloud_cos::CosResult result = cos.DeleteObjects(req, &resp);
- std::cout << "===================DeleteObjectsResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================="
- << std::endl;
- std::cout << "Resp body=[" << resp.DebugString() << "]" << std::endl;
- }
- void UploadPartCopy(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name,
- const std::string& upload_id, const std::string& source,
- const std::string& range, uint64_t number,
- std::string* etag) {
- qcloud_cos::UploadPartCopyDataReq req(bucket_name, object_name, upload_id,
- number);
- req.SetXCosCopySource(source);
- req.SetXCosCopySourceRange(range);
- qcloud_cos::UploadPartCopyDataResp resp;
- qcloud_cos::CosResult result = cos.UploadPartCopyData(req, &resp);
- std::cout << "===================UploadPartCopyDataResp====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- if (result.IsSucc()) {
- *etag = resp.GetEtag();
- }
- }
- //用户自行调用初始化、拷贝分块、完成分块接口进行分块拷贝
- void UploadCopy(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
- std::string upload_id;
- std::string object_name = "test/sevenyou_200MB_part_copy_from_north";
- std::vector<uint64_t> numbers;
- std::vector<std::string> etags;
- std::string etag1 = "", etag2 = "";
- InitMultiUpload(cos, bucket_name, object_name, &upload_id);
- UploadPartCopy(cos, bucket_name, object_name, upload_id,
- "xxxx-appid.cos.region.myqcloud.com/objectkey",
- "bytes=0-104857600", 1, &etag1);
- numbers.push_back(1);
- etags.push_back(etag1);
- UploadPartCopy(cos, bucket_name, object_name, upload_id,
- "xxxx-appid.cos.region.myqcloud.com/objectkey",
- "bytes=104857600-209715200", 2, &etag2);
- numbers.push_back(2);
- etags.push_back(etag2);
- CompleteMultiUpload(cos, bucket_name, object_name, upload_id, etags,
- numbers);
- }
- void Copy(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name, const std::string& source) {
- qcloud_cos::CopyReq req(bucket_name, object_name);
- qcloud_cos::CopyResp resp;
- req.SetXCosCopySource(source);
- qcloud_cos::CosResult result = cos.Copy(req, &resp);
- std::cout << "===================Copy=====================" << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- void ListMultipartUpload(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- qcloud_cos::ListMultipartUploadReq req(bucket_name);
- qcloud_cos::ListMultipartUploadResp resp;
- qcloud_cos::CosResult result = cos.ListMultipartUpload(req, &resp);
- std::vector<Upload> rst = resp.GetUpload();
- for (std::vector<qcloud_cos::Upload>::const_iterator itr = rst.begin();
- itr != rst.end(); ++itr) {
- const qcloud_cos::Upload& upload = *itr;
- std::cout << "key = " << upload.m_key << ", uploadid= " << upload.m_uploadid
- << ", storagen class = " << upload.m_storage_class
- << ", m_initiated= " << upload.m_initiated << std::endl;
- }
- std::cout << "===================ListMultipartUpload====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- // 开启bucket日志配置
- void PutBucketLogging(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& TargetBucketname,
- const std::string& TargetPrefix) {
- qcloud_cos::PutBucketLoggingReq req(bucket_name);
- qcloud_cos::LoggingEnabled rules;
- rules.SetTargetBucket(TargetBucketname);
- rules.SetTargetPrefix(TargetPrefix);
- req.SetLoggingEnabled(rules);
- qcloud_cos::PutBucketLoggingResp resp;
- qcloud_cos::CosResult result = cos.PutBucketLogging(req, &resp);
- std::cout
- << "===================PutBucketLoggingResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- // 获取bucket日志配置
- void GetBucketLogging(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
- qcloud_cos::GetBucketLoggingReq req(bucket_name);
- qcloud_cos::GetBucketLoggingResp resp;
- qcloud_cos::CosResult result = cos.GetBucketLogging(req, &resp);
- std::cout
- << "===================GetBucketLoggingResponse====================="
- << std::endl;
- const qcloud_cos::LoggingEnabled loggingenabled = resp.GetLoggingEnabled();
- std::cout << "TargetBucket = " << loggingenabled.GetTargetBucket()
- << std::endl;
- std::cout << "TargetPrefix = " << loggingenabled.GetTargetPrefix()
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- // 设置存储桶绑定自定义域名
- void PutBucketDomain(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
- qcloud_cos::PutBucketDomainReq req(bucket_name);
- DomainRule rules;
- rules.SetStatus("ENABLED");
- rules.SetName("www.abc.com");
- rules.SetType("REST");
- // rules.SetForcedReplacement();
- req.SetDomainRule(rules);
- qcloud_cos::PutBucketDomainResp resp;
- qcloud_cos::CosResult result = cos.PutBucketDomain(req, &resp);
- std::cout << "===================PutBucketDomainResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- // 获取存储桶绑定自定义域名
- void GetBucketDomain(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
- qcloud_cos::GetBucketDomainReq req(bucket_name);
- qcloud_cos::GetBucketDomainResp resp;
- qcloud_cos::CosResult result = cos.GetBucketDomain(req, &resp);
- std::cout << "===================GetBucketDomainResponse====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- // 为存储桶配置静态网站
- void PutBucketWebsite(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
- qcloud_cos::PutBucketWebsiteReq req(bucket_name);
- qcloud_cos::PutBucketWebsiteResp resp;
- req.SetSuffix("index.xml"); //必选项
- req.SetProtocol("https");
- req.SetKey("Error.html");
- //设置重定向规则,最多设置100条
- //设置第一条规则
- qcloud_cos::RoutingRule routerule1;
- qcloud_cos::Condition temp_condtion1;
- temp_condtion1.SetHttpErrorCodeReturnedEquals(404); //需要设置,默认404
- routerule1.SetCondition(temp_condtion1);
- qcloud_cos::Redirect temp_redirect1;
- temp_redirect1.SetProtocol("https");
- temp_redirect1.SetReplaceKeyWith("404.htmp");
- routerule1.SetRedirect(temp_redirect1);
- //设置第二条规则
- qcloud_cos::RoutingRule routerule2;
- qcloud_cos::Condition temp_condtion2;
- temp_condtion2.SetHttpErrorCodeReturnedEquals(403); //需要设置,默认404
- routerule2.SetCondition(temp_condtion2);
- qcloud_cos::Redirect temp_redirect2;
- temp_redirect2.SetProtocol("https");
- temp_redirect2.SetReplaceKeyWith("403.htmp");
- routerule2.SetRedirect(temp_redirect2);
- //设置第三条规则
- qcloud_cos::RoutingRule routerule3;
- qcloud_cos::Condition temp_condtion3;
- temp_condtion3.SetKeyPrefixEquals("img/");
- temp_condtion3.SetHttpErrorCodeReturnedEquals(402);
- routerule3.SetCondition(temp_condtion3);
- qcloud_cos::Redirect temp_redirect3;
- temp_redirect3.SetProtocol("https");
- temp_redirect3.SetReplaceKeyWith("401.htmp");
- routerule3.SetRedirect(temp_redirect3);
- qcloud_cos::RoutingRule routerule4;
- qcloud_cos::Condition temp_condtion4;
- temp_condtion4.SetKeyPrefixEquals("img1/");
- routerule4.SetCondition(temp_condtion4);
- qcloud_cos::Redirect temp_redirect4;
- temp_redirect4.SetProtocol("https");
- temp_redirect4.SetReplaceKeyPrefixWith("402.htmp");
- routerule4.SetRedirect(temp_redirect4);
- req.AddRoutingRule(routerule1);
- req.AddRoutingRule(routerule2);
- req.AddRoutingRule(routerule3);
- req.AddRoutingRule(routerule4);
- qcloud_cos::CosResult result = cos.PutBucketWebsite(req, &resp);
- std::cout << "===================PutBucketWebsite====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- // 获取与存储桶关联的静态网站配置信息
- void GetBucketWebsite(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
- qcloud_cos::GetBucketWebsiteReq req(bucket_name);
- qcloud_cos::GetBucketWebsiteResp resp;
- qcloud_cos::CosResult result = cos.GetBucketWebsite(req, &resp);
- std::cout << "===================GetBucketWebsite====================="
- << std::endl;
- std::cout << "Suffix:" << resp.GetSuffix() << std::endl;
- std::cout << "Protocol:" << resp.GetProtocol() << std::endl;
- std::cout << "ErrorDocument:" << resp.GetKey() << std::endl;
- std::vector<RoutingRule> routingrules = resp.GetRoutingRules();
- std::vector<RoutingRule>::iterator it = routingrules.begin();
- for (; it != routingrules.end(); ++it) {
- const Condition& condition = it->GetCondition();
- std::cout << "condition httpcode"
- << condition.GetHttpErrorCodeReturnedEquals() << std::endl;
- std::cout << "condition KeyPrefixEquals:" << condition.GetKeyPrefixEquals()
- << std::endl;
- const Redirect& redirect = it->GetRedirect();
- std::cout << "redirect Protocol:" << redirect.GetProtocol() << std::endl;
- std::cout << "redirect ReplaceKeyWith" << redirect.GetReplaceKeyWith()
- << std::endl;
- std::cout << "redirect ReplaceKeyPrefixWith"
- << redirect.GetReplaceKeyPrefixWith() << std::endl;
- std::cout << std::endl;
- }
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- // 删除存储桶中的静态网站配置
- void DeleteBucketWebsite(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- qcloud_cos::DeleteBucketWebsiteReq req(bucket_name);
- qcloud_cos::DeleteBucketWebsiteResp resp;
- qcloud_cos::CosResult result = cos.DeleteBucketWebsite(req, &resp);
- std::cout << "===================DeleteBucketWebsite====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- // 为已存在的 Bucket 设置标签(Tag)
- void PutBucketTagging(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
- std::vector<Tag> tagset;
- Tag tag1;
- tag1.SetKey("age");
- tag1.SetValue("19");
- Tag tag2;
- tag2.SetKey("name");
- tag2.SetValue("xiaoming");
- tagset.push_back(tag1);
- tagset.push_back(tag2);
- qcloud_cos::PutBucketTaggingReq req(bucket_name);
- qcloud_cos::PutBucketTaggingResp resp;
- req.SetTagSet(tagset);
- qcloud_cos::CosResult result = cos.PutBucketTagging(req, &resp);
- std::cout << "===================PutBucketTagging====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- //用于查询指定存储桶下已有的存储桶标签
- void GetBucketTagging(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
- qcloud_cos::GetBucketTaggingReq req(bucket_name);
- qcloud_cos::GetBucketTaggingResp resp;
- qcloud_cos::CosResult result = cos.GetBucketTagging(req, &resp);
- std::cout << "===================GetBucketTagging====================="
- << std::endl;
- std::vector<Tag> tagset = resp.GetTagSet();
- for (std::vector<Tag>::iterator it = tagset.begin(); it != tagset.end();
- ++it) {
- std::cout << it->GetKey() << ":" << it->GetValue() << std::endl;
- }
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- //删除指定存储桶下已有的存储桶标签
- void DeleteBucketTagging(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- qcloud_cos::DeleteBucketTaggingReq req(bucket_name);
- qcloud_cos::DeleteBucketTaggingResp resp;
- qcloud_cos::CosResult result = cos.DeleteBucketTagging(req, &resp);
- std::cout << "===================DeleteBucketTagging====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- // 在存储桶中创建清单任务
- void PutBucketInventory(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- qcloud_cos::PutBucketInventoryReq req(bucket_name);
- req.SetId("list3");
-
- COSBucketDestination destination;
- destination.SetFormat("CSV");
- // destination.SetAccountId("100010974959");
- destination.SetAccountId("appid");
- std::string target_bucket =
- "qcs::cos:ap-guangzhou::xxxxxx-appid";
- destination.SetBucket(target_bucket);
- destination.SetPrefix("/");
- destination.SetEncryption(true);
- OptionalFields fields;
- fields.SetIsSize(true);
- fields.SetIsLastModified(true);
- fields.SetIsStorageClass(true);
- fields.SetIsMultipartUploaded(true);
- fields.SetIsReplicationStatus(true);
- fields.SetIsEtag(true);
- Inventory inventory;
- inventory.SetIsEnable(true);
- inventory.SetIncludedObjectVersions("All");
- inventory.SetFilter("/");
- inventory.SetId(req.GetId());
- inventory.SetFrequency("Daily");
- inventory.SetCOSBucketDestination(destination);
- inventory.SetOptionalFields(fields);
- req.SetInventory(inventory);
- qcloud_cos::PutBucketInventoryResp resp;
- qcloud_cos::CosResult result = cos.PutBucketInventory(req, &resp);
- std::cout << "===================PutBucketinventory====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- // 用于查询存储桶中用户的清单任务信息
- void GetBucketInventory(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- qcloud_cos::GetBucketInventoryReq req(bucket_name);
- qcloud_cos::GetBucketInventoryResp resp;
- req.SetId("list3");
- qcloud_cos::CosResult result = cos.GetBucketInventory(req, &resp);
- // std::cout << "===================GetBucketinventory====================="
- // << std::endl;
- //
- // const Inventory inventory = resp.GetInventory();
- // std::cout << "inventory isenabled:" << inventory.GetIsEnable() <<
- // std::endl; std::cout << "inventory IncludedObjectVersions:" <<
- // inventory.GetIncludedObjectVersions() << std::endl; std::cout << "inventory
- // filter:" << inventory.GetFilter() << std::endl; std::cout << "inventory
- // id:"
- // << inventory.GetId() << std::endl; std::cout << "inventory Frequency:" <<
- // inventory.GetFrequency() << std::endl;
- // std:: cout << "===================================" << std::endl;
- // COSBucketDestination destination = inventory.GetCOSBucketDestination();
- // std::cout << "destination Format:" << destination.GetFormat() << std::endl;
- // std::cout << "destination AccountID:" << destination.GetAccountId() <<
- // std::endl; std::cout << "destination Bucket:" << destination.GetBucket() <<
- // std::endl; std::cout << "destination Encryption:" <<
- // destination.GetEncryption() << std::endl;
- // std:: cout << "===================================" << std::endl;
- //
- // OptionalFields fields = inventory.GetOptionalFields();
- // std::cout << "fields Size:" << fields.GetIsSize() << std::endl;
- // std::cout << "fields LastModified:" << fields.GetIsLastModified() <<
- // std::endl; std::cout << "fields StorageClass:" <<
- // fields.GetIsStorageClass()
- // << std::endl; std::cout << "fields Region:" <<
- // fields.GetIsMultipartUploaded() << std::endl; std::cout << "fields
- // ReplicationStatus:" << fields.GetIsReplicationStatus() << std::endl;
- // std::cout << "fields Tag:" << fields.GetIsETag() << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- // 请求返回一个存储桶中的所有清单任务
- void ListBucketInventoryConfigurations(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- qcloud_cos::ListBucketInventoryConfigurationsReq req(bucket_name);
- qcloud_cos::ListBucketInventoryConfigurationsResp resp;
- qcloud_cos::CosResult result =
- cos.ListBucketInventoryConfigurations(req, &resp);
- // std::cout <<
- // "===================ListBucketInventoryConfigurations====================="
- // << std::endl;
- //
- // std::vector<Inventory> inventory_vec = resp.GetInventory();
- //
- // std::cout << resp.GetIsTruncated() << std::endl;
- // std::cout << resp.GetContinuationToken() << std::endl;
- // std::cout << resp.GetNextContinuationToken() << std::endl;
- //
- // std::vector<Inventory>::iterator itr = inventory_vec.begin();
- // for(; itr != inventory_vec.end(); ++itr) {
- //
- // std:: cout << "==============Inventory=============================" <<
- // std::endl; std::cout << "inventory id:" << itr->GetId() << std::endl;
- // std::cout << "inventory isenabled:" << itr->GetIsEnable() << std::endl;
- // std::cout << "inventory IncludedObjectVersions:" <<
- // itr->GetIncludedObjectVersions() << std::endl; std::cout << "inventory
- // filter:" << itr->GetFilter() << std::endl; std::cout << "inventory
- // Frequency:" << itr->GetFrequency() << std::endl;
- // std:: cout << "==============GetCOSBucketDestination=================="
- // << std::endl; COSBucketDestination destination =
- // itr->GetCOSBucketDestination(); std::cout << "destination Format:" <<
- // destination.GetFormat() << std::endl; std::cout << "destination
- // AccountID:" << destination.GetAccountId() << std::endl; std::cout <<
- // "destination Bucket:" << destination.GetBucket() << std::endl; std::cout
- // << "destination Encryption:" << destination.GetEncryption() <<
- // std::endl;
- //
- //
- // std:: cout << "===================OptionalFields======================"
- // << std::endl; OptionalFields fields = itr->GetOptionalFields();
- // std::cout << "fields Size:" << fields.GetIsSize() << std::endl;
- // std::cout << "fields LastModified:" << fields.GetIsLastModified() <<
- // std::endl; std::cout << "fields StorageClass:" <<
- // fields.GetIsStorageClass() << std::endl; std::cout << "fields Region:"
- // << fields.GetIsMultipartUploaded() << std::endl; std::cout << "fields
- // ReplicationStatus:" << fields.GetIsReplicationStatus() << std::endl;
- // std::cout << "fields Tag:" << fields.GetIsETag() << std::endl;
- // }
- // std::cout << "===============ListBucketInventoryConfigurations
- // end================" << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- // 用于删除存储桶中指定的清单任务.
- void DeleteBucketInventory(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- qcloud_cos::DeleteBucketInventoryReq req(bucket_name);
- qcloud_cos::DeleteBucketInventoryResp resp;
- req.SetId("list");
- qcloud_cos::CosResult result = cos.DeleteBucketInventory(req, &resp);
- std::cout << "===================DeleteBucketinventory====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- // 配置存储桶referer
- void PutBucketReferer(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
- qcloud_cos::PutBucketRefererReq req(bucket_name);
- qcloud_cos::PutBucketRefererResp resp;
- req.SetStatus("Enabled");
- req.SetRefererType("White-List");
- req.AddDomain("test1.com");
- req.AddDomain("test2.com");
- qcloud_cos::CosResult result = cos.PutBucketReferer(req, &resp);
- std::cout << "===================PutBucketReferer====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- // 获取存储桶referer
- void GetBucketReferer(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
- qcloud_cos::GetBucketRefererReq req(bucket_name);
- qcloud_cos::GetBucketRefererResp resp;
- qcloud_cos::CosResult result = cos.GetBucketReferer(req, &resp);
- std::cout << "===================GetBucketReferer====================="
- << std::endl;
- PrintResult(result, resp);
- if (result.IsSucc()) {
- std::cout << "Status:" << resp.GetStatus() << std::endl;
- std::cout << "RefererType:" << resp.GetRefererType() << std::endl;
- for (auto& domain : resp.GetDomainList()) {
- std::cout << "Domain:" << domain << std::endl;
- }
- std::cout << "EmptyReferConfiguration:" << resp.GetEmptyReferConf()
- << std::endl;
- }
- std::cout
- << "===================================================================="
- << std::endl;
- }
- // 创建直播通道
- void CreateLiveChannel(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& channel_name) {
- qcloud_cos::PutLiveChannelReq req(bucket_name, channel_name);
- qcloud_cos::PutLiveChannelResp resp;
- std::map<std::string, std::string> url_params;
- url_params.insert(std::make_pair("a", "b"));
- LiveChannelConfiguration config("test", "Enabled", "HLS", 5, 10,
- "playlist.m3u8");
- req.SetLiveChannelConfig(config);
- req.SetUrlParams(url_params);
- req.SetExpire(1000);
- qcloud_cos::CosResult result = cos.PutLiveChannel(req, &resp);
- std::cout << "===================CreateLiveChannel====================="
- << std::endl;
- PrintResult(result, resp);
- if (result.IsSucc()) {
- std::cout << "PlayUrl:" << resp.GetPlayUrl() << std::endl;
- std::cout << "PublishUrl:" << resp.GetPublishUrl() << std::endl;
- }
- std::cout
- << "===================================================================="
- << std::endl;
- }
- // 获取直播通道配置
- void GetLiveChannel(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& channel_name) {
- qcloud_cos::GetLiveChannelReq req(bucket_name, channel_name);
- qcloud_cos::GetLiveChannelResp resp;
- qcloud_cos::CosResult result = cos.GetLiveChannel(req, &resp);
- std::cout << "===================GetLiveChannel====================="
- << std::endl;
- PrintResult(result, resp);
- if (result.IsSucc()) {
- LiveChannelConfiguration chan_conf = resp.GetLiveChannelConf();
- std::stringstream oss;
- oss << "Description:" << chan_conf.GetDescription() << ",";
- oss << "Switch:" << chan_conf.GetSwitch() << ",";
- oss << "Type:" << chan_conf.GetType() << ",";
- oss << "FragDuration:" << chan_conf.GetFragDuration() << ",";
- oss << "FragCount:" << chan_conf.GetFragCount() << ",";
- oss << "PlaylistName:" << chan_conf.GetPlaylistName() << ",";
- oss << "PublishUrls:" << chan_conf.GetPublishUrl() << ",";
- oss << "PlayUrls:" << chan_conf.GetPlayUrl();
- std::cout << "LiveChannelConfiguration:" << oss.str() << std::endl;
- }
- std::cout
- << "===================================================================="
- << std::endl;
- }
- // 获取通道推流URL
- void GetRtmpSignedPublishUrl(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name,
- const std::string& channel_name) {
- std::cout << "===================GetRtmpSignedPublishUrl====================="
- << std::endl;
- std::cout << "PublishUrl:"
- << cos.GetRtmpSignedPublishUrl(bucket_name, channel_name, 3600,
- std::map<std::string, std::string>())
- << std::endl;
- std::cout
- << "===================================================================="
- << std::endl;
- }
- // 修改直播通道开关
- void PutLiveChannelSwitch(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name,
- const std::string& channel_name) {
- qcloud_cos::PutLiveChannelSwitchReq req(bucket_name, channel_name);
- qcloud_cos::PutLiveChannelSwitchResp resp;
- qcloud_cos::CosResult result;
- std::cout
- << "===================PutLiveChannelSwitch disabled====================="
- << std::endl;
- req.SetDisabled();
- result = cos.PutLiveChannelSwitch(req, &resp);
- PrintResult(result, resp);
- std::cout
- << "===================PutLiveChannelSwitch enabled====================="
- << std::endl;
- req.SetEnabled();
- result = cos.PutLiveChannelSwitch(req, &resp);
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- // 获取直播通道推流历史
- void GetLiveChannelHistory(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name,
- const std::string& channel_name) {
- qcloud_cos::GetLiveChannelHistoryReq req(bucket_name, channel_name);
- qcloud_cos::GetLiveChannelHistoryResp resp;
- qcloud_cos::CosResult result = cos.GetLiveChannelHistory(req, &resp);
- std::cout << "===================GetLiveChannelHistory====================="
- << std::endl;
- PrintResult(result, resp);
- if (result.IsSucc()) {
- const std::vector<LiveRecord>& chan_history = resp.GetChanHistory();
- std::vector<LiveRecord>::const_iterator it = chan_history.begin();
- for (; it != chan_history.end(); ++it) {
- std::stringstream oss;
- oss << "StartTime:" << it->m_start_time << ", ";
- oss << "EndTime:" << it->m_end_time << ", ";
- oss << "RemoteAddr:" << it->m_remote_addr << ", ";
- oss << "RequestId:" << it->m_request_id;
- std::cout << oss.str() << std::endl;
- }
- }
- std::cout
- << "===================================================================="
- << std::endl;
- }
- // 获取直播通道推流状态
- void GetLiveChannelStatus(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name,
- const std::string& channel_name) {
- qcloud_cos::GetLiveChannelStatusReq req(bucket_name, channel_name);
- qcloud_cos::GetLiveChannelStatusResp resp;
- qcloud_cos::CosResult result = cos.GetLiveChannelStatus(req, &resp);
- std::cout << "===================GetLiveChannelStatus====================="
- << std::endl;
- PrintResult(result, resp);
- if (result.IsSucc()) {
- const LiveChannelStatus& chan_status = resp.GetLiveChannelStatus();
- if (chan_status.m_status == "Idle") {
- std::cout << "Status:" << chan_status.m_status << std::endl;
- } else {
- std::stringstream oss;
- oss << "Status:" << chan_status.m_status << ", ";
- oss << "ConnectedTime:" << chan_status.m_connected_time << ", ";
- oss << "RemoteAddr:" << chan_status.m_remote_addr << ", ";
- oss << "RequestId:" << chan_status.m_request_id;
- if (chan_status.m_has_video) {
- oss << "Width:" << chan_status.m_video.m_width << ", ";
- oss << "Heigh:" << chan_status.m_video.m_heigh << ", ";
- oss << "FrameRate:" << chan_status.m_video.m_framerate << ", ";
- oss << "Bindwidth:" << chan_status.m_video.m_bandwidth << ", ";
- oss << "Codec:" << chan_status.m_video.m_codec << ", ";
- }
- if (chan_status.m_has_audio) {
- oss << "Bindwidth:" << chan_status.m_audio.m_bandwidth << ", ";
- oss << "SampleRate:" << chan_status.m_audio.m_samplerate << ", ";
- oss << "Codec:" << chan_status.m_audio.m_codec << ", ";
- }
- std::cout << oss.str() << std::endl;
- }
- }
- std::cout
- << "===================================================================="
- << std::endl;
- }
- // 下载直播通道点播播放列表
- void GetLiveChannelVodPlaylist(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name,
- const std::string& channel_name) {
- qcloud_cos::GetLiveChannelVodPlaylistReq req(bucket_name, channel_name);
- qcloud_cos::GetLiveChannelVodPlaylistResp resp;
- req.SetTime(time(NULL) - 10000, time(NULL));
- qcloud_cos::CosResult result = cos.GetLiveChannelVodPlaylist(req, &resp);
- std::cout
- << "===================GetLiveChannelVodPlaylist====================="
- << std::endl;
- PrintResult(result, resp);
- if (result.IsSucc()) {
- resp.WriteResultToFile("./playlist.m3u8");
- }
- std::cout
- << "===================================================================="
- << std::endl;
- }
- // 为直播通道产生点播播放列表
- void PostLiveChannelVodPlaylist(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name,
- const std::string& channel_name) {
- qcloud_cos::PostLiveChannelVodPlaylistReq req(bucket_name, channel_name);
- qcloud_cos::PostLiveChannelVodPlaylistResp resp;
- req.SetTime(time(NULL) - 10000, time(NULL));
- req.SetPlaylistName("newplaylist.m3u8");
- qcloud_cos::CosResult result = cos.PostLiveChannelVodPlaylist(req, &resp);
- std::cout
- << "===================PostLiveChannelVodPlaylist====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- // 删除直播通道
- void DeleteLiveChannel(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& channel_name) {
- qcloud_cos::DeleteLiveChannelReq req(bucket_name, channel_name);
- qcloud_cos::DeleteLiveChannelResp resp;
- qcloud_cos::CosResult result = cos.DeleteLiveChannel(req, &resp);
- std::cout << "===================DeleteLiveChannel====================="
- << std::endl;
- PrintResult(result, resp);
- std::cout
- << "===================================================================="
- << std::endl;
- }
- // 获取直播通道列表
- void ListLiveChannel(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
- qcloud_cos::ListLiveChannelReq req(bucket_name);
- qcloud_cos::ListLiveChannelResp resp;
- ListLiveChannelResult list_result;
- qcloud_cos::CosResult result;
- std::cout << "===================ListLiveChannel====================="
- << std::endl;
- do {
- req.SetMaxKeys(10);
- req.SetMarker(list_result.m_next_marker);
- result = cos.ListLiveChannel(req, &resp);
- PrintResult(result, resp);
- if (result.IsSucc()) {
- list_result = resp.GetListResult();
- std::stringstream oss;
- oss << "MaxKeys:" << list_result.m_max_keys << ", ";
- oss << "Marker:" << list_result.m_marker << ", ";
- oss << "Prefix:" << list_result.m_prefix << ", ";
- oss << "IsTruncated:" << list_result.m_is_truncated << ", ";
- oss << "NextMarker:" << list_result.m_next_marker << ", ";
- std::vector<LiveChannel>::const_iterator it =
- list_result.m_channels.begin();
- for (; it != list_result.m_channels.end(); ++it) {
- oss << "Name:" << it->m_name << ", LastModified:" << it->m_last_modified
- << std::endl;
- }
- std::cout << oss.str() << std::endl;
- resp.ClearResult();
- }
- } while (list_result.m_is_truncated == "true" && result.IsSucc());
- std::cout
- << "===================================================================="
- << std::endl;
- }
- void TestIntelligentTiering(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- {
- qcloud_cos::PutBucketIntelligentTieringReq req(bucket_name);
- qcloud_cos::PutBucketIntelligentTieringResp resp;
- qcloud_cos::CosResult result;
- std::cout << "===================PutIntelligentTiering====================="
- << std::endl;
- req.SetStatus(true);
- req.SetDays(60);
- result = cos.PutBucketIntelligentTiering(req, &resp);
- PrintResult(result, resp);
- std::cout << "============================================================="
- "======="
- << std::endl;
- }
- {
- qcloud_cos::GetBucketIntelligentTieringReq req(bucket_name);
- qcloud_cos::GetBucketIntelligentTieringResp resp;
- qcloud_cos::CosResult result;
- std::cout << "===================GetIntelligentTiering====================="
- << std::endl;
- result = cos.GetBucketIntelligentTiering(req, &resp);
- PrintResult(result, resp);
- if (result.IsSucc()) {
- std::cout << "Status:" << resp.GetStatus() << std::endl;
- std::cout << "Days:" << resp.GetDays() << std::endl;
- std::cout << "RequestFrequent:" << resp.GetRequestFrequent() << std::endl;
- }
- std::cout << "============================================================="
- "======="
- << std::endl;
- }
- }
- static void ResumableGetObject(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name,
- const std::string& object_name,
- const std::string& file_path) {
- qcloud_cos::GetObjectByFileReq req(bucket_name, object_name, file_path);
- qcloud_cos::GetObjectByFileResp resp;
- CosResult resule = cos.ResumableGetObject(req, &resp);
- if (resule.IsSucc()) {
- std::cout << "ResumableGetObject Succ." << std::endl;
- } else {
- std::cout << "ResumableGetObject Fail." << std::endl;
- }
- std::cout
- << "===================ResumableGetObject============================="
- << std::endl;
- PrintResult(resule, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- static void ProgressCallback(uint64_t transferred_size, uint64_t total_size,
- void* user_data) {
- qcloud_cos::ObjectReq* req = static_cast<qcloud_cos::ObjectReq*>(user_data);
- if (0 == transferred_size % 1048576) {
- std::cout << "ObjectName:" << req->GetObjectName()
- << ", TranferedSize:" << transferred_size
- << ",TotalSize:" << total_size << std::endl;
- }
- }
- static void MultiGetObjectAsyncDoneCallback(const SharedAsyncContext& context,
- void* user_data) {
- UNUSED_PARAM(user_data)
- std::cout << "MultiGetObjectAsyncDoneCallback, BucketName:"
- << context->GetBucketName()
- << ", ObjectName:" << context->GetObjectName()
- << ", LocalFile:" << context->GetLocalFilePath() << std::endl;
- // qcloud_cos::MultiGetObjectReq对应的响应为qcloud_cos::GetObjectByFileResp
- if (context->GetResult().IsSucc()) {
- // 获取响应
- std::cout << "AsyncMultiGetObject succeed" << std::endl;
- std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
- AsyncResp resp = context->GetAsyncResp();
- std::cout << "ETag:" << resp.GetEtag() << std::endl;
- std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
- } else {
- std::cout << "AsyncMultiGetObject failed" << std::endl;
- std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;
- }
- }
- // 异步下载对象,支持更新下载进度
- void AsyncMultiGetObject(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name,
- const std::string& object_name,
- const std::string& file_path) {
- qcloud_cos::AsyncMultiGetObjectReq req(bucket_name, object_name, file_path);
- // 设置进度回调
- req.SetTransferProgressCallback(&ProgressCallback);
- // 设置状态回调
- req.SetDoneCallback(&MultiGetObjectAsyncDoneCallback);
- // 设置私有数据
- req.SetUserData(&req);
- // 开始下载
- SharedAsyncContext context = cos.AsyncMultiGetObject(req);
- // 等待下载结束
- context->WaitUntilFinish();
- // 检查结果
- if (context->GetResult().IsSucc()) {
- // 获取响应
- std::cout << "AsyncMultiGetObject succeed" << std::endl;
- std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
- AsyncResp resp = context->GetAsyncResp();
- std::cout << "ETag:" << resp.GetEtag() << std::endl;
- std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
- } else {
- std::cout << "AsyncMultiGetObject failed" << std::endl;
- std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;
- }
- //context->WaitUntilFinish()阻塞完毕的逻辑是:异步线程在下载时文件落盘完毕。
- //此时您的主线程如果立即结束,异步线程可能正在进行资源释放、结束线程等过程。此时可能会产生crash。
- //此问题并不会对文件的下载造成影响,仅仅是体验上可能会出现意料之外的崩溃信息。
- //可以采用此策略避免crash。
- //或者可以采用下方AsyncMultiGetObjectWithTaskManager函数中的方式,使用能透传TaskManager的接口,使用join的方式避免crash。
- //特别注意,此处的TaskManager为全局共用,请务必保证在主线程结束前才能调用
- std::this_thread::sleep_for(std::chrono::seconds(1));
- }
- // 异步下载对象,支持更新下载进度
- // 回传taskManager对象,可用使用它等待异步线程彻底结束
- void AsyncMultiGetObjectWithTaskManager(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name,
- const std::string& object_name,
- const std::string& file_path) {
- qcloud_cos::AsyncMultiGetObjectReq req(bucket_name, object_name, file_path);
- // 设置进度回调
- req.SetTransferProgressCallback(&ProgressCallback);
- // 设置状态回调
- req.SetDoneCallback(&MultiGetObjectAsyncDoneCallback);
- // 设置私有数据
- req.SetUserData(&req);
- // 开始下载
- Poco::TaskManager* taskManager;
- qcloud_cos::SharedAsyncContext context = cos.AsyncMultiGetObject(req,taskManager);
- // 等待下载结束
- context->WaitUntilFinish();
- // 检查结果
- if (context->GetResult().IsSucc()) {
- // 获取响应
- std::cout << "AsyncMultiGetObject succeed" << std::endl;
- std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
- AsyncResp resp = context->GetAsyncResp();
- std::cout << "ETag:" << resp.GetEtag() << std::endl;
- std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
- } else {
- std::cout << "AsyncMultiGetObject failed" << std::endl;
- std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;
- }
- // 使用taskManager等待异步线程彻底结束
- (*taskManager).joinAll();
- }
- static void MultiPutObjectAsyncDoneCallback(const SharedAsyncContext& context,
- void* user_data) {
- UNUSED_PARAM(user_data)
- std::cout << "MultiPutObjectAsyncDoneCallback, BucketName:"
- << context->GetBucketName()
- << ", ObjectName:" << context->GetObjectName()
- << ", LocalFile:" << context->GetLocalFilePath() << std::endl;
- if (context->GetResult().IsSucc()) {
- // 获取响应
- std::cout << "AsyncMultiPutObject succeed" << std::endl;
- std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
- AsyncResp resp = context->GetAsyncResp();
- //std::cout << "Location:" << resp.GetLocation() << std::endl;
- //std::cout << "Bucket:" << resp.GetBucket() << std::endl;
- //std::cout << "Key:" << resp.GetKey() << std::endl;
- std::cout << "ETag:" << resp.GetEtag() << std::endl;
- std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
- } else {
- std::cout << "AsyncMultiGetObject failed" << std::endl;
- std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;
- }
- }
- // 异步多上传对象,支持更新上传进度
- void AsyncMultiPutObject(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name,
- const std::string& object_name,
- const std::string& local_file) {
- qcloud_cos::AsyncMultiPutObjectReq req(bucket_name, object_name, local_file);
- req.SetRecvTimeoutInms(1000 * 60);
- // 设置上传进度回调
- req.SetTransferProgressCallback(&ProgressCallback);
- // 设置上传状态回调
- req.SetDoneCallback(&MultiPutObjectAsyncDoneCallback);
- // 设置私有数据
- req.SetUserData(&req);
- // 开始上传
- SharedAsyncContext context = cos.AsyncMultiPutObject(req);
- // 等待上传结束
- std::cout << "wait finish..." << std::endl;
- context->WaitUntilFinish();
- // 检查结果
- if (context->GetResult().IsSucc()) {
- // 获取响应
- std::cout << "AsyncMultiPutObject succeed" << std::endl;
- std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
- AsyncResp resp = context->GetAsyncResp();
- //std::cout << "Location:" << resp.GetLocation() << std::endl;
- //std::cout << "Bucket:" << resp.GetBucket() << std::endl;
- //std::cout << "Key:" << resp.GetKey() << std::endl;
- std::cout << "ETag:" << resp.GetEtag() << std::endl;
- std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
- } else {
- std::cout << "AsyncMultiPutObject failed" << std::endl;
- std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;
- }
- }
- // 从目录上传文件
- void PutObjectsFromDirectory(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name,
- const std::string& directory_name) {
- PutObjectsByDirectoryReq req(bucket_name, directory_name);
- PutObjectsByDirectoryResp resp;
- CosResult result = cos.PutObjects(req, &resp);
- if (result.IsSucc()) {
- std::cout << "PutObjectsFromDirectory Succ." << std::endl;
- for (auto& r : resp.m_succ_put_objs) {
- std::cout << "file_name: " << r.m_file_name
- << ", object_name: " << r.m_object_name
- << ", ETag: " << r.m_cos_resp.GetEtag() << std::endl;
- }
- } else {
- std::cout << "PutObjectsFromDirectory Fail, ErrorMsg: "
- << result.GetErrorMsg() << std::endl;
- }
- }
- // 从目录上传文件到cos指定目录
- void PutObjectsFromDirectoryToCosPath(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name,
- const std::string& directory_name,
- const std::string& cos_path) {
- PutObjectsByDirectoryReq req(bucket_name, directory_name);
- req.SetCosPath(cos_path);
- PutObjectsByDirectoryResp resp;
- CosResult result = cos.PutObjects(req, &resp);
- if (result.IsSucc()) {
- std::cout << "PutObjectsFromDirectoryToCosPath Succ." << std::endl;
- for (auto& r : resp.m_succ_put_objs) {
- std::cout << "file_name: " << r.m_file_name
- << ", object_name: " << r.m_object_name
- << ", ETag: " << r.m_cos_resp.GetEtag() << std::endl;
- }
- } else {
- std::cout << "PutObjectsFromDirectoryToCosPath Fail, ErrorMsg: "
- << result.GetErrorMsg() << std::endl;
- }
- }
- // 创建目录
- void PutDirectory(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& directory_name) {
- PutDirectoryReq req(bucket_name, directory_name);
- PutDirectoryResp resp;
- CosResult result = cos.PutDirectory(req, &resp);
- if (result.IsSucc()) {
- std::cout << "PutDirectory Succ." << std::endl;
- } else {
- std::cout << "PutDirectory Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- }
- // 删除目录
- void DeleteDirectory(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& directory_name) {
- DeleteObjectsByPrefixReq req(bucket_name, directory_name);
- DeleteObjectsByPrefixResp resp;
- CosResult result = cos.DeleteObjects(req, &resp);
- if (result.IsSucc()) {
- std::cout << "DeleteDirectory Succ." << std::endl;
- } else {
- std::cout << "DeleteDirectory Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- std::cout << "Succ del objs:" << std::endl;
- for (auto& obj : resp.m_succ_del_objs) {
- std::cout << obj << std::endl;
- }
- }
- // 下载目录到本地
- void DownloadFromDirectory(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name,
- const std::string& server_dir_name,
- const std::string& local_dir_name) {
- qcloud_cos::GetBucketReq get_bucket_req(bucket_name);
- get_bucket_req.SetPrefix(server_dir_name);
- qcloud_cos::CosResult get_bucket_result;
- bool is_truncated = false;
- do {
- qcloud_cos::GetBucketResp get_bucket_resp;
- get_bucket_result = cos.GetBucket(get_bucket_req, &get_bucket_resp);
- if (get_bucket_result.IsSucc()) {
- std::vector<Content> contents = get_bucket_resp.GetContents();
- for (auto& content : contents) {
- std::cout << "====process " << content.m_key << std::endl;
- std::string local_file_name = local_dir_name + content.m_key;
- if (StringUtil::StringEndsWith(local_file_name, "/")) {
- continue;
- }
- size_t found_dir = local_file_name.find_last_of("/");
- if (found_dir) {
- std::string dirname = local_file_name.substr(0, found_dir);
- char* p_dirname = const_cast<char*>(dirname.c_str());
- struct stat buffer;
- if (stat(p_dirname, &buffer) != 0) {
- std::cout << "====mkdir " << dirname << std::endl;
- std::string mkdir_cmd = "mkdir -p " + dirname;
- system(mkdir_cmd.c_str());
- }
- }
- GetObjectByFileReq get_req(bucket_name, content.m_key, local_file_name);
- GetObjectByFileResp get_resp;
- CosResult get_result = cos.GetObject(get_req, &get_resp);
- if (get_result.IsSucc()) {
- std::cout << "====download " << content.m_key << " to "
- << local_file_name << " succeed" << std::endl;
- } else {
- std::cout << "====download " << content.m_key << " to "
- << local_file_name << " failed" << std::endl;
- }
- }
- get_bucket_req.SetMarker(
- get_bucket_resp.GetNextMarker()); // 设置下次列举的起始key
- is_truncated = get_bucket_resp.IsTruncated();
- }
- } while (get_bucket_result.IsSucc() && is_truncated);
- }
- // 指定前缀删除
- void DeleteObjectsByPrefix(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name,
- const std::string& prefix) {
- DeleteObjectsByPrefixReq req(bucket_name, prefix);
- DeleteObjectsByPrefixResp resp;
- CosResult result = cos.DeleteObjects(req, &resp);
- if (result.IsSucc()) {
- std::cout << "DeleteObjectsByPrefix Succ." << std::endl;
- } else {
- std::cout << "DeleteObjectsByPrefix Fail, ErrorMsg: "
- << result.GetErrorMsg() << std::endl;
- }
- std::cout << "Succ del objs:" << std::endl;
- for (auto& obj : resp.m_succ_del_objs) {
- std::cout << obj << std::endl;
- }
- }
- // 移动桶内对象
- void MoveObject(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& src_object, const std::string& dst_object) {
- MoveObjectReq req(bucket_name, src_object, dst_object);
- CosResult result = cos.MoveObject(req);
- if (result.IsSucc()) {
- std::cout << "MoveObject Succ." << std::endl;
- } else {
- std::cout << "MoveObject Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- }
- /*数据处理*/
- // 图片缩放
- void ImageThumbnail(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name,
- const std::string& local_file) {
- GetObjectByFileReq req(bucket_name, object_name, local_file);
- req.AddParam("imageMogr2/thumbnail/!50p", "");
- GetObjectByFileResp resp;
- CosResult result = cos.GetObject(req, &resp);
- if (result.IsSucc()) {
- std::cout << "ImageThumbnail Succ." << std::endl;
- } else {
- std::cout << "ImageThumbnail Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- }
- // 上传时处理图片
- void PutImage(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name, const std::string& local_file) {
- PutImageByFileReq req(bucket_name, object_name, local_file);
- PutImageByFileResp resp;
- PicOperation pic_operation;
- PicRules rule;
- // 锐化
- rule.fileid = "/" + object_name + "_sharpen.jpg";
- rule.rule = "imageMogr2/sharpen/70";
- pic_operation.AddRule(rule);
- // 旋转
- rule.fileid = "/" + object_name + "_rotate.jpg";
- rule.rule = "imageMogr2/rotate/90";
- pic_operation.AddRule(rule);
- req.SetPicOperation(pic_operation);
- CosResult result = cos.PutImage(req, &resp);
- if (result.IsSucc()) {
- std::cout << "PutImage Succ." << std::endl;
- std::cout << "ProcessResult: " << resp.GetUploadResult().to_string()
- << std::endl;
- } else {
- std::cout << "PutImage Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- std::cout << "===================PutImage============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 添加盲水印
- void AddWatermark(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name,
- const std::string& local_file) {
- PutImageByFileReq req(bucket_name, object_name, local_file);
- PutImageByFileResp resp;
- PicOperation pic_operation;
- PicRules rule;
- // 添加盲水印
- rule.fileid = "/" + object_name + "_watermark.jpg";
- // std::string image = "http://" + bucket_name +
- // ".cos.ap-guangzhou.myqcloud.com/" + object_name + "_watermark.jpg";
- rule.rule =
- "watermark/3/type/3/text/" + CodecUtil::Base64Encode("test wartermart");
- pic_operation.AddRule(rule);
- req.SetPicOperation(pic_operation);
- CosResult result = cos.PutImage(req, &resp);
- if (result.IsSucc()) {
- std::cout << "PutImage Succ." << std::endl;
- std::cout << "ProcessResult: " << resp.GetUploadResult().to_string()
- << std::endl;
- } else {
- std::cout << "PutImage Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- std::cout << "===================PutImage============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 下载图片时添加盲水印
- // https://cloud.tencent.com/document/product/436/46782
- void GetImageWithWatermark(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name,
- const std::string& object_name,
- const std::string& local_file) {
- GetObjectByFileReq req(bucket_name, object_name, local_file);
- GetObjectByFileResp resp;
- std::string watermark_text = "test"; // 盲水印文本
- std::string watermark_param =
- "watermark/3/type/3/text/" + CodecUtil::Base64Encode(watermark_text);
- req.AddParam(watermark_param, "");
- CosResult result = cos.GetObject(req, &resp);
- if (result.IsSucc()) {
- std::cout << "GetImageWithWatermark Succ." << std::endl;
- } else {
- std::cout << "GetImageWithWatermark Fail, ErrorMsg: "
- << result.GetErrorMsg() << std::endl;
- }
- std::cout
- << "===================GetImageWithWatermark============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 提取盲水印
- void ExtractWatermark(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name,
- const std::string& local_file) {
- PutImageByFileReq req(bucket_name, object_name, local_file);
- PutImageByFileResp resp;
- PicOperation pic_operation;
- PicRules rule;
- // 提取盲水印
- rule.fileid = "/" + object_name + "_watermark_extract.jpg";
- rule.rule = "watermark/4/type/3";
- pic_operation.AddRule(rule);
- req.SetPicOperation(pic_operation);
- CosResult result = cos.PutImage(req, &resp);
- if (result.IsSucc()) {
- std::cout << "PutImage Succ." << std::endl;
- std::cout << "ProcessResult: " << resp.GetUploadResult().to_string()
- << std::endl;
- } else {
- std::cout << "PutImage Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- std::cout << "===================PutImage============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 云上处理图片
- void CloudImageProcess(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name) {
- CloudImageProcessReq req(bucket_name, object_name);
- CloudImageProcessResp resp;
- PicOperation pic_operation;
- PicRules rule;
- rule.fileid = "/" + object_name + "_thumbnail.jpg";
- rule.rule = "imageMogr2/thumbnail/!30p";
- pic_operation.AddRule(rule);
- rule.fileid = "/" + object_name + "_cut.jpg";
- rule.rule = "imageMogr2/cut/300x300";
- pic_operation.AddRule(rule);
- req.SetPicOperation(pic_operation);
- CosResult result = cos.CloudImageProcess(req, &resp);
- if (result.IsSucc()) {
- std::cout << "CloudImageProcess Succ." << std::endl;
- std::cout << "ProcessResult: " << resp.GetUploadResult().to_string()
- << std::endl;
- } else {
- std::cout << "CloudImageProcess Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- std::cout
- << "===================CloudImageProcess============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 上传图片识别二维码
- void PutQRcode(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name, const std::string& local_file) {
- PutImageByFileReq req(bucket_name, object_name, local_file);
- PutImageByFileResp resp;
- PicOperation pic_operation;
- PicRules rule1;
- rule1.fileid = "/" + object_name + "_put_qrcode.jpg";
- rule1.rule = "QRcode/cover/1";
- pic_operation.AddRule(rule1);
- req.SetPicOperation(pic_operation);
- CosResult result = cos.PutImage(req, &resp);
- if (result.IsSucc()) {
- std::cout << "PutQRcode Succ." << std::endl;
- std::cout << "ProcessResult: " << resp.GetUploadResult().to_string()
- << std::endl;
- } else {
- std::cout << "PutQRcode Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- std::cout << "===================PutQRcode============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 下载图片时识别二维码
- void GetQRcode(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name) {
- GetQRcodeReq req(bucket_name, object_name);
- GetQRcodeResp resp;
- CosResult result = cos.GetQRcode(req, &resp);
- if (result.IsSucc()) {
- std::cout << "GetQRcode Succ." << std::endl;
- std::cout << "Result: " << resp.GetResult().to_string() << std::endl;
- } else {
- std::cout << "GetQRcode Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- std::cout << "===================GetQRcode============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 查询文档预览开通状态
- // https://cloud.tencent.com/document/product/436/54057
- void DescribeDocProcessBuckets(qcloud_cos::CosAPI& cos) {
- DescribeDocProcessBucketsReq req;
- DescribeDocProcessBucketsResp resp;
- req.SetRegions("ap-guangzhou");
- // 设置存储桶名称,以“,”分隔,支持多个存储桶,精确搜索
- // req.SetBucketNames("xxx");
- // 设置存储桶名称前缀,前缀搜索
- // req.SetBucketName("xxx");
- // 设置第几页
- // req.SetPageNumber();
- // 设置每页个数
- // req.SetPageSize();
- CosResult result = cos.DescribeDocProcessBuckets(req, &resp);
- if (result.IsSucc()) {
- std::cout << "DescribeDocProcessBuckets Succ." << std::endl;
- std::cout << "Result: " << resp.GetResult().to_string() << std::endl;
- } else {
- std::cout << "DescribeDocProcessBuckets Fail, ErrorMsg: "
- << result.GetErrorMsg() << std::endl;
- }
- std::cout << "===================DescribeDocProcessBuckets==================="
- "=========="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 文档预览
- // https://cloud.tencent.com/document/product/436/54058
- void DocPreview(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name, const std::string& local_file) {
- DocPreviewReq req(bucket_name, object_name, local_file);
- req.SetSrcType("docx");
- DocPreviewResp resp;
- CosResult result = cos.DocPreview(req, &resp);
- if (result.IsSucc()) {
- std::cout << "DocPreview Succ." << std::endl;
- } else {
- std::cout << "DocPreview Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- std::cout << "===================DocPreview============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 提交一个文档预览任务
- void CreateDocProcessJobs(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- CreateDocProcessJobsReq req(bucket_name);
- CreateDocProcessJobsResp resp;
- req.SetVerifyCert(false);
- Input input;
- input.object = "test.docx";
- req.SetInput(input);
- Operation operation;
- Output output;
- output.bucket = "wqingzhang";
- output.region = "ap-chongqing";
- output.object = "/test-ci/test-create-doc-process-${Number}";
- operation.output = output;
- req.SetOperation(operation);
- // req.SetQueueId("xxx");
- CosResult result = cos.CreateDocProcessJobs(req, &resp);
- if (result.IsSucc()) {
- std::cout << "CreateDocProcessJobs Succ." << std::endl;
- std::cout << "jobs_detail: " << resp.GetJobsDetail().to_string()
- << std::endl;
- } else {
- std::cout << "CreateDocProcessJobs Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- std::cout
- << "===================CreateDocProcessJobs============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 查询文档预览任务
- void DescribeDocProcessJob(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- DescribeDocProcessJobReq req(bucket_name);
- DescribeDocProcessJobResp resp;
- req.SetJobId("xxx");
- CosResult result = cos.DescribeDocProcessJob(req, &resp);
- if (result.IsSucc()) {
- std::cout << "DescribeDocProcessJobs Succ." << std::endl;
- std::cout << "jobs_detail: " << resp.GetJobsDetail().to_string()
- << std::endl;
- } else {
- std::cout << "DescribeDocProcessJobs Fail, ErrorMsg: "
- << result.GetErrorMsg() << std::endl;
- }
- std::cout << "===================DescribeDocProcessJobs======================"
- "======="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 拉取符合条件的文档预览任务
- void DescribeDocProcessJobs(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- DescribeDocProcessJobsReq req(bucket_name);
- DescribeDocProcessJobsResp resp;
- req.SetQueueId("xxx");
- CosResult result = cos.DescribeDocProcessJobs(req, &resp);
- if (result.IsSucc()) {
- std::cout << "DescribeDocProcessJobs Succ." << std::endl;
- std::vector<JobsDetail> jobs_detail = resp.GetJobsDetails();
- for (auto& job : jobs_detail) {
- std::cout << "job: " << job.to_string() << std::endl;
- }
- std::cout << "NextToken: " << resp.GetNextToken() << std::endl;
- } else {
- std::cout << "DescribeDocProcessJobs Fail, ErrorMsg: "
- << result.GetErrorMsg() << std::endl;
- }
- std::cout << "===================DescribeDocProcessJobs======================"
- "======="
- << std::endl;
- // PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 查询文档预览队列
- void DescribeDocProcessQueues(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- DescribeDocProcessQueuesReq req(bucket_name);
- DescribeDocProcessQueuesResp resp;
- CosResult result = cos.DescribeDocProcessQueues(req, &resp);
- if (result.IsSucc()) {
- std::cout << "DescribeDocProcessQueues Succ." << std::endl;
- std::cout << "ReqeustId: " << resp.GetRequestId()
- << ", PageNumber: " << resp.GetPageNumber()
- << ", PageSize: " << resp.GetPageSize()
- << ", QueueList: " << resp.GetQueueList().to_string()
- << ", NonExistPIDs: " << resp.GetNonExistPIDs().to_string()
- << std::endl;
- } else {
- std::cout << "DescribeDocProcessQueues Fail, ErrorMsg: "
- << result.GetErrorMsg() << std::endl;
- }
- std::cout << "===================DescribeDocProcessQueues===================="
- "========="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 更新文档预览队列
- void UpdateDocProcessQueue(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- UpdateDocProcessQueueReq req(bucket_name);
- UpdateDocProcessQueueResp resp;
- req.SetName("queue-doc-process-1");
- req.SetQueueId("xxx");
- req.SetState("Active");
- NotifyConfig notify_config;
- notify_config.url = "http://example.com";
- notify_config.state = "On";
- notify_config.type = "Url";
- notify_config.event = "TransCodingFinish";
- req.SetNotifyConfig(notify_config);
- CosResult result = cos.UpdateDocProcessQueue(req, &resp);
- if (result.IsSucc()) {
- std::cout << "UpdateDocProcessQueue Succ." << std::endl;
- std::cout << "ReqeustId: " << resp.GetRequestId()
- << ", QueueList: " << resp.GetQueueList().to_string()
- << std::endl;
- } else {
- std::cout << "UpdateDocProcessQueue Fail, ErrorMsg: "
- << result.GetErrorMsg() << std::endl;
- }
- std::cout
- << "===================UpdateDocProcessQueue============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 获取对象不带签名的URL
- void GetObjectUrl(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name) {
- std::cout << "https url: " << cos.GetObjectUrl(bucket_name, object_name)
- << std::endl;
- std::cout << "http url: " << cos.GetObjectUrl(bucket_name, object_name, false)
- << std::endl;
- std::cout << "ap-beijing https url: "
- << cos.GetObjectUrl(bucket_name, object_name, true, "ap-beijing")
- << std::endl;
- std::cout << "ap-beijing http url: "
- << cos.GetObjectUrl(bucket_name, object_name, false, "ap-beijing")
- << std::endl;
- }
- // 查询已经开通媒体处理功能的存储桶
- // https://cloud.tencent.com/document/product/436/48988
- void DescribeMediaBuckets(qcloud_cos::CosAPI& cos) {
- DescribeMediaBucketsReq req;
- DescribeMediaBucketsResp resp;
- req.SetRegions("ap-guangzhou");
- // 设置存储桶名称,以“,”分隔,支持多个存储桶,精确搜索
- // req.SetBucketNames("xxx");
- // 设置存储桶名称前缀,前缀搜索
- // req.SetBucketName("xxx");
- // 设置第几页
- // req.SetPageNumber();
- // 设置每页个数
- // req.SetPageSize();
- CosResult result = cos.DescribeMediaBuckets(req, &resp);
- if (result.IsSucc()) {
- std::cout << "DescribeMediaBuckets Succ." << std::endl;
- std::cout << "Result: " << resp.GetResult().to_string() << std::endl;
- } else {
- std::cout << "DescribeMediaBuckets Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- std::cout << "===================DescribeMediaBuckets==================="
- "=========="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 获取媒体文件某个时间的截图
- // https://cloud.tencent.com/document/product/436/55671
- void GetSnapshot(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name,
- const std::string& local_file) {
- GetSnapshotReq req(bucket_name, object_name, local_file);
- GetSnapshotResp resp;
- req.SetTime(100);
- // req.SetWitdh();
- // req.SetHeight();
- // req.SetFormat();
- CosResult result = cos.GetSnapshot(req, &resp);
- if (result.IsSucc()) {
- std::cout << "GetSnapshot Succ." << std::endl;
- } else {
- std::cout << "GetSnapshot Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- std::cout << "===================GetSnapshot==================="
- "=========="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 获取媒体信息
- // https://cloud.tencent.com/document/product/436/55672
- void GetMediaInfo(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name) {
- GetMediaInfoReq req(bucket_name, object_name);
- GetMediaInfoResp resp;
- CosResult result = cos.GetMediaInfo(req, &resp);
- if (result.IsSucc()) {
- std::cout << "GetMediaInfo Succ." << std::endl;
- std::cout << "Result: " << resp.GetResult().to_string() << std::endl;
- } else {
- std::cout << "GetMediaInfo Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- std::cout << "===================GetMediaInfo==================="
- "=========="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 获取私有 M3U8 ts 资源的下载授权
- // https://cloud.tencent.com/document/product/436/63740
- void GetPm3u8(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name,
- const std::string& local_file) {
- GetPm3u8Req req(bucket_name, object_name, local_file);
- GetPm3u8Resp resp;
- req.SetExpires(3600);
- CosResult result = cos.GetPm3u8(req, &resp);
- if (result.IsSucc()) {
- std::cout << "GetPm3u8 Succ." << std::endl;
- } else {
- std::cout << "GetPm3u8 Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- std::cout << "===================GetPm3u8==================="
- "=========="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 查询媒体处理队列
- void DescribeMediaQueues(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- DescribeMediaQueuesReq req(bucket_name);
- DescribeQueuesResp resp;
- CosResult result = cos.DescribeMediaQueues(req, &resp);
- if (result.IsSucc()) {
- std::cout << "DescribeMediaQueues Succ." << std::endl;
- std::cout << "ReqeustId: " << resp.GetRequestId()
- << ", PageNumber: " << resp.GetPageNumber()
- << ", PageSize: " << resp.GetPageSize()
- << ", QueueList: " << resp.GetQueueList().to_string()
- << ", NonExistPIDs: " << resp.GetNonExistPIDs().to_string()
- << std::endl;
- } else {
- std::cout << "DescribeMediaQueues Fail, ErrorMsg: "
- << result.GetErrorMsg() << std::endl;
- }
- std::cout << "===================DescribeMediaQueues===================="
- "========="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 更新媒体处理队列
- void UpdateMediaQueue(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- UpdateMediaQueueReq req(bucket_name);
- UpdateQueueResp resp;
- req.SetName("queue-media-process-1");
- req.SetQueueId("xxx");
- req.SetState("Active");
- NotifyConfig notify_config;
- notify_config.url = "http://example.com";
- notify_config.state = "On";
- notify_config.type = "Url";
- notify_config.event = "TransCodingFinish";
- req.SetNotifyConfig(notify_config);
- CosResult result = cos.UpdateMediaQueue(req, &resp);
- if (result.IsSucc()) {
- std::cout << "UpdateMediaQueue Succ." << std::endl;
- std::cout << "ReqeustId: " << resp.GetRequestId()
- << ", QueueList: " << resp.GetQueueList().to_string()
- << std::endl;
- } else {
- std::cout << "UpdateMediaQueue Fail, ErrorMsg: "
- << result.GetErrorMsg() << std::endl;
- }
- std::cout
- << "===================UpdateMediaQueue============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
-
- // 创建文件解压缩任务
- void CreateFileUncompressJobs(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- CreateDataProcessJobsReq req(bucket_name);
- CreateDataProcessJobsResp resp;
- JobsOptions opt;
- opt.input.bucket = bucket_name;
- opt.input.region = "ap-chongqing";
- opt.input.object = "test.zip";
- opt.tag = "FileUncompress";
-
- // 文件解压参数
- // 指定解压后输出文件的前缀,不填则默认保存在存储桶根路径,非必选
- opt.operation.file_uncompress_config.prefix = "output/";
- // 指定解压后的文件路径是否需要替换前缀,可选值如下
- // 0:不添加额外的前缀,解压缩将保存在Prefix指定的路径下(不会保留压缩包的名称,仅将压缩包内的文件保存至指定的路径)
- // 1:以压缩包本身的名称作为前缀,解压缩将保存在Prefix指定的路径下
- // 2:以压缩包完整路径作为前缀,此时如果不指定Prefix,就是解压到压缩包所在的当前路径(包含压缩包本身名称)
- // 非必选参数,默认 0
- opt.operation.file_uncompress_config.prefix_replaced = "1";
- // 解压密钥,传入时需先经过 base64 编码,非必选
- // opt.operation.file_uncompress_config.un_compress_key = "MTIzNDU2Nzg5MA==";
-
- opt.operation.output.bucket = bucket_name;
- opt.operation.output.region = "ap-chongqing";
- req.setOperation(opt);
- CosResult result = cos.CreateDataProcessJobs(req, &resp);
- if (result.IsSucc()) {
- std::cout << "CreateFileProcessJobs Succ." << std::endl;
- } else {
- std::cout << "CreateFileProcessJobs Fail, ErrorMsg: "
- << result.GetErrorMsg() << std::endl;
- }
- std::cout
- << "===================CreateFileProcessJobs============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 查询异步任务
- void DescribeDataProcessJobs(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- DescribeDataProcessJobReq req(bucket_name);
- DescribeDataProcessJobResp resp;
- // 任务ID
- req.SetJobId("jf5b1f35c4e2411ee9c3ca7d7d6d3bc6f");
- CosResult result = cos.DescribeDataProcessJob(req, &resp);
- if (result.IsSucc()) {
- std::cout << "DescribeDataProcessJob Succ." << std::endl;
- } else {
- std::cout << "DescribeDataProcessJob Fail, ErrorMsg: "
- << result.GetErrorMsg() << std::endl;
- }
- std::cout
- << "===================DescribeDataProcessJob============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 取消异步任务
- void CancelDataProcessJob(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- CancelDataProcessJobReq req(bucket_name);
- CancelDataProcessJobResp resp;
- // 任务ID
- req.SetJobId("XXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
- CosResult result = cos.CancelDataProcessJob(req, &resp);
- if (result.IsSucc()) {
- std::cout << "CancelDataProcessJob Succ." << std::endl;
- } else {
- std::cout << "CancelDataProcessJob Fail, ErrorMsg: "
- << result.GetErrorMsg() << std::endl;
- }
- std::cout
- << "===================CancelDataProcessJob============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 创建视频截图任务
- void CreateSnapshotJobs(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- CreateDataProcessJobsReq req(bucket_name);
- CreateDataProcessJobsResp resp;
- JobsOptions opt;
- opt.input.bucket = bucket_name;
- opt.input.region = "ap-chongqing";
- opt.input.object = "test.mp4";
- opt.tag = "Snapshot";
- // opt.operation.template_id = "XXXXXXXXXXXXXXXXXXX";
- opt.operation.snapshot.mode = "Interval";
- opt.operation.snapshot.start = "0";
- opt.operation.snapshot.time_interval = "5";
- opt.operation.snapshot.count = "3";
-
- opt.operation.output.bucket = bucket_name;
- opt.operation.output.region = "ap-chongqing";
- opt.operation.output.object = "snapshot/test-${number}.jpg";
- req.setOperation(opt);
- CosResult result = cos.CreateDataProcessJobs(req, &resp);
- if (result.IsSucc()) {
- std::cout << "CreateDataProcessJobs Succ." << std::endl;
- } else {
- std::cout << "CreateDataProcessJobs Fail, ErrorMsg: "
- << result.GetErrorMsg() << std::endl;
- }
- std::cout
- << "===================CreateDataProcessJobs============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 创建视频转码任务
- void CreateTranscodeJobs(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- CreateDataProcessJobsReq req(bucket_name);
- CreateDataProcessJobsResp resp;
- JobsOptions opt;
- opt.input.bucket = bucket_name;
- opt.input.region = "ap-chongqing";
- opt.input.object = "test.mp4";
- opt.tag = "Transcode";
- // 使用转码模版ID提交任务
- // opt.operation.template_id = "t1460606b9752148c4ab182f55163ba7cd";
- // 使用转码参数提交任务
- opt.operation.transcode.container.format = "mp4";
- opt.operation.transcode.video.codec = "H.264";
- opt.operation.transcode.video.profile = "high";
- opt.operation.transcode.video.bit_rate = "1000";
- opt.operation.transcode.video.width = "1280";
- opt.operation.transcode.video.fps = "30";
- opt.operation.transcode.video.preset = "medium";
- opt.operation.transcode.audio.codec = "aac";
- opt.operation.transcode.audio.sample_format = "fltp";
- opt.operation.transcode.audio.bit_rate = "128";
- opt.operation.transcode.audio.channels = "4";
- opt.operation.transcode.trans_config.adj_dar_method = "scale";
- opt.operation.transcode.trans_config.is_check_audio_bit_rate = "false";
- opt.operation.transcode.trans_config.reso_adj_method = "1";
- opt.operation.transcode.time_interval.start = "0";
- opt.operation.transcode.time_interval.duration = "10";
- // 混音参数
- AudioMix audio_mix_1 = AudioMix();
- audio_mix_1.audio_source = "https://test-12345.cos.ap-chongqing.myqcloud.com/test.wav";
- audio_mix_1.mix_mode = "Once";
- audio_mix_1.replace = "true";
- audio_mix_1.effect_config.enable_start_fade_in = "true";
- audio_mix_1.effect_config.start_fade_in_time = "3";
- audio_mix_1.effect_config.enable_end_fade_out = "false";
- audio_mix_1.effect_config.end_fade_out_time = "0";
- audio_mix_1.effect_config.enable_bgm_fade = "true";
- audio_mix_1.effect_config.bgm_fade_time = "1.7";
- opt.operation.transcode.audio_mix_array.push_back(audio_mix_1);
- AudioMix audio_mix_2 = AudioMix();
- audio_mix_2.audio_source = "https://test-12345.cos.ap-chongqing.myqcloud.com/test.wav";
- audio_mix_2.mix_mode = "Once";
- audio_mix_2.replace = "true";
- audio_mix_2.effect_config.enable_start_fade_in = "true";
- audio_mix_2.effect_config.start_fade_in_time = "3";
- audio_mix_2.effect_config.enable_end_fade_out = "false";
- audio_mix_2.effect_config.end_fade_out_time = "0";
- audio_mix_2.effect_config.enable_bgm_fade = "true";
- audio_mix_2.effect_config.bgm_fade_time = "1.7";
- opt.operation.transcode.audio_mix_array.push_back(audio_mix_2);
- // 去除水印参数
- opt.operation.remove_watermark.dx = "150";
- opt.operation.remove_watermark.dy = "150";
- opt.operation.remove_watermark.width = "75";
- opt.operation.remove_watermark.height = "75";
- // 数字水印参数
- opt.operation.digital_watermark.type = "Text";
- opt.operation.digital_watermark.message = "12345";
- opt.operation.digital_watermark.version = "V1";
- opt.operation.digital_watermark.ignore_error = "false";
- // 水印参数
- // 使用水印模版ID
- // opt.operation.watermark_template_id.push_back("t1318c5f428d474afba1797f84091cbe22");
- // opt.operation.watermark_template_id.push_back("t1318c5f428d474afba1797f84091cbe23");
- // opt.operation.watermark_template_id.push_back("t1318c5f428d474afba1797f84091cbe24");
- // 使用水印参数
- Watermark watermark_1 = Watermark();
- watermark_1.type = "Text";
- watermark_1.loc_mode = "Absolute";
- watermark_1.dx = "128";
- watermark_1.dy = "128";
- watermark_1.pos = "TopRight";
- watermark_1.start_time = "0";
- watermark_1.end_time = "100.5";
- watermark_1.text.text = "水印内容";
- watermark_1.text.font_size = "30";
- watermark_1.text.font_color = "0x0B172F";
- watermark_1.text.font_type = "simfang.ttf";
- watermark_1.text.transparency = "30";
- opt.operation.watermarks.push_back(watermark_1);
- Watermark watermark_2 = Watermark();
- watermark_2.type = "Image";
- watermark_2.loc_mode = "Absolute";
- watermark_2.dx = "128";
- watermark_2.dy = "128";
- watermark_2.pos = "TopRight";
- watermark_2.start_time = "0";
- watermark_2.end_time = "100.5";
- watermark_2.image.url = "http://test-1234567890.ci.ap-chongqing.myqcloud.com/shuiyin_2.png";
- watermark_2.image.mode = "Proportion";
- watermark_2.image.width = "10";
- watermark_2.image.height = "10";
- watermark_2.image.transparency = "30";
- opt.operation.watermarks.push_back(watermark_2);
- opt.operation.output.bucket = bucket_name;
- opt.operation.output.region = "ap-chongqing";
- opt.operation.output.object = "output/test.mp4";
- req.setOperation(opt);
- CosResult result = cos.CreateDataProcessJobs(req, &resp);
- if (result.IsSucc()) {
- std::cout << "CreateDataProcessJobs Succ." << std::endl;
- } else {
- std::cout << "CreateDataProcessJobs Fail, ErrorMsg: "
- << result.GetErrorMsg() << std::endl;
- }
- std::cout
- << "===================CreateDataProcessJobs============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 创建视频转动图任务
- void CreateAnimationJobs(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- CreateDataProcessJobsReq req(bucket_name);
- CreateDataProcessJobsResp resp;
- JobsOptions opt;
- opt.input.bucket = bucket_name;
- opt.input.region = "ap-chongqing";
- opt.input.object = "test.mp4";
- opt.tag = "Animation";
- // 使用模版ID提交任务
- // opt.operation.template_id = "XXXXXXXXXXXXXXXXXXX";
- // 使用参数形式提交任务
- opt.operation.animation.container.format = "gif";
- opt.operation.animation.video.codec = "gif";
- opt.operation.animation.video.width = "1280";
- opt.operation.animation.video.height = "960";
- opt.operation.animation.video.fps = "15";
- opt.operation.animation.video.animate_only_keep_key_frame = "true";
- opt.operation.animation.time_interval.start = "0";
- opt.operation.animation.time_interval.duration = "60";
-
- opt.operation.output.bucket = bucket_name;
- opt.operation.output.region = "ap-chongqing";
- opt.operation.output.object = "output/out.${ext}";
- req.setOperation(opt);
- CosResult result = cos.CreateDataProcessJobs(req, &resp);
- if (result.IsSucc()) {
- std::cout << "CreateDataProcessJobs Succ." << std::endl;
- } else {
- std::cout << "CreateDataProcessJobs Fail, ErrorMsg: "
- << result.GetErrorMsg() << std::endl;
- }
- std::cout
- << "===================CreateDataProcessJobs============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 创建视频拼接任务
- void CreateConcatJobs(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- CreateDataProcessJobsReq req(bucket_name);
- CreateDataProcessJobsResp resp;
- JobsOptions opt;
- opt.input.bucket = bucket_name;
- opt.input.region = "ap-chongqing";
- opt.input.object = "test.mp4";
- opt.tag = "Concat";
- // 使用模版ID提交任务
- // opt.operation.template_id = "XXXXXXXXXXXXXXXXXXX";
- // 使用参数形式提交任务
- ConcatFragment fragment1 = ConcatFragment();
- fragment1.url = "http://test-12345.cos.ap-chongqing.myqcloud.com/start.mp4";
- opt.operation.concat.concat_fragment.push_back(fragment1);
- ConcatFragment fragment2 = ConcatFragment();
- fragment2.url = "http://test-12345.cos.ap-chongqing.myqcloud.com/end.mp4";
- opt.operation.concat.concat_fragment.push_back(fragment2);
- opt.operation.concat.audio.codec = "mp3";
- opt.operation.concat.video.codec = "H.264";
- opt.operation.concat.video.bit_rate = "1000";
- opt.operation.concat.video.width = "1280";
- opt.operation.concat.video.height = "720";
- opt.operation.concat.video.fps = "30";
- opt.operation.concat.container.format = "mp4";
- // 混音参数
- AudioMix audio_mix_1 = AudioMix();
- audio_mix_1.audio_source = "https://test-xxx.cos.ap-chongqing.myqcloud.com/mix1.mp3";
- audio_mix_1.mix_mode = "Once";
- audio_mix_1.replace = "true";
- audio_mix_1.effect_config.enable_start_fade_in = "true";
- audio_mix_1.effect_config.start_fade_in_time = "3";
- audio_mix_1.effect_config.enable_end_fade_out = "false";
- audio_mix_1.effect_config.end_fade_out_time = "0";
- audio_mix_1.effect_config.enable_bgm_fade = "true";
- audio_mix_1.effect_config.bgm_fade_time = "1.7";
- opt.operation.transcode.audio_mix_array.push_back(audio_mix_1);
- AudioMix audio_mix_2 = AudioMix();
- audio_mix_2.audio_source = "https://test-xxx.cos.ap-chongqing.myqcloud.com/mix2.mp3";
- audio_mix_2.mix_mode = "Once";
- audio_mix_2.replace = "true";
- audio_mix_2.effect_config.enable_start_fade_in = "true";
- audio_mix_2.effect_config.start_fade_in_time = "3";
- audio_mix_2.effect_config.enable_end_fade_out = "false";
- audio_mix_2.effect_config.end_fade_out_time = "0";
- audio_mix_2.effect_config.enable_bgm_fade = "true";
- audio_mix_2.effect_config.bgm_fade_time = "1.7";
- opt.operation.transcode.audio_mix_array.push_back(audio_mix_2);
- opt.operation.output.bucket = bucket_name;
- opt.operation.output.region = "ap-chongqing";
- opt.operation.output.object = "output/out.${ext}";
- req.setOperation(opt);
- CosResult result = cos.CreateDataProcessJobs(req, &resp);
- if (result.IsSucc()) {
- std::cout << "CreateDataProcessJobs Succ." << std::endl;
- } else {
- std::cout << "CreateDataProcessJobs Fail, ErrorMsg: "
- << result.GetErrorMsg() << std::endl;
- }
- std::cout
- << "===================CreateDataProcessJobs============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 创建智能封面任务
- void CreateSmartCoverJobs(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- CreateDataProcessJobsReq req(bucket_name);
- CreateDataProcessJobsResp resp;
- JobsOptions opt;
- opt.input.bucket = bucket_name;
- opt.input.region = "ap-chongqing";
- opt.input.object = "test.mp4";
- opt.tag = "SmartCover";
- // 使用模版ID提交任务
- // opt.operation.template_id = "XXXXXXXXXXXXXXXXXXX";
- // 使用参数形式提交任务
- opt.operation.smart_cover.format = "jpg";
- opt.operation.smart_cover.width = "1280";
- opt.operation.smart_cover.height = "960";
- opt.operation.smart_cover.count = "5";
- opt.operation.smart_cover.delete_duplicates = "true";
-
- opt.operation.output.bucket = bucket_name;
- opt.operation.output.region = "ap-chongqing";
- opt.operation.output.object = "output/out-${number}.jpg";
- req.setOperation(opt);
- CosResult result = cos.CreateDataProcessJobs(req, &resp);
- if (result.IsSucc()) {
- std::cout << "CreateDataProcessJobs Succ." << std::endl;
- } else {
- std::cout << "CreateDataProcessJobs Fail, ErrorMsg: "
- << result.GetErrorMsg() << std::endl;
- }
- std::cout
- << "===================CreateDataProcessJobs============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 创建数字水印任务任务
- void CreateDigitalWatermarkJobs(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- CreateDataProcessJobsReq req(bucket_name);
- CreateDataProcessJobsResp resp;
- JobsOptions opt;
- opt.input.bucket = bucket_name;
- opt.input.region = "ap-chongqing";
- opt.input.object = "test.mp4";
- opt.tag = "DigitalWatermark";
- // 使用参数形式提交任务
- opt.operation.digital_watermark.type = "Text";
- opt.operation.digital_watermark.version = "V1";
- opt.operation.digital_watermark.message = "水印内容";
-
- opt.operation.output.bucket = bucket_name;
- opt.operation.output.region = "ap-chongqing";
- opt.operation.output.object = "output/out.${ext}";
- req.setOperation(opt);
- CosResult result = cos.CreateDataProcessJobs(req, &resp);
- if (result.IsSucc()) {
- std::cout << "CreateDataProcessJobs Succ." << std::endl;
- } else {
- std::cout << "CreateDataProcessJobs Fail, ErrorMsg: "
- << result.GetErrorMsg() << std::endl;
- }
- std::cout
- << "===================CreateDataProcessJobs============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 创建提取数字水印任务任务
- void CreateExtractDigitalWatermarkJobs(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- CreateDataProcessJobsReq req(bucket_name);
- CreateDataProcessJobsResp resp;
- JobsOptions opt;
- opt.input.bucket = bucket_name;
- opt.input.region = "ap-chongqing";
- opt.input.object = "test.mp4";
- opt.tag = "ExtractDigitalWatermark";
- // 使用参数形式提交任务
- opt.operation.extract_digital_watermark.type = "Text";
- opt.operation.extract_digital_watermark.version = "V1";
-
- req.setOperation(opt);
- CosResult result = cos.CreateDataProcessJobs(req, &resp);
- if (result.IsSucc()) {
- std::cout << "CreateDataProcessJobs Succ." << std::endl;
- } else {
- std::cout << "CreateDataProcessJobs Fail, ErrorMsg: "
- << result.GetErrorMsg() << std::endl;
- }
- std::cout
- << "===================CreateDataProcessJobs============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 创建精彩集锦任务任务
- void CreateVideoMontageJobs(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- CreateDataProcessJobsReq req(bucket_name);
- CreateDataProcessJobsResp resp;
- JobsOptions opt;
- opt.input.bucket = bucket_name;
- opt.input.region = "ap-chongqing";
- opt.input.object = "test.mp4";
- opt.tag = "VideoMontage";
- // 使用模版ID提交任务
- // opt.operation.template_id = "XXXXXXXXXXXXXXXXXXX";
- // 使用参数形式提交任务
- opt.operation.video_montage.container.format = "mp4";
- opt.operation.video_montage.video.codec = "H.264";
- opt.operation.video_montage.video.width = "1280";
- opt.operation.video_montage.video.bit_rate = "1000";
- opt.operation.video_montage.audio.codec = "aac";
- opt.operation.video_montage.audio.sample_format = "fltp";
- opt.operation.video_montage.audio.bit_rate = "128";
- opt.operation.video_montage.audio.channels = "4";
- // 混音参数
- AudioMix audio_mix_1 = AudioMix();
- audio_mix_1.audio_source = "https://test-xxx.cos.ap-chongqing.myqcloud.com/mix1.mp3";
- audio_mix_1.mix_mode = "Once";
- audio_mix_1.replace = "true";
- audio_mix_1.effect_config.enable_start_fade_in = "true";
- audio_mix_1.effect_config.start_fade_in_time = "3";
- audio_mix_1.effect_config.enable_end_fade_out = "false";
- audio_mix_1.effect_config.end_fade_out_time = "0";
- audio_mix_1.effect_config.enable_bgm_fade = "true";
- audio_mix_1.effect_config.bgm_fade_time = "1.7";
- opt.operation.transcode.audio_mix_array.push_back(audio_mix_1);
- AudioMix audio_mix_2 = AudioMix();
- audio_mix_2.audio_source = "https://test-xxx.cos.ap-chongqing.myqcloud.com/mix2.mp3";
- audio_mix_2.mix_mode = "Once";
- audio_mix_2.replace = "true";
- audio_mix_2.effect_config.enable_start_fade_in = "true";
- audio_mix_2.effect_config.start_fade_in_time = "3";
- audio_mix_2.effect_config.enable_end_fade_out = "false";
- audio_mix_2.effect_config.end_fade_out_time = "0";
- audio_mix_2.effect_config.enable_bgm_fade = "true";
- audio_mix_2.effect_config.bgm_fade_time = "1.7";
- opt.operation.transcode.audio_mix_array.push_back(audio_mix_2);
-
- opt.operation.output.bucket = bucket_name;
- opt.operation.output.region = "ap-chongqing";
- opt.operation.output.object = "output/out.${ext}";
- req.setOperation(opt);
- CosResult result = cos.CreateDataProcessJobs(req, &resp);
- if (result.IsSucc()) {
- std::cout << "CreateDataProcessJobs Succ." << std::endl;
- } else {
- std::cout << "CreateDataProcessJobs Fail, ErrorMsg: "
- << result.GetErrorMsg() << std::endl;
- }
- std::cout
- << "===================CreateDataProcessJobs============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 创建人声分离任务
- void CreateVoiceSeparateJobs(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- CreateDataProcessJobsReq req(bucket_name);
- CreateDataProcessJobsResp resp;
- JobsOptions opt;
- opt.input.bucket = bucket_name;
- opt.input.region = "ap-chongqing";
- opt.input.object = "test.mp3";
- opt.tag = "VoiceSeparate";
- // 使用模版ID提交任务
- // opt.operation.template_id = "XXXXXXXXXXXXXXXXXXX";
- // 使用参数形式提交任务
- opt.operation.voice_separate.audio_mode = "IsAudio";
- opt.operation.voice_separate.audio_config.codec = "aac";
- opt.operation.voice_separate.audio_config.sample_rate = "11025";
- opt.operation.voice_separate.audio_config.bit_rate = "16";
- opt.operation.voice_separate.audio_config.channels = "2";
-
- opt.operation.output.bucket = bucket_name;
- opt.operation.output.region = "ap-chongqing";
- opt.operation.output.object = "output/out.${ext}";
- opt.operation.output.au_object = "output/au_object.${ext}";
- req.setOperation(opt);
- CosResult result = cos.CreateDataProcessJobs(req, &resp);
- if (result.IsSucc()) {
- std::cout << "CreateDataProcessJobs Succ." << std::endl;
- } else {
- std::cout << "CreateDataProcessJobs Fail, ErrorMsg: "
- << result.GetErrorMsg() << std::endl;
- }
- std::cout
- << "===================CreateDataProcessJobs============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 创建转封装任务
- void CreateSegmentJobs(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- CreateDataProcessJobsReq req(bucket_name);
- CreateDataProcessJobsResp resp;
- JobsOptions opt;
- opt.input.bucket = bucket_name;
- opt.input.region = "ap-chongqing";
- opt.input.object = "test.mp4";
- opt.tag = "Segment";
- // 使用模版ID提交任务
- // opt.operation.template_id = "XXXXXXXXXXXXXXXXXXX";
- // 使用参数形式提交任务
- opt.operation.segment.duration = "15";
- opt.operation.segment.format = "mp4";
- opt.operation.output.bucket = bucket_name;
- opt.operation.output.region = "ap-chongqing";
- opt.operation.output.object = "output/out-${number}";
- req.setOperation(opt);
- CosResult result = cos.CreateDataProcessJobs(req, &resp);
- if (result.IsSucc()) {
- std::cout << "CreateDataProcessJobs Succ." << std::endl;
- } else {
- std::cout << "CreateDataProcessJobs Fail, ErrorMsg: "
- << result.GetErrorMsg() << std::endl;
- }
- std::cout
- << "===================CreateDataProcessJobs============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 查询文件解压缩任务
- void DescribeFileUncompressJobs(qcloud_cos::CosAPI& cos,
- const std::string& bucket_name) {
- DescribeDataProcessJobReq req(bucket_name);
- DescribeDataProcessJobResp resp;
- // 任务ID
- req.SetJobId("XXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
- CosResult result = cos.DescribeDataProcessJob(req, &resp);
- if (result.IsSucc()) {
- std::cout << "DescribeDataProcessJob Succ." << std::endl;
- } else {
- std::cout << "DescribeDataProcessJob Fail, ErrorMsg: "
- << result.GetErrorMsg() << std::endl;
- }
- std::cout
- << "===================DescribeDataProcessJob============================="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 图片同步审核
- // https://cloud.tencent.com/document/product/436/45434
- void GetImageAuditing(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name) {
- GetImageAuditingReq req(bucket_name);
- GetImageAuditingResp resp;
- // 审核对象
- req.SetObjectKey(object_name);
- // 审核图片链接,Object和URL只用设置一个,设置URL时默认审核URL
- // req.SetDetectUrl("https://exampleurl.com");
- // 审核策略,不填为默认审核策略
- // req.SetBizType("xxxxxxxxxxxxxxxxxxxxxxxxx");
- // 该字段在结果中返回原始内容
- req.SetDataId("data_id_example");
- // 审核场景类型
- req.SetDetectType("ads,porn,politics,terrorism");
- // GIF截帧间隔,单位秒
- req.SetInterval(1);
- // GIF最大截帧数量
- req.SetMaxFrames(2);
- // 超过大小限制的图片是否压缩,0 不压缩, 1 压缩
- // req.SetLargeImageDetect(0);
- CosResult result = cos.GetImageAuditing(req, &resp);
- if (result.IsSucc()) {
- std::cout << "GetImageAuditing Succ." << std::endl;
- std::cout << "Result: \n" << resp.GetJobsDetail().to_string() << std::endl;
- } else {
- std::cout << "GetImageAuditing Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- std::cout << "===================GetImageAuditing==================="
- "=========="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 图片批量审核
- // https://cloud.tencent.com/document/product/436/63593
- void BatchImageAuditing(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::vector<std::string>& object_names) {
- BatchImageAuditingReq req(bucket_name);
- BatchImageAuditingResp resp;
- for(std::vector<std::string>::const_iterator it = object_names.begin();
- it != object_names.end(); ++it) {
- AuditingInput input = AuditingInput();
- input.SetObject(*it);
- // 审核图片链接,Object和URL只用设置一个,设置URL时默认审核URL
- // input.SetUrl("https://exampleurl.com");
- // 图片标识,该字段在结果中返回原始内容
- input.SetDataId("data_id_example");
- // GIF截帧间隔,单位秒
- // input.SetInterval(5);
- // GIF最大截帧数量
- // input.SetMaxFrames(20);
- // 超过大小限制的图片是否压缩,0 不压缩, 1 压缩
- // input.SetLargeImageDetect(0);
- req.AddInput(input);
- }
- // 审核策略,不填为默认审核策略
- // req.SetBizType("biz_type_example");
- // 审核场景类型
- req.SetDetectType("Ads,Porn,Politics,Terrorism");
- CosResult result = cos.BatchImageAuditing(req, &resp);
- if (result.IsSucc()) {
- std::cout << "BatchImageAuditing Succ." << std::endl;
- std::cout << "RequestId: " << resp.GetRequestId() << std::endl;
- std::vector<ImageAuditingJobsDetail> jobsDetails = resp.GetJobsDetails();
- for (std::vector<ImageAuditingJobsDetail>::const_iterator it = jobsDetails.begin();
- it != jobsDetails.end(); ++it) {
- std::cout << "JobsDetails: \n" << it->to_string() << std::endl;
- }
- } else {
- std::cout << "BatchImageAuditing Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- std::cout << "===================BatchImageAuditing==================="
- "=========="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 图片审核任务结果查询
- // https://cloud.tencent.com/document/product/436/68904
- void DescribeImageAuditingJob(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& job_id) {
- DescribeImageAuditingJobReq req(bucket_name);
- DescribeImageAuditingJobResp resp;
- req.SetJobId(job_id);
- CosResult result = cos.DescribeImageAuditingJob(req, &resp);
- if (result.IsSucc()) {
- std::cout << "DescribeImageAuditingJob Succ." << std::endl;
- std::cout << "RequestId: " << resp.GetRequestId() << std::endl;
- std::cout << "JobsDetail: \n" << resp.GetJobsDetail().to_string() << std::endl;
- } else {
- std::cout << "DescribeImageAuditingJob Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- std::cout << "===================DescribeImageAuditingJob==================="
- "=========="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 提交视频审核任务
- // https://cloud.tencent.com/document/product/436/47316
- void CreateVideoAuditingJob(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name) {
- CreateVideoAuditingJobReq req(bucket_name);
- CreateVideoAuditingJobResp resp;
- // input
- // 需要审核的视频object
- req.SetObject(object_name);
- // 审核视频链接,Object和URL只用设置一个,设置URL时默认审核URL
- //req.SetUrl("https://exampleurl.com");
- // 视频标识,该字段在结果中返回原始内容
- req.SetDataId("DataId");
- // 用户业务字段
- // UserInfo user_info = UserInfo();
- // user_info.SetIp("ip_example");
- // req.SetUserInfo(user_info);
- // conf
- SnapShotConf snap_shot = SnapShotConf();
- // 截图数量
- snap_shot.SetCount(100);
- // 截图类型,Interval表示间隔模式、Average平均模式、Fps固定帧率模式,默认为Interval,
- snap_shot.SetMode("Interval");
- req.SetSnapShot(snap_shot);
- // 审核场景
- req.SetDetectType("Porn,Ads,Politics,Terrorism,Abuse,Illegal");
- // 审核策略
- // req.SetBizType("xxxxxxxxxxxxxxxxxxxxxxxxx");
- // 回调地址
- req.SetCallBack("https://callback.com");
- // 回调信息内容 默认为Simple
- req.SetCallBackVersion("Simple");
- // 是否审核视频声音,1 审核视频画面截图和声音,0 审核视频画面,默认0
- req.SetDetectContent(1);
- CosResult result = cos.CreateVideoAuditingJob(req, &resp);
- if (result.IsSucc()) {
- std::cout << "CreateVideoAuditingJob Succ." << std::endl;
- std::cout << "RequestId: " << resp.GetRequestId() << std::endl;
- std::cout << "JobsDetail: " << resp.GetJobsDetail().to_string() << std::endl;
- } else {
- std::cout << "CreateVideoAuditingJob Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- std::cout << "===================CreateVideoAuditingJob==================="
- "=========="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 查询视频审核任务结果
- // https://cloud.tencent.com/document/product/436/47317
- void DescribeVideoAuditingJob(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& job_id) {
- DescribeVideoAuditingJobReq req(bucket_name);
- DescribeVideoAuditingJobResp resp;
- req.SetJobId(job_id);
- CosResult result = cos.DescribeVideoAuditingJob(req, &resp);
- if (result.IsSucc()) {
- std::cout << "DescribeVideoAuditingJob Succ." << std::endl;
- std::cout << "RequestId: " << resp.GetRequestId() << std::endl;
- std::cout << "JobsDetail: " << resp.GetJobsDetail().to_string() << std::endl;
- } else {
- std::cout << "DescribeVideoAuditingJob Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- std::cout << "===================DescribeVideoAuditingJob==================="
- "=========="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 提交音频审核任务
- // https://cloud.tencent.com/document/product/436/54063
- void CreateAudioAuditingJob(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name) {
- CreateAudioAuditingJobReq req(bucket_name);
- CreateAudioAuditingJobResp resp;
- // input
- // 需要审核的音频object
- req.SetObject(object_name);
- // 审核音频链接,Object和URL只用设置一个,设置URL时默认审核URL
- //req.SetUrl("https://exampleurl.com");
- // 音频标识,该字段在结果中返回原始内容
- req.SetDataId("DataId");
- // 用户业务字段
- // UserInfo user_info = UserInfo();
- // user_info.SetType("type");
- // req.SetUserInfo(user_info);
- // conf
- // 审核场景
- req.SetDetectType("Porn,Ads,Politics,Terrorism,Abuse,Illegal");
- // 审核策略
- // req.SetBizType("xxxxxxxxxxxxxxxxxxxxxxxxx");
- // 回调地址
- req.SetCallBack("https://callback.com");
- // 回调信息内容 默认为Simple
- req.SetCallBackVersion("Simple");
- CosResult result = cos.CreateAudioAuditingJob(req, &resp);
- if (result.IsSucc()) {
- std::cout << "CreateAudioAuditingJob Succ." << std::endl;
- std::cout << "RequestId: " << resp.GetRequestId() << std::endl;
- std::cout << "JobsDetail: " << resp.GetJobsDetail().to_string() << std::endl;
- } else {
- std::cout << "CreateAudioAuditingJob Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- std::cout << "===================CreateAudioAuditingJob==================="
- "=========="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 查询音频审核任务结果
- // https://cloud.tencent.com/document/product/436/54064
- void DescribeAudioAuditingJob(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& job_id) {
- DescribeAudioAuditingJobReq req(bucket_name);
- DescribeAudioAuditingJobResp resp;
- req.SetJobId(job_id);
- CosResult result = cos.DescribeAudioAuditingJob(req, &resp);
- if (result.IsSucc()) {
- std::cout << "DescribeAudioAuditingJob Succ." << std::endl;
- std::cout << "RequestId: " << resp.GetRequestId() << std::endl;
- std::cout << "JobsDetails: \n" << resp.GetJobsDetail().to_string() << std::endl;
- } else {
- std::cout << "DescribeAudioAuditingJob Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- std::cout << "===================DescribeAudioAuditingJob==================="
- "=========="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 提交文本审核任务
- // https://cloud.tencent.com/document/product/436/56289
- void CreateTextAuditingJob(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name) {
- CreateTextAuditingJobReq req(bucket_name);
- CreateTextAuditingJobResp resp;
- // input
- // 需要审核的文本object
- req.SetObject(object_name);
- // 审核文本链接,Object和URL只用设置一个,设置URL时默认审核URL
- //req.SetUrl("https://exampleurl.com");
- // 音频标识,该字段在结果中返回原始内容
- req.SetDataId("DataId");
- // 需要审核的文本
- // req.SetContent("需要审核的文本");
- // 用户业务字段
- // UserInfo user_info = UserInfo();
- // user_info.SetType("type");
- // req.SetUserInfo(user_info);
- // conf
- // 审核场景
- req.SetDetectType("Porn,Ads,Politics,Terrorism,Abuse,Illegal");
- // 审核策略
- // req.SetBizType("xxxxxxxxxxxxxxxxxxxxxxxxx");
- // 回调地址
- req.SetCallBack("https://callback.com");
- // 回调信息内容 默认为Simple
- req.SetCallBackVersion("Simple");
- CosResult result = cos.CreateTextAuditingJob(req, &resp);
- if (result.IsSucc()) {
- std::cout << "CreateTextAuditingJob Succ." << std::endl;
- std::cout << "RequestId: " << resp.GetRequestId() << std::endl;
- std::cout << "JobsDetail: " << resp.GetJobsDetail().to_string() << std::endl;
- } else {
- std::cout << "CreateTextAuditingJob Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- std::cout << "===================CreateTextAuditingJob==================="
- "=========="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 查询文本审核任务结果
- // https://cloud.tencent.com/document/product/436/56288
- void DescribeTextAuditingJob(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& job_id) {
- DescribeTextAuditingJobReq req(bucket_name);
- DescribeTextAuditingJobResp resp;
- req.SetJobId(job_id);
- CosResult result = cos.DescribeTextAuditingJob(req, &resp);
- if (result.IsSucc()) {
- std::cout << "DescribeTextAuditingJob Succ." << std::endl;
- std::cout << "RequestId: " << resp.GetRequestId() << std::endl;
- std::cout << "JobsDetails: " << resp.GetJobsDetail().to_string() << std::endl;
- } else {
- std::cout << "DescribeTextAuditingJob Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- std::cout << "===================DescribeTextAuditingJob==================="
- "=========="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 提交文档审核任务
- // https://cloud.tencent.com/document/product/436/59381
- void CreateDocumentAuditingJob(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& object_name) {
- CreateDocumentAuditingJobReq req(bucket_name);
- CreateDocumentAuditingJobResp resp;
- // input
- // 需要审核的音频object
- req.SetObject(object_name);
- // 审核音频链接,Object和URL只用设置一个,设置URL时默认审核URL
- //req.SetUrl("https://exampleurl.com");
- // 文档标识,该字段在结果中返回原始内容
- req.SetDataId("DataId");
- // 指定文档后缀
- req.SetType("docx");
- // 用户业务字段
- // UserInfo user_info = UserInfo();
- // user_info.SetType("type");
- // req.SetUserInfo(user_info);
- // conf
- // 审核场景
- req.SetDetectType("Porn,Ads,Politics,Terrorism,Abuse,Illegal");
- // 审核策略
- // req.SetBizType("biz_type");
- // 回调地址
- req.SetCallBack("https://callback.com");
- CosResult result = cos.CreateDocumentAuditingJob(req, &resp);
- if (result.IsSucc()) {
- std::cout << "CreateDocumentAuditingJob Succ." << std::endl;
- std::cout << "RequestId: " << resp.GetRequestId() << std::endl;
- std::cout << "JobsDetail: " << resp.GetJobsDetail().to_string() << std::endl;
- } else {
- std::cout << "CreateDocumentAuditingJob Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- std::cout << "===================CreateTextAuditingJob==================="
- "=========="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 查询文档审核任务结果
- // https://cloud.tencent.com/document/product/436/59382
- void DescribeDocumentAuditingJob(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& job_id) {
- DescribeDocumentAuditingJobReq req(bucket_name);
- DescribeDocumentAuditingJobResp resp;
- req.SetJobId(job_id);
- CosResult result = cos.DescribeDocumentAuditingJob(req, &resp);
- if (result.IsSucc()) {
- std::cout << "DescribeDocumentAuditingJob Succ." << std::endl;
- std::cout << "RequestId: " << resp.GetRequestId() << std::endl;
- std::cout << "JobsDetails: \n" << resp.GetJobsDetail().to_string() << std::endl;
- } else {
- std::cout << "DescribeDocumentAuditingJob Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- std::cout << "===================DescribeDocumentAuditingJob==================="
- "=========="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 提交网页审核任务
- // https://cloud.tencent.com/document/product/436/63958
- void CreateWebPageAuditingJob(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& url) {
- CreateWebPageAuditingJobReq req(bucket_name);
- CreateWebPageAuditingJobResp resp;
- // input
- // 审核网页链接
- req.SetUrl(url);
- // 音频标识,该字段在结果中返回原始内容
- req.SetDataId("DataId");
- // 用户业务字段
- // UserInfo user_info = UserInfo();
- // user_info.SetType("type");
- // req.SetUserInfo(user_info);
- // conf
- // 审核场景
- req.SetDetectType("Porn,Ads,Politics,Terrorism,Abuse,Illegal");
- // 是否需要高亮展示网页内的违规文本
- req.SetReturnHighligthHtml(true);
- // 回调地址
- req.SetCallBack("https://callback.com");
- CosResult result = cos.CreateWebPageAuditingJob(req, &resp);
- if (result.IsSucc()) {
- std::cout << "CreateWebPageAuditingJob Succ." << std::endl;
- std::cout << "RequestId: " << resp.GetRequestId() << std::endl;
- std::cout << "JobsDetail: \n" << resp.GetJobsDetail().to_string() << std::endl;
- } else {
- std::cout << "CreateWebPageAuditingJob Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- std::cout << "===================CreateWebPageAuditingJob==================="
- "=========="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- // 查询网页审核任务结果
- // https://cloud.tencent.com/document/product/436/63959
- void DescribeWebPageAuditingJob(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
- const std::string& job_id) {
- DescribeWebPageAuditingJobReq req(bucket_name);
- DescribeWebPageAuditingJobResp resp;
- req.SetJobId(job_id);
- CosResult result = cos.DescribeWebPageAuditingJob(req, &resp);
- if (result.IsSucc()) {
- std::cout << "DescribeWebPageAuditingJob Succ." << std::endl;
- std::cout << "RequestId: " << resp.GetRequestId() << std::endl;
- std::cout << "JobsDetails: " << resp.GetJobsDetail().to_string() << std::endl;
- } else {
- std::cout << "DescribeWebPageAuditingJob Fail, ErrorMsg: " << result.GetErrorMsg()
- << std::endl;
- }
- std::cout << "===================DescribeWebPageAuditingJob==================="
- "=========="
- << std::endl;
- PrintResult(result, resp);
- std::cout << "========================================================"
- << std::endl;
- }
- void TestLogCallback(const std::string& log) {
- std::ofstream ofs;
- ofs.open("test.log", std::ios_base::app);
- ofs << log;
- ofs.close();
- }
- /*
- export CPP_SDK_ACCESS_KEY=xxx
- export CPP_SDK_SECRET_KEY=xxx
- export CPP_SDK_REGION=xxx
- export CPP_SDK_APPID=xxx
- export CPP_SDK_BUCKET=xxx
- */
- int main(int argc, char** argv) {
- UNUSED_PARAM(argc)
- UNUSED_PARAM(argv)
- // config.json中字段的说明,可以参考https://cloud.tencent.com/document/product/436/12301
- // qcloud_cos::CosConfig config("./config.json"); //配置文件方式初始化
- std::string appid_str = getenv("CPP_SDK_APPID");
- uint64_t appId = std::stoll(appid_str); // 替换为用户appid
- qcloud_cos::CosConfig config(appId,
- getenv("CPP_SDK_ACCESS_KEY"),
- getenv("CPP_SDK_SECRET_KEY"),
- getenv("CPP_SDK_REGION")); //替换为用户ak,sk 和 region
- config.SetLogCallback(&TestLogCallback);
- // config.SetTmpToken("TmpToken"); //替换为用户临时密钥
- qcloud_cos::CosAPI cos(config);
- std::string bucket_name = getenv("CPP_SDK_BUCKET"); //替换为用户的存储桶名,由bucketname-appid
- ///组成,appid必须填入,可以在COS控制台查看存储桶名称。
- /// https://console.cloud.tencent.com/cos5/bucket
- CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
- GetService(cos);
- PutBucket(cos, bucket_name);
- GetBucket(cos, bucket_name);
- HeadBucket(cos, bucket_name);
- PutBucketVersioning(cos, bucket_name);
- GetBucketVersioning(cos, bucket_name);
- PutBucketReplication(cos, bucket_name);
- GetBucketReplication(cos, bucket_name);
- DeleteBucketReplication(cos, bucket_name);
-
- PutBucketLifecycle(cos, bucket_name);
- GetBucketLifecycle(cos, bucket_name);
- DeleteBucketLifecycle(cos, bucket_name);
- PutBucketACL(cos, bucket_name);
- GetBucketACL(cos, bucket_name);
- PutBucketPolicy(cos, bucket_name);
- GetBucketPolicy(cos, bucket_name);
- DeleteBucketPolicy(cos, bucket_name);
- PutBucketCORS(cos, bucket_name);
- GetBucketCORS(cos, bucket_name);
- DeleteBucketCORS(cos, bucket_name);
- PutBucketDomain(cos, bucket_name);
- GetBucketDomain(cos, bucket_name);
- PutBucketLogging(cos, bucket_name, " xxxxx-appid", "prefix"); //替换为用户的日志投递存储桶名和投递路径,由bucketname-appid
- GetBucketLogging(cos, bucket_name);
- PutBucketWebsite(cos, bucket_name);
- GetBucketWebsite(cos, bucket_name);
- DeleteBucketWebsite(cos, bucket_name);
- PutBucketTagging(cos, bucket_name);
- GetBucketTagging(cos, bucket_name);
- DeleteBucketTagging(cos, bucket_name);
- PutBucketInventory(cos, bucket_name);
- GetBucketInventory(cos,bucket_name);
-
- // 简单上传(文件)
- PutObjectByFile(cos, bucket_name, "test/test1", "./temp/test1"); //替换为用户 object key 和本地文件路径
- HeadObject(cos, bucket_name, "test/test1"); //替换为用户 object key
- GetObjectByFile(cos, bucket_name, "test/test1", "./temp/test1.down"); //替换为用户 object key 和本地文件路径
- PutObjectByStream(cos, bucket_name);
- GetObjectByStream(cos, bucket_name, "test/sevenyou_10m");//替换为用户 object key
- MultiUpload(cos, bucket_name); //用户自定义复合上传
-
- MultiUploadObject(cos, bucket_name, "test/test2", "./temp/testfilebig"); // sdk 封装好的多线程上传接口,替换为用户 object key 和本地文件路径
-
- MultiGetObject(cos, bucket_name, "test/test2", "./temp/testfilebig.down"); // 替换为用户 object key 和本地文件路径
- PutObjectACL(cos, bucket_name, "test/sevenyou_10m"); // 替换为用户 object key
- GetObjectACL(cos, bucket_name, "test/sevenyou_10m"); // 替换为用户 object key
- PutObjectTagging(cos, bucket_name, "test/sevenyou_10m"); // 替换为用户 object key
- GetObjectTagging(cos, bucket_name, "test/sevenyou_10m"); // 替换为用户 object key
- DeleteObjectTagging(cos, bucket_name, "test/sevenyou_10m"); // 替换为 用户object key
- PutObjectCopy(cos, bucket_name,
- "test/sevenyou_e3_put_obj_copy_from_north_normal",
- "xxxx-appid.cos.region.myqcloud.com/objectkey"); //此处替换为用户拷贝源
- DeleteObject(cos, bucket_name,"test/sevenyou_e3_put_obj_copy_from_north_normal"); // 替换为用户 object key
- // Batch Delete
- {
- PutObjectByFile(cos, bucket_name, "test/test4", "./temp/test1");
- PutObjectByFile(cos, bucket_name, "test/test5", "./temp/test1");
- PutObjectByFile(cos, bucket_name, "test/test6", "./temp/test1");
- PutObjectByFile(cos, bucket_name, "test/test7", "./temp/test1");
- std::vector<std::string> objects;
- std::vector<ObjectVersionPair> to_be_deleted;
- objects.push_back("test/test4"); // 替换为用户 object key
- objects.push_back("test/test5"); // 替换为用户 object key
- objects.push_back("test/test6"); // 替换为用户 object key
- objects.push_back("test/test7"); // 替换为用户 object key
- for (size_t idx = 0; idx < objects.size(); ++idx) {
- ObjectVersionPair pair;
- pair.m_object_name = objects[idx];
- // if (idx == 2) {
- // pair.m_version_id = "MTg0NDY3NDI1NTg4Mjc3NzExNjI";
- // } else if (idx == 3) {
- // pair.m_version_id = "MTg0NDY3NDI1NTg4Mjc3NzA3Nzc";
- // } // 有需要可以指定删除的版本
- to_be_deleted.push_back(pair);
- }
- DeleteObjects(cos, bucket_name, to_be_deleted);
- }
-
- UploadCopy(cos, bucket_name); //用户自定义分块拷贝
-
- Copy(cos, bucket_name, "test/testCopy2",
- "xxxx-appid.cos.region.myqcloud.com/objectkey"); //此处替换为用户拷贝源
- GeneratePresignedUrl(cos, bucket_name);
- GetBucketLocation(cos, bucket_name);
- IsBucketExist(cos, bucket_name);
- IsObjectExist(cos, bucket_name);
- ResumableGetObject(cos, bucket_name, "test/test2", "./temp/testfilebig_resumable_dowm"); // 替换为用户 object key 和本地文件路径
- AsyncMultiPutObject(cos, bucket_name, "test/testfilebig_async", "./temp/testfilebig"); // 替换为用户 object key 和本地文件路径
- AsyncMultiGetObject(cos, bucket_name, "test/testfilebig_async", "./temp/testfilebig_async_down"); // 替换为用户 object key 和本地文件路径
- PutObjectsFromDirectoryToCosPath(cos, bucket_name,
- "./temp/testdir/", "test/testdir/"); // 替换为用户 object文件夹路径 和本地路径
- DownloadFromDirectory(cos, bucket_name, "test/testdir/", "./temp/testdir_down"); // 替换为用户 object文件夹路径 和本地路径
- MoveObject(cos, bucket_name, "test/test1", "test/test10"); // 替换为用户move 的两个 object key
-
- DeleteDirectory(cos, bucket_name, "test/testdir/"); // 替换为用户 object文件夹路径
- DeleteObjectsByPrefix(cos, bucket_name, "test/test"); // 替换为用户 object key 的前缀
-
- DeleteDirectory(cos, bucket_name, "test/"); // 替换为用户 object文件夹路径
-
- DeleteBucket(cos, bucket_name);
- // 图片处理
- // {
- // ImageThumbnail(cos, bucket_name, "flower.jpg", "flower_thumbnail.jpg");
- // PutImage(cos, bucket_name, "test_ci/flower.jpg", "./flower.jpg");
- // AddWatermark(cos, bucket_name, "test_ci/flower.jpg", "./flower.jpg");
- // GetImageWithWatermark(cos, bucket_name, "test_ci/flower.jpg",
- // "./flower_download_with_matermark.jpg"); ExtractWatermark(cos,
- // bucket_name, "test_ci/flower_download_with_matermark.jpg",
- // "./flower_download_with_matermark.jpg"); CloudImageProcess(cos,
- // bucket_name, "test_ci/flower.jpg"); PutQRcode(cos, bucket_name,
- // "test_ci/qrcode.png", "./qrcode.png"); GetQRcode(cos, bucket_name,
- // "test_ci/qrcode.png");
- // }
- // 文档接口
- //{
- // DescribeDocProcessBuckets(cos);
- // DocPreview(cos, bucket_name, "test.docx", "test_preview.jpg");
- // CreateDocProcessJobs(cos, bucket_name);
- // DescribeDocProcessJob(cos, bucket_name);
- // DescribeDocProcessJobs(cos, bucket_name);
- // DescribeDocProcessQueues(cos, bucket_name);
- // UpdateDocProcessQueue(cos, bucket_name);
- //}
- // GetObjectUrl(cos, bucket_name, "test_object");
- // 媒体接口
- //{
- // DescribeMediaBuckets(cos);
- // GetSnapshot(cos, bucket_name, "1920_1080.mp4", "snapshot.jpg");
- // GetMediaInfo(cos, bucket_name, "1920_1080.mp4");
- // GetPm3u8(cos, bucket_name, "video.m3u8");
- // DescribeMediaQueues(cos, bucket_name);
- // UpdateMediaQueue(cos, bucket_name);
- // CreateSnapshotJobs(cos, bucket_name);
- // CreateAnimationJobs(cos, bucket_name);
- // CreateConcatJobs(cos, bucket_name);
- // CreateTranscodeJobs(cos, bucket_name);
- // CreateDigitalWatermarkJobs(cos, bucket_name);
- // CreateExtractDigitalWatermarkJobs(cos, bucket_name);
- // CreateSegmentJobs(cos, bucket_name);
- // CreateVoiceSeparateJobs(cos, bucket_name);
- // CreateVideoMontageJobs(cos, bucket_name);
- // CreateSmartCoverJobs(cos, bucket_name);
- // DescribeDataProcessJobs(cos, bucket_name);
- // CancelDataProcessJob(cos, bucket_name);
- //}
- // 文件处理接口
- {
- // CreateFileUncompressJobs(cos, bucket_name);
- // DescribeFileUncompressJobs(cos, bucket_name);
- }
- // 图片审核
- //{
- // GetImageAuditing(cos, bucket_name, "data/audit/gif/test.gif");
- // std::vector<std::string> objects = {"data/audit/ads/clipboard.png", "data/audit/git/test.jpg"};
- // BatchImageAuditing(cos, bucket_name, objects);
- // DescribeImageAuditingJob(cos, bucket_name, "sife0bdb79d72211ecb174525400bc4ce1");
- //}
- // 视频审核
- //{
- // CreateVideoAuditingJob(cos, bucket_name, "data/audit/video/test.mp4");
- // DescribeVideoAuditingJob(cos, bucket_name, "avda91c516d81a11ec933c525400863904");
- //}
- // 音频审核
- //{
- // CreateAudioAuditingJob(cos, bucket_name, "data/audit/audio/test.mp3");
- // DescribeAudioAuditingJob(cos, bucket_name, "sa3be08072d81f11ecb378525400bc4ce1");
- //}
- // 文本审核
- //{
- // CreateTextAuditingJob(cos, bucket_name, "data/audit/text/test.txt");
- // DescribeTextAuditingJob(cos, bucket_name, "stf8d06a90d82211ecb378525400bc4ce1");
- //}
- // 文档审核
- //{
- // CreateDocumentAuditingJob(cos, bucket_name, "data/audit/documents/test.docx");
- // DescribeDocumentAuditingJob(cos, bucket_name, "sd5609cbe0db1211ec9bdf525400863904");
- //}
- // 网页审核
- //{
- // CreateWebPageAuditingJob(cos, bucket_name, "https://www.test.com/");
- // DescribeWebPageAuditingJob(cos, bucket_name, "shea80c920db2a11ec954f525400bc4ce1");
- //}
- #if defined(_WIN32)
- // MultiUploadObjectWideChar(cos, bucket_name);
- system("pause");
- #endif
- return 0;
- }
|