cos_demo.cpp 160 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185
  1. // Copyright (c) 2017, Tencent Inc.
  2. // All rights reserved.
  3. //
  4. // Author: sevenyou <sevenyou@tencent.com>
  5. // Created: 07/18/17
  6. // Description:
  7. #include <stdlib.h>
  8. #include <sys/stat.h>
  9. #include <iostream>
  10. #include <map>
  11. #include <string>
  12. #include <thread>
  13. #include <vector>
  14. #include "cos_api.h"
  15. #include "cos_sys_config.h"
  16. #include "util/auth_tool.h"
  17. #include "Poco/TaskManager.h"
  18. using namespace qcloud_cos;
  19. void PrintResult(const qcloud_cos::CosResult& result,
  20. const qcloud_cos::BaseResp& resp) {
  21. if (result.IsSucc()) {
  22. std::cout << resp.DebugString() << std::endl;
  23. } else {
  24. std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
  25. std::cout << "HttpStatus=" << result.GetHttpStatus() << std::endl;
  26. std::cout << "ErrorCode=" << result.GetErrorCode() << std::endl;
  27. std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
  28. std::cout << "ResourceAddr=" << result.GetResourceAddr() << std::endl;
  29. std::cout << "XCosRequestId=" << result.GetXCosRequestId() << std::endl;
  30. std::cout << "XCosTraceId=" << result.GetXCosTraceId() << std::endl;
  31. }
  32. }
  33. void GetService(qcloud_cos::CosAPI& cos) {
  34. qcloud_cos::GetServiceReq req;
  35. qcloud_cos::GetServiceResp resp;
  36. req.AddParam("max-keys", "5");
  37. qcloud_cos::CosResult result = cos.GetService(req, &resp);
  38. std::cout << "===================GetService====================="
  39. << std::endl;
  40. PrintResult(result, resp);
  41. const qcloud_cos::Owner& owner = resp.GetOwner();
  42. const std::vector<qcloud_cos::Bucket>& buckets = resp.GetBuckets();
  43. std::cout << "owner.m_id=" << owner.m_id
  44. << ", owner.display_name=" << owner.m_display_name << std::endl;
  45. for (std::vector<qcloud_cos::Bucket>::const_iterator itr = buckets.begin();
  46. itr != buckets.end(); ++itr) {
  47. const qcloud_cos::Bucket& bucket = *itr;
  48. std::cout << "Bucket name=" << bucket.m_name
  49. << ", location=" << bucket.m_location
  50. << ", create_date=" << bucket.m_create_date << std::endl;
  51. }
  52. std::cout << "========================================================="
  53. << std::endl;
  54. }
  55. void HeadBucket(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
  56. qcloud_cos::HeadBucketReq req(bucket_name);
  57. qcloud_cos::HeadBucketResp resp;
  58. qcloud_cos::CosResult result = cos.HeadBucket(req, &resp);
  59. std::cout << "===================HeadBucketResponse====================="
  60. << std::endl;
  61. PrintResult(result, resp);
  62. std::cout << "=========================================================="
  63. << std::endl;
  64. }
  65. void DeleteBucket(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
  66. qcloud_cos::DeleteBucketReq req(bucket_name);
  67. qcloud_cos::DeleteBucketResp resp;
  68. qcloud_cos::CosResult result = cos.DeleteBucket(req, &resp);
  69. std::cout << "===================DeleteBucketResponse====================="
  70. << std::endl;
  71. PrintResult(result, resp);
  72. std::cout << "========================================================="
  73. << std::endl;
  74. }
  75. void PutBucket(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
  76. qcloud_cos::PutBucketReq req(bucket_name);
  77. //创建MAZ存储桶使用下方进行设置
  78. //req.SetMAZBucket();
  79. qcloud_cos::PutBucketResp resp;
  80. qcloud_cos::CosResult result = cos.PutBucket(req, &resp);
  81. std::cout << "===================PutBucketResponse====================="
  82. << std::endl;
  83. PrintResult(result, resp);
  84. std::cout << "========================================================="
  85. << std::endl;
  86. }
  87. void GetBucket(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
  88. qcloud_cos::GetBucketReq req(bucket_name);
  89. qcloud_cos::GetBucketResp resp;
  90. qcloud_cos::CosResult result = cos.GetBucket(req, &resp);
  91. std::cout << "===================GetBucketResponse====================="
  92. << std::endl;
  93. PrintResult(result, resp);
  94. std::cout << "========================================================="
  95. << std::endl;
  96. }
  97. void PutBucketVersioning(qcloud_cos::CosAPI& cos,
  98. const std::string& bucket_name) {
  99. qcloud_cos::PutBucketVersioningReq req(bucket_name);
  100. req.SetStatus(true);
  101. qcloud_cos::PutBucketVersioningResp resp;
  102. qcloud_cos::CosResult result = cos.PutBucketVersioning(req, &resp);
  103. std::cout
  104. << "===================PutBucketVersioningResponse====================="
  105. << std::endl;
  106. PrintResult(result, resp);
  107. std::cout
  108. << "===================================================================="
  109. << std::endl;
  110. }
  111. void GetBucketVersioning(qcloud_cos::CosAPI& cos,
  112. const std::string& bucket_name) {
  113. qcloud_cos::GetBucketVersioningReq req(bucket_name);
  114. qcloud_cos::GetBucketVersioningResp resp;
  115. qcloud_cos::CosResult result = cos.GetBucketVersioning(req, &resp);
  116. std::cout << "Bucket status= " << resp.GetStatus() << std::endl;
  117. std::cout
  118. << "===================GetBucketVersioningResponse====================="
  119. << std::endl;
  120. PrintResult(result, resp);
  121. std::cout
  122. << "===================================================================="
  123. << std::endl;
  124. }
  125. void PutBucketReplication(qcloud_cos::CosAPI& cos,
  126. const std::string& bucket_name) {
  127. qcloud_cos::PutBucketReplicationReq req(bucket_name);
  128. req.SetRole("qcs::cam::uin/12345:uin/12345"); // 替换为用户 Role
  129. qcloud_cos::ReplicationRule rule(
  130. "", "qcs:id/0:cos:ap-xxx:appid/xxxxx:bucketname", "",
  131. "", true);// 替换为用户 rule
  132. req.AddReplicationRule(rule);
  133. qcloud_cos::PutBucketReplicationResp resp;
  134. qcloud_cos::CosResult result = cos.PutBucketReplication(req, &resp);
  135. std::cout
  136. << "===================PutBucketReplicationResponse====================="
  137. << std::endl;
  138. PrintResult(result, resp);
  139. std::cout
  140. << "===================================================================="
  141. << std::endl;
  142. }
  143. void GetBucketReplication(qcloud_cos::CosAPI& cos,
  144. const std::string& bucket_name) {
  145. qcloud_cos::GetBucketReplicationReq req(bucket_name);
  146. qcloud_cos::GetBucketReplicationResp resp;
  147. qcloud_cos::CosResult result = cos.GetBucketReplication(req, &resp);
  148. std::cout
  149. << "===================GetBucketReplicationResponse====================="
  150. << std::endl;
  151. PrintResult(result, resp);
  152. std::cout
  153. << "===================================================================="
  154. << std::endl;
  155. }
  156. void DeleteBucketReplication(qcloud_cos::CosAPI& cos,
  157. const std::string& bucket_name) {
  158. qcloud_cos::DeleteBucketReplicationReq req(bucket_name);
  159. qcloud_cos::DeleteBucketReplicationResp resp;
  160. qcloud_cos::CosResult result = cos.DeleteBucketReplication(req, &resp);
  161. std::cout << "===================DeleteBucketReplicationResponse============="
  162. "========"
  163. << std::endl;
  164. PrintResult(result, resp);
  165. std::cout
  166. << "===================================================================="
  167. << std::endl;
  168. }
  169. void PutBucketLifecycle(qcloud_cos::CosAPI& cos,
  170. const std::string& bucket_name) {
  171. qcloud_cos::PutBucketLifecycleReq req(bucket_name);
  172. {
  173. qcloud_cos::LifecycleRule rule;
  174. rule.SetIsEnable(true);
  175. rule.SetId("lifecycle_rule00");
  176. qcloud_cos::LifecycleFilter filter;
  177. filter.SetPrefix("sevenyou_e1");
  178. rule.SetFilter(filter);
  179. qcloud_cos::LifecycleTransition transition;
  180. transition.SetDays(30);
  181. transition.SetStorageClass("Standard_IA");
  182. rule.AddTransition(transition);
  183. req.AddRule(rule);
  184. }
  185. qcloud_cos::PutBucketLifecycleResp resp;
  186. qcloud_cos::CosResult result = cos.PutBucketLifecycle(req, &resp);
  187. std::cout
  188. << "===================PutBucketLifecycleResponse====================="
  189. << std::endl;
  190. PrintResult(result, resp);
  191. std::cout
  192. << "===================================================================="
  193. << std::endl;
  194. }
  195. void GetBucketLifecycle(qcloud_cos::CosAPI& cos,
  196. const std::string& bucket_name) {
  197. qcloud_cos::GetBucketLifecycleReq req(bucket_name);
  198. qcloud_cos::GetBucketLifecycleResp resp;
  199. qcloud_cos::CosResult result = cos.GetBucketLifecycle(req, &resp);
  200. const std::vector<qcloud_cos::LifecycleRule>& rules = resp.GetRules();
  201. for (size_t idx = 0; idx != rules.size(); ++idx) {
  202. std::cout << "id = " << rules[idx].GetId() << std::endl;
  203. }
  204. std::cout
  205. << "===================GetBucketLifecycleResponse====================="
  206. << std::endl;
  207. PrintResult(result, resp);
  208. std::cout
  209. << "===================================================================="
  210. << std::endl;
  211. }
  212. void DeleteBucketLifecycle(qcloud_cos::CosAPI& cos,
  213. const std::string& bucket_name) {
  214. qcloud_cos::DeleteBucketLifecycleReq req(bucket_name);
  215. qcloud_cos::DeleteBucketLifecycleResp resp;
  216. qcloud_cos::CosResult result = cos.DeleteBucketLifecycle(req, &resp);
  217. std::cout
  218. << "===================DeleteBucketLifecycleResponse====================="
  219. << std::endl;
  220. PrintResult(result, resp);
  221. std::cout
  222. << "===================================================================="
  223. << std::endl;
  224. }
  225. void PutBucketACL(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
  226. // 1. 设置ACL配置(通过Body,
  227. // 设置ACL可以通过Body、Header两种方式,但只能二选一,否则会有冲突)
  228. {
  229. qcloud_cos::PutBucketACLReq req(bucket_name);
  230. qcloud_cos::Owner owner = {"qcs::cam::uin/xxx:uin/xxx",
  231. "qcs::cam::uin/xxx:uin/xxx"};
  232. qcloud_cos::Grant grant;
  233. req.SetOwner(owner);
  234. grant.m_grantee.m_type = "Group";
  235. grant.m_grantee.m_uri = "http://cam.qcloud.com/groups/global/AllUsers";
  236. grant.m_perm = "READ";
  237. req.AddAccessControlList(grant);
  238. qcloud_cos::PutBucketACLResp resp;
  239. qcloud_cos::CosResult result = cos.PutBucketACL(req, &resp);
  240. std::cout << "===================PutBucketACLResponse====================="
  241. << std::endl;
  242. PrintResult(result, resp);
  243. std::cout << "============================================================="
  244. "======="
  245. << std::endl;
  246. }
  247. #if 0
  248. // 2. 设置ACL配置(通过Header, 设置ACL可以通过Body、Header两种方式,但只能二选一,否则会有冲突)
  249. {
  250. qcloud_cos::PutBucketACLReq req(bucket_name);
  251. req.SetXCosAcl("public-read");
  252. qcloud_cos::PutBucketACLResp resp;
  253. qcloud_cos::CosResult result = cos.PutBucketACL(req, &resp);
  254. std::cout << "===================PutBucketACLResponse=====================" << std::endl;
  255. PrintResult(result, resp);
  256. std::cout << "====================================================================" << std::endl;
  257. }
  258. #endif
  259. }
  260. void GetBucketACL(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
  261. qcloud_cos::GetBucketACLReq req(bucket_name);
  262. qcloud_cos::GetBucketACLResp resp;
  263. qcloud_cos::CosResult result = cos.GetBucketACL(req, &resp);
  264. std::vector<qcloud_cos::Grant> grants = resp.GetAccessControlList();
  265. for (size_t idx = 0; idx < grants.size(); ++idx) {
  266. std::cout << "Type is " << grants[idx].m_grantee.m_type << std::endl;
  267. }
  268. std::cout << "===================GetBucketACLResponse====================="
  269. << std::endl;
  270. PrintResult(result, resp);
  271. std::cout
  272. << "===================================================================="
  273. << std::endl;
  274. }
  275. void GetBucketPolicy(qcloud_cos::CosAPI& cos,const std::string& bucket_name) {
  276. qcloud_cos::GetBucketPolicyReq req(bucket_name);
  277. qcloud_cos::GetBucketPolicyResp resp;
  278. qcloud_cos::CosResult result = cos.GetBucketPolicy(req, &resp);
  279. std::cout << resp.GetPolicy() << std::endl;
  280. std::cout << "===================GetBucketPolicy====================="
  281. << std::endl;
  282. PrintResult(result, resp);
  283. std::cout
  284. << "===================================================================="
  285. << std::endl;
  286. }
  287. void PutBucketPolicy(qcloud_cos::CosAPI& cos,const std::string& bucket_name) {
  288. qcloud_cos::PutBucketPolicyReq req(bucket_name);
  289. qcloud_cos::PutBucketPolicyResp resp;
  290. std::string bucket_policy =
  291. " {"
  292. " \"Statement\": ["
  293. " {"
  294. " \"Principal\": {"
  295. " \"qcs\": ["
  296. " \"qcs::cam::uin/xxx:uin/xxxx\"" //替换成您想授予权限的账户 uin / 子用户 uin
  297. " ]\n"
  298. " },\n"
  299. " \"Effect\": \"allow\","
  300. " \"Action\": ["
  301. " \"cos:PutObject\""
  302. " ],\n"
  303. " \"Resource\": [" //这里改成允许的路径前缀,可以根据自己网站的用户登录态判断允许上传的具体路径,例子: a.jpg 或者 a/* 或者 * (使用通配符*存在重大安全风险, 请谨慎评估使用)
  304. " \"qcs::cos:ap-guangzhou:uid/appid:backetname-appid/exampleobject\""
  305. " ],\n"
  306. " \"Condition\": {"
  307. " \"string_equal\": {"
  308. " \"cos:x-cos-mime-limit\": \"image/jpeg\""
  309. " }"
  310. " }"
  311. " }"
  312. " ],"
  313. " \"Version\": \"2.0\""
  314. " }";
  315. std::cout << bucket_policy << std::endl;
  316. req.SetBody(bucket_policy);
  317. qcloud_cos::CosResult result = cos.PutBucketPolicy(req, &resp);
  318. std::cout << "===================PutBucketPolicy====================="
  319. << std::endl;
  320. PrintResult(result, resp);
  321. std::cout
  322. << "===================================================================="
  323. << std::endl;
  324. }
  325. void DeleteBucketPolicy(qcloud_cos::CosAPI& cos,const std::string& bucket_name) {
  326. qcloud_cos::DeleteBucketPolicyReq req(bucket_name);
  327. qcloud_cos::DeleteBucketPolicyResp resp;
  328. qcloud_cos::CosResult result = cos.DeleteBucketPolicy(req, &resp);
  329. std::cout << "===================DeleteBucketPolicy====================="
  330. << std::endl;
  331. PrintResult(result, resp);
  332. std::cout
  333. << "===================================================================="
  334. << std::endl;
  335. }
  336. void PutBucketCORS(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
  337. qcloud_cos::PutBucketCORSReq req(bucket_name);
  338. qcloud_cos::CORSRule rule;
  339. rule.m_id = "123";
  340. rule.m_allowed_headers.push_back("x-cos-meta-test");
  341. rule.m_allowed_origins.push_back("http://www.qq.com");
  342. rule.m_allowed_origins.push_back("http://www.qcloud.com");
  343. rule.m_allowed_methods.push_back("PUT");
  344. rule.m_allowed_methods.push_back("GET");
  345. rule.m_max_age_secs = "600";
  346. rule.m_expose_headers.push_back("x-cos-expose");
  347. req.AddRule(rule);
  348. qcloud_cos::PutBucketCORSResp resp;
  349. qcloud_cos::CosResult result = cos.PutBucketCORS(req, &resp);
  350. std::cout << "===================PutBucketCORSResponse====================="
  351. << std::endl;
  352. PrintResult(result, resp);
  353. std::cout
  354. << "===================================================================="
  355. << std::endl;
  356. }
  357. void GetBucketCORS(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
  358. qcloud_cos::GetBucketCORSReq req(bucket_name);
  359. qcloud_cos::GetBucketCORSResp resp;
  360. qcloud_cos::CosResult result = cos.GetBucketCORS(req, &resp);
  361. std::cout << "===================GetBucketCORSResponse====================="
  362. << std::endl;
  363. PrintResult(result, resp);
  364. std::cout
  365. << "===================================================================="
  366. << std::endl;
  367. }
  368. void DeleteBucketCORS(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
  369. qcloud_cos::DeleteBucketCORSReq req(bucket_name);
  370. qcloud_cos::DeleteBucketCORSResp resp;
  371. qcloud_cos::CosResult result = cos.DeleteBucketCORS(req, &resp);
  372. std::cout
  373. << "===================DeleteBucketCORSResponse====================="
  374. << std::endl;
  375. PrintResult(result, resp);
  376. std::cout
  377. << "===================================================================="
  378. << std::endl;
  379. }
  380. void PutObjectByFile(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  381. const std::string& object_name,
  382. const std::string& file_path) {
  383. qcloud_cos::PutObjectByFileReq req(bucket_name, object_name, file_path);
  384. // req.AddParam("versionId", "MTg0NDY3NDI1NjQ4NjUyMjQ1MTA");
  385. qcloud_cos::PutObjectByFileResp resp;
  386. qcloud_cos::CosResult result = cos.PutObject(req, &resp);
  387. std::cout << "===================PutObjectResponse====================="
  388. << std::endl;
  389. PrintResult(result, resp);
  390. std::cout << "========================================================="
  391. << std::endl;
  392. }
  393. // 限速上传对象
  394. void PutObjectByFileLimitTraffic(qcloud_cos::CosAPI& cos,
  395. const std::string& bucket_name,
  396. const std::string& object_name,
  397. const std::string& file_path,
  398. const uint64_t traffic_limit) {
  399. qcloud_cos::PutObjectByFileReq req(bucket_name, object_name, file_path);
  400. req.SetTrafficLimit(traffic_limit);
  401. qcloud_cos::PutObjectByFileResp resp;
  402. qcloud_cos::CosResult result = cos.PutObject(req, &resp);
  403. std::cout << "===================PutObjectFileLimitTrafficResponse==========="
  404. "=========="
  405. << std::endl;
  406. PrintResult(result, resp);
  407. std::cout << "========================================================="
  408. << std::endl;
  409. }
  410. void PutObjectByStream(qcloud_cos::CosAPI& cos,
  411. const std::string& bucket_name) {
  412. std::istringstream iss("put object");
  413. qcloud_cos::PutObjectByStreamReq req(bucket_name, "test/sevenyou_10m", iss);
  414. qcloud_cos::PutObjectByStreamResp resp;
  415. qcloud_cos::CosResult result = cos.PutObject(req, &resp);
  416. std::cout << "===================PutObjectResponse====================="
  417. << std::endl;
  418. PrintResult(result, resp);
  419. std::cout << "========================================================="
  420. << std::endl;
  421. }
  422. void HeadObject(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  423. const std::string& object_name) {
  424. qcloud_cos::HeadObjectReq req(bucket_name, object_name);
  425. // req.AddParam("versionId", "MTg0NDY3NDI1NjQ4NjUyMjQ1MTA");
  426. qcloud_cos::HeadObjectResp resp;
  427. qcloud_cos::CosResult result = cos.HeadObject(req, &resp);
  428. std::cout << "===================HeadObjectResponse====================="
  429. << std::endl;
  430. PrintResult(result, resp);
  431. std::cout << "=========================================================="
  432. << std::endl;
  433. }
  434. // 下载对象到指定文件
  435. void GetObjectByFile(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  436. const std::string& object_name,
  437. const std::string& file_path) {
  438. qcloud_cos::GetObjectByFileReq req(bucket_name, object_name, file_path);
  439. // req.AddParam("versionId", "MTg0NDY3NDI1NjQwODU3NzU2MTA");
  440. qcloud_cos::GetObjectByFileResp resp;
  441. qcloud_cos::CosResult result = cos.GetObject(req, &resp);
  442. std::cout << "===================GetObjectResponse====================="
  443. << std::endl;
  444. PrintResult(result, resp);
  445. std::cout << "========================================================="
  446. << std::endl;
  447. }
  448. // 限速下载对象
  449. void GetObjectByFileLimitTraffic(qcloud_cos::CosAPI& cos,
  450. const std::string& bucket_name,
  451. const std::string& object_name,
  452. const std::string& file_path,
  453. const uint64_t traffic_limit) {
  454. qcloud_cos::GetObjectByFileReq req(bucket_name, object_name, file_path);
  455. req.SetTrafficLimit(traffic_limit);
  456. qcloud_cos::GetObjectByFileResp resp;
  457. qcloud_cos::CosResult result = cos.GetObject(req, &resp);
  458. std::cout << "===================GetObjectByFileLimitTrafficResponse========="
  459. "============"
  460. << std::endl;
  461. PrintResult(result, resp);
  462. std::cout << "========================================================="
  463. << std::endl;
  464. }
  465. void GetObjectByStream(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  466. const std::string& object_name) {
  467. std::ostringstream os;
  468. qcloud_cos::GetObjectByStreamReq req(bucket_name, object_name, os);
  469. qcloud_cos::GetObjectByStreamResp resp;
  470. qcloud_cos::CosResult result = cos.GetObject(req, &resp);
  471. std::cout << "===================GetObjectResponse====================="
  472. << std::endl;
  473. PrintResult(result, resp);
  474. std::cout << "========================================================="
  475. << std::endl;
  476. std::cout << os.str() << std::endl;
  477. }
  478. // 多线程下载对象
  479. void MultiGetObject(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  480. const std::string& object_name,
  481. const std::string& file_path) {
  482. qcloud_cos::MultiGetObjectReq req(bucket_name, object_name, file_path);
  483. qcloud_cos::MultiGetObjectResp resp;
  484. qcloud_cos::CosResult result = cos.MultiGetObject(req, &resp);
  485. std::cout << "===================GetObjectResponse====================="
  486. << std::endl;
  487. PrintResult(result, resp);
  488. std::cout << "========================================================="
  489. << std::endl;
  490. }
  491. // 多线程限速下载对象
  492. void MultiGetObjectLimitTraffic(qcloud_cos::CosAPI& cos,
  493. const std::string& bucket_name,
  494. const std::string& object_name,
  495. const std::string& file_path,
  496. const uint64_t traffic_limit) {
  497. qcloud_cos::MultiGetObjectReq req(bucket_name, object_name, file_path);
  498. req.SetTrafficLimit(traffic_limit);
  499. qcloud_cos::MultiGetObjectResp resp;
  500. qcloud_cos::CosResult result = cos.MultiGetObject(req, &resp);
  501. std::cout << "===================MultiGetObjectLimitTrafficResponse=========="
  502. "==========="
  503. << std::endl;
  504. PrintResult(result, resp);
  505. std::cout << "========================================================="
  506. << std::endl;
  507. }
  508. // 初始化分块上传
  509. void InitMultiUpload(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  510. const std::string& object_name, std::string* upload_id) {
  511. qcloud_cos::InitMultiUploadReq req(bucket_name, object_name);
  512. qcloud_cos::InitMultiUploadResp resp;
  513. qcloud_cos::CosResult result = cos.InitMultiUpload(req, &resp);
  514. std::cout << "=====================InitMultiUpload====================="<< std::endl;
  515. PrintResult(result, resp);
  516. std::cout << "========================================================="<< std::endl;
  517. *upload_id = resp.GetUploadId();
  518. }
  519. // 上传分块
  520. void UploadPartData(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  521. const std::string& object_name,
  522. const std::string& upload_id, std::fstream& is,
  523. uint64_t number, std::string* etag) {
  524. qcloud_cos::UploadPartDataReq req(bucket_name, object_name, upload_id, is);
  525. req.SetPartNumber(number);
  526. qcloud_cos::UploadPartDataResp resp;
  527. qcloud_cos::CosResult result = cos.UploadPartData(req, &resp);
  528. *etag = resp.GetEtag();
  529. std::cout << "======================UploadPartData====================="<< std::endl;
  530. PrintResult(result, resp);
  531. std::cout << "========================================================="<< std::endl;
  532. }
  533. // 限速上传分块
  534. void UploadPartDataLimitTraffic(qcloud_cos::CosAPI& cos,
  535. const std::string& bucket_name,
  536. const std::string& object_name,
  537. const std::string& upload_id, std::fstream& is,
  538. uint64_t number, std::string* etag,
  539. const uint64_t traffic_limit) {
  540. qcloud_cos::UploadPartDataReq req(bucket_name, object_name, upload_id, is);
  541. req.SetPartNumber(number);
  542. req.SetTrafficLimit(traffic_limit);
  543. qcloud_cos::UploadPartDataResp resp;
  544. qcloud_cos::CosResult result = cos.UploadPartData(req, &resp);
  545. *etag = resp.GetEtag();
  546. std::cout << "======================UploadPartDataLimitTrafficResponse======="<< std::endl;
  547. PrintResult(result, resp);
  548. std::cout << "========================================================="<< std::endl;
  549. }
  550. void AbortMultiUpload(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  551. const std::string& object_name,
  552. const std::string& upload_id) {
  553. qcloud_cos::AbortMultiUploadReq req(bucket_name, object_name, upload_id);
  554. qcloud_cos::AbortMultiUploadResp resp;
  555. qcloud_cos::CosResult result = cos.AbortMultiUpload(req, &resp);
  556. std::cout << "======================AbortUploadPart=====================";
  557. PrintResult(result, resp);
  558. std::cout << "=========================================================";
  559. }
  560. void CompleteMultiUpload(qcloud_cos::CosAPI& cos,
  561. const std::string& bucket_name,
  562. const std::string& object_name,
  563. const std::string& upload_id,
  564. const std::vector<std::string>& etags,
  565. const std::vector<uint64_t>& numbers) {
  566. qcloud_cos::CompleteMultiUploadReq req(bucket_name, object_name, upload_id);
  567. qcloud_cos::CompleteMultiUploadResp resp;
  568. req.AddParam("versionId", "MTg0NDY3NDI1NjQ4NjUyMjQ1MTA");
  569. req.SetEtags(etags);
  570. req.SetPartNumbers(numbers);
  571. qcloud_cos::CosResult result = cos.CompleteMultiUpload(req, &resp);
  572. std::cout << "===================Complete============================="
  573. << std::endl;
  574. PrintResult(result, resp);
  575. std::cout << "========================================================"
  576. << std::endl;
  577. }
  578. // 多线程上传对象
  579. void MultiUploadObject(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  580. const std::string& object_name,
  581. const std::string& local_file) {
  582. qcloud_cos::MultiPutObjectReq req(bucket_name, object_name, local_file);
  583. req.SetRecvTimeoutInms(1000 * 60);
  584. qcloud_cos::MultiPutObjectResp resp;
  585. qcloud_cos::CosResult result = cos.MultiPutObject(req, &resp);
  586. if (result.IsSucc()) {
  587. std::cout << "MultiUpload Succ." << std::endl;
  588. std::cout << resp.GetLocation() << std::endl;
  589. std::cout << resp.GetKey() << std::endl;
  590. std::cout << resp.GetBucket() << std::endl;
  591. std::cout << resp.GetEtag() << std::endl;
  592. } else {
  593. std::cout << "MultiUpload Fail." << std::endl;
  594. // 获取具体失败在哪一步
  595. std::string resp_tag = resp.GetRespTag();
  596. if ("Init" == resp_tag) {
  597. // print result
  598. } else if ("Upload" == resp_tag) {
  599. // print result
  600. } else if ("Complete" == resp_tag) {
  601. // print result
  602. }
  603. }
  604. std::cout
  605. << "===================MultiUploadObject============================="
  606. << std::endl;
  607. PrintResult(result, resp);
  608. std::cout << "========================================================"
  609. << std::endl;
  610. }
  611. #ifdef _WIN32
  612. // 上传宽字符文件
  613. void MultiUploadObjectWideChar(qcloud_cos::CosAPI& cos,
  614. const std::string& bucket_name) {
  615. std::string local_file = "./안녕하십니까.mp4";
  616. std::string object_name = "안녕하십니까.mp4";
  617. qcloud_cos::MultiPutObjectReq req(bucket_name, object_name, local_file);
  618. req.SetWideCharPath(); // 需要调用该函数
  619. req.SetRecvTimeoutInms(1000 * 60);
  620. qcloud_cos::MultiPutObjectResp resp;
  621. qcloud_cos::CosResult result = cos.MultiPutObject(req, &resp);
  622. if (result.IsSucc()) {
  623. std::cout << "MultiUploadObjectWideChar Succ." << std::endl;
  624. std::cout << resp.GetLocation() << std::endl;
  625. std::cout << resp.GetKey() << std::endl;
  626. std::cout << resp.GetBucket() << std::endl;
  627. std::cout << resp.GetEtag() << std::endl;
  628. } else {
  629. std::cout << "MultiUploadObjectWideChar Fail." << std::endl;
  630. }
  631. std::cout << "===================MultiUploadObjectWideChar==================="
  632. "=========="
  633. << std::endl;
  634. PrintResult(result, resp);
  635. std::cout << "========================================================"
  636. << std::endl;
  637. }
  638. #endif
  639. //限速多线程上传
  640. void MultiUploadObjectLimitTraffic(qcloud_cos::CosAPI& cos,
  641. const std::string& bucket_name,
  642. const std::string& object_name,
  643. const std::string& local_file,
  644. const uint64_t traffic_limit) {
  645. qcloud_cos::MultiPutObjectReq req(bucket_name, object_name, local_file);
  646. req.SetTrafficLimit(traffic_limit);
  647. req.SetRecvTimeoutInms(1000 * 60);
  648. qcloud_cos::MultiPutObjectResp resp;
  649. qcloud_cos::CosResult result = cos.MultiPutObject(req, &resp);
  650. if (result.IsSucc()) {
  651. std::cout << "MultiUpload Succ." << std::endl;
  652. std::cout << resp.GetLocation() << std::endl;
  653. std::cout << resp.GetKey() << std::endl;
  654. std::cout << resp.GetBucket() << std::endl;
  655. std::cout << resp.GetEtag() << std::endl;
  656. } else {
  657. std::string resp_tag = resp.GetRespTag();
  658. std::cout << "MultiUpload Fail, respond tag " << resp_tag << std::endl;
  659. }
  660. std::cout << "===================MultiUpload============================="
  661. << std::endl;
  662. PrintResult(result, resp);
  663. std::cout << "========================================================"
  664. << std::endl;
  665. }
  666. void ListParts(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  667. const std::string& object_name, const std::string& upload_id) {
  668. qcloud_cos::ListPartsReq req(bucket_name, object_name, upload_id);
  669. req.SetMaxParts(1);
  670. req.SetPartNumberMarker("1");
  671. qcloud_cos::ListPartsResp resp;
  672. qcloud_cos::CosResult result = cos.ListParts(req, &resp);
  673. std::cout << "===================ListParts=====================" << std::endl;
  674. PrintResult(result, resp);
  675. std::cout
  676. << "===================================================================="
  677. << std::endl;
  678. }
  679. //用户自行调用初始化、上传分块、完成分块接口进行分块上传
  680. void MultiUpload(qcloud_cos::CosAPI& cos,
  681. const std::string& bucket_name) {
  682. std::string upload_id;
  683. std::string object_name = "test/sevenyou_e2_1102_north_multi";
  684. std::vector<uint64_t> numbers;
  685. std::vector<std::string> etags;
  686. std::string etag1 = "", etag2 = "";
  687. InitMultiUpload(cos, bucket_name, object_name, &upload_id);
  688. std::fstream is1("./temp/testfilebig");
  689. UploadPartData(cos, bucket_name, object_name, upload_id, is1, 1, &etag1);
  690. numbers.push_back(1);
  691. etags.push_back(etag1);
  692. is1.close();
  693. ListParts(cos, bucket_name, object_name, upload_id);
  694. // AbortMultiUpload(cos, bucket_name, object_name, upload_id);
  695. std::fstream is2("./temp/testfilebig");
  696. UploadPartData(cos, bucket_name, object_name, upload_id, is2, 2,
  697. &etag2);
  698. numbers.push_back(2);
  699. etags.push_back(etag2);
  700. is2.close();
  701. CompleteMultiUpload(cos, bucket_name, object_name, upload_id, etags, numbers);
  702. }
  703. void PutObjectACL(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  704. const std::string& object_name) {
  705. // 1 设置ACL配置(通过Body,
  706. // 设置ACL可以通过Body、Header两种方式,但只能二选一,否则会有冲突)
  707. {
  708. qcloud_cos::PutObjectACLReq req(bucket_name, object_name);
  709. qcloud_cos::Owner owner = {"qcs::cam::uin/xxx:uin/xxx",
  710. "qcs::cam::uin/xxx:uin/xxx"};
  711. qcloud_cos::Grant grant;
  712. req.SetOwner(owner);
  713. grant.m_grantee.m_type = "CanonicalUser";
  714. grant.m_grantee.m_id = "qcs::cam::uin/xxx:uin/xxx";
  715. grant.m_perm = "READ";
  716. req.AddAccessControlList(grant);
  717. qcloud_cos::PutObjectACLResp resp;
  718. qcloud_cos::CosResult result = cos.PutObjectACL(req, &resp);
  719. std::cout << "===================PutObjectACLResponse====================="
  720. << std::endl;
  721. PrintResult(result, resp);
  722. std::cout << "============================================================="
  723. "======="
  724. << std::endl;
  725. }
  726. // 2 设置ACL配置(通过Header,
  727. // 设置ACL可以通过Body、Header两种方式,但只能二选一,否则会有冲突)
  728. {
  729. qcloud_cos::PutObjectACLReq req(bucket_name, object_name);
  730. req.SetXCosAcl("default");
  731. qcloud_cos::PutObjectACLResp resp;
  732. qcloud_cos::CosResult result = cos.PutObjectACL(req, &resp);
  733. std::cout << "===================PutObjectACLResponse====================="
  734. << std::endl;
  735. PrintResult(result, resp);
  736. std::cout << "============================================================="
  737. "======="
  738. << std::endl;
  739. }
  740. }
  741. void GetObjectACL(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  742. const std::string& object_name) {
  743. qcloud_cos::GetObjectACLReq req(bucket_name, object_name);
  744. qcloud_cos::GetObjectACLResp resp;
  745. qcloud_cos::CosResult result = cos.GetObjectACL(req, &resp);
  746. std::cout << "===================GetObjectACLResponse====================="
  747. << std::endl;
  748. PrintResult(result, resp);
  749. std::cout
  750. << "===================================================================="
  751. << std::endl;
  752. }
  753. // 为已存在的 Object 设置标签(Tag)
  754. void PutObjectTagging(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  755. const std::string& object_name) {
  756. qcloud_cos::PutObjectTaggingReq req(bucket_name, object_name);
  757. qcloud_cos::PutObjectTaggingResp resp;
  758. std::vector<Tag> tagset;
  759. Tag tag1;
  760. tag1.SetKey("age");
  761. tag1.SetValue("19");
  762. Tag tag2;
  763. tag2.SetKey("name");
  764. tag2.SetValue("xiaoming");
  765. Tag tag3;
  766. tag3.SetKey("sex");
  767. tag3.SetValue("male");
  768. tagset.push_back(tag1);
  769. tagset.push_back(tag2);
  770. tagset.push_back(tag3);
  771. req.SetTagSet(tagset);
  772. qcloud_cos::CosResult result = cos.PutObjectTagging(req, &resp);
  773. std::cout << "===================PutBucketTagging====================="
  774. << std::endl;
  775. PrintResult(result, resp);
  776. std::cout
  777. << "===================================================================="
  778. << std::endl;
  779. }
  780. //查询指定Object的标签
  781. void GetObjectTagging(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  782. const std::string& object_name) {
  783. qcloud_cos::GetObjectTaggingReq req(bucket_name, object_name);
  784. qcloud_cos::GetObjectTaggingResp resp;
  785. qcloud_cos::CosResult result = cos.GetObjectTagging(req, &resp);
  786. std::cout << "===================GetObjectTagging====================="
  787. << std::endl;
  788. std::vector<Tag> tagset = resp.GetTagSet();
  789. for (std::vector<Tag>::iterator it = tagset.begin(); it != tagset.end();
  790. ++it) {
  791. std::cout << it->GetKey() << ":" << it->GetValue() << std::endl;
  792. }
  793. PrintResult(result, resp);
  794. std::cout
  795. << "===================================================================="
  796. << std::endl;
  797. }
  798. //删除指定Object的标签
  799. void DeleteObjectTagging(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  800. const std::string& object_name) {
  801. qcloud_cos::DeleteObjectTaggingReq req(bucket_name, object_name);
  802. qcloud_cos::DeleteObjectTaggingResp resp;
  803. qcloud_cos::CosResult result = cos.DeleteObjectTagging(req, &resp);
  804. std::cout << "===================DeleteObjectTagging====================="
  805. << std::endl;
  806. PrintResult(result, resp);
  807. std::cout
  808. << "===================================================================="
  809. << std::endl;
  810. }
  811. void GeneratePresignedUrl(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
  812. qcloud_cos::GeneratePresignedUrlReq req(bucket_name, "seven_50M.tmp",
  813. qcloud_cos::HTTP_GET); std::string presigned_url = cos.GeneratePresignedUrl(req);
  814. std::cout << "===================GeneratePresignedUrl====================="
  815. << std::endl;
  816. std::cout << "Presigend Url=[" <<presigned_url << "]" << std::endl;
  817. std::cout
  818. << "===================================================================="
  819. << std::endl;
  820. }
  821. void GetBucketLocation(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
  822. std::cout << "===================GetBucketLocation====================="
  823. << std::endl;
  824. std::cout << "Bucket=" << bucket_name << ", Location=" <<
  825. cos.GetBucketLocation(bucket_name) << std::endl;
  826. std::cout
  827. << "===================================================================="
  828. << std::endl;
  829. }
  830. void IsObjectExist(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
  831. std::cout << "===================IsObjectExist====================="
  832. << std::endl;
  833. std::cout << (cos.IsObjectExist(bucket_name, "abcdefg") ? "true" : "false") << std::endl;
  834. std::cout << (cos.IsObjectExist(bucket_name, "/test/test1") ? "true" : "false") << std::endl;
  835. std::cout
  836. << "===================================================================="
  837. << std::endl;
  838. }
  839. void IsBucketExist(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
  840. std::cout << "===================IsBucketExist====================="
  841. << std::endl;
  842. std::cout << (cos.IsBucketExist("abcdefg") ? "true" : "false") << std::endl;
  843. std::cout << (cos.IsBucketExist(bucket_name) ? "true" : "false") << std::endl;
  844. std::cout
  845. << "===================================================================="
  846. << std::endl;
  847. }
  848. void PutObjectCopy(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  849. const std::string& object_name, const std::string& source) {
  850. qcloud_cos::PutObjectCopyReq req(bucket_name, object_name);
  851. req.SetXCosCopySource(source);
  852. qcloud_cos::PutObjectCopyResp resp;
  853. qcloud_cos::CosResult result = cos.PutObjectCopy(req, &resp);
  854. std::cout << "===================PutObjectCopyResponse====================="
  855. << std::endl;
  856. PrintResult(result, resp);
  857. std::cout
  858. << "===================================================================="
  859. << std::endl;
  860. }
  861. void DeleteObject(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  862. const std::string& object_name) {
  863. qcloud_cos::DeleteObjectReq req(bucket_name, object_name);
  864. qcloud_cos::DeleteObjectResp resp;
  865. qcloud_cos::CosResult result = cos.DeleteObject(req, &resp);
  866. std::cout << "===================DeleteObjectResponse====================="
  867. << std::endl;
  868. PrintResult(result, resp);
  869. std::cout << "========================================================="
  870. << std::endl;
  871. }
  872. void DeleteObjects(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  873. const std::vector<ObjectVersionPair>& objects) {
  874. qcloud_cos::DeleteObjectsReq req(bucket_name, objects);
  875. qcloud_cos::DeleteObjectsResp resp;
  876. qcloud_cos::CosResult result = cos.DeleteObjects(req, &resp);
  877. std::cout << "===================DeleteObjectsResponse====================="
  878. << std::endl;
  879. PrintResult(result, resp);
  880. std::cout << "========================================================="
  881. << std::endl;
  882. std::cout << "Resp body=[" << resp.DebugString() << "]" << std::endl;
  883. }
  884. void UploadPartCopy(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  885. const std::string& object_name,
  886. const std::string& upload_id, const std::string& source,
  887. const std::string& range, uint64_t number,
  888. std::string* etag) {
  889. qcloud_cos::UploadPartCopyDataReq req(bucket_name, object_name, upload_id,
  890. number);
  891. req.SetXCosCopySource(source);
  892. req.SetXCosCopySourceRange(range);
  893. qcloud_cos::UploadPartCopyDataResp resp;
  894. qcloud_cos::CosResult result = cos.UploadPartCopyData(req, &resp);
  895. std::cout << "===================UploadPartCopyDataResp====================="
  896. << std::endl;
  897. PrintResult(result, resp);
  898. std::cout
  899. << "===================================================================="
  900. << std::endl;
  901. if (result.IsSucc()) {
  902. *etag = resp.GetEtag();
  903. }
  904. }
  905. //用户自行调用初始化、拷贝分块、完成分块接口进行分块拷贝
  906. void UploadCopy(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
  907. std::string upload_id;
  908. std::string object_name = "test/sevenyou_200MB_part_copy_from_north";
  909. std::vector<uint64_t> numbers;
  910. std::vector<std::string> etags;
  911. std::string etag1 = "", etag2 = "";
  912. InitMultiUpload(cos, bucket_name, object_name, &upload_id);
  913. UploadPartCopy(cos, bucket_name, object_name, upload_id,
  914. "xxxx-appid.cos.region.myqcloud.com/objectkey",
  915. "bytes=0-104857600", 1, &etag1);
  916. numbers.push_back(1);
  917. etags.push_back(etag1);
  918. UploadPartCopy(cos, bucket_name, object_name, upload_id,
  919. "xxxx-appid.cos.region.myqcloud.com/objectkey",
  920. "bytes=104857600-209715200", 2, &etag2);
  921. numbers.push_back(2);
  922. etags.push_back(etag2);
  923. CompleteMultiUpload(cos, bucket_name, object_name, upload_id, etags,
  924. numbers);
  925. }
  926. void Copy(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  927. const std::string& object_name, const std::string& source) {
  928. qcloud_cos::CopyReq req(bucket_name, object_name);
  929. qcloud_cos::CopyResp resp;
  930. req.SetXCosCopySource(source);
  931. qcloud_cos::CosResult result = cos.Copy(req, &resp);
  932. std::cout << "===================Copy=====================" << std::endl;
  933. PrintResult(result, resp);
  934. std::cout
  935. << "===================================================================="
  936. << std::endl;
  937. }
  938. void ListMultipartUpload(qcloud_cos::CosAPI& cos,
  939. const std::string& bucket_name) {
  940. qcloud_cos::ListMultipartUploadReq req(bucket_name);
  941. qcloud_cos::ListMultipartUploadResp resp;
  942. qcloud_cos::CosResult result = cos.ListMultipartUpload(req, &resp);
  943. std::vector<Upload> rst = resp.GetUpload();
  944. for (std::vector<qcloud_cos::Upload>::const_iterator itr = rst.begin();
  945. itr != rst.end(); ++itr) {
  946. const qcloud_cos::Upload& upload = *itr;
  947. std::cout << "key = " << upload.m_key << ", uploadid= " << upload.m_uploadid
  948. << ", storagen class = " << upload.m_storage_class
  949. << ", m_initiated= " << upload.m_initiated << std::endl;
  950. }
  951. std::cout << "===================ListMultipartUpload====================="
  952. << std::endl;
  953. PrintResult(result, resp);
  954. std::cout
  955. << "===================================================================="
  956. << std::endl;
  957. }
  958. // 开启bucket日志配置
  959. void PutBucketLogging(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  960. const std::string& TargetBucketname,
  961. const std::string& TargetPrefix) {
  962. qcloud_cos::PutBucketLoggingReq req(bucket_name);
  963. qcloud_cos::LoggingEnabled rules;
  964. rules.SetTargetBucket(TargetBucketname);
  965. rules.SetTargetPrefix(TargetPrefix);
  966. req.SetLoggingEnabled(rules);
  967. qcloud_cos::PutBucketLoggingResp resp;
  968. qcloud_cos::CosResult result = cos.PutBucketLogging(req, &resp);
  969. std::cout
  970. << "===================PutBucketLoggingResponse====================="
  971. << std::endl;
  972. PrintResult(result, resp);
  973. std::cout
  974. << "===================================================================="
  975. << std::endl;
  976. }
  977. // 获取bucket日志配置
  978. void GetBucketLogging(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
  979. qcloud_cos::GetBucketLoggingReq req(bucket_name);
  980. qcloud_cos::GetBucketLoggingResp resp;
  981. qcloud_cos::CosResult result = cos.GetBucketLogging(req, &resp);
  982. std::cout
  983. << "===================GetBucketLoggingResponse====================="
  984. << std::endl;
  985. const qcloud_cos::LoggingEnabled loggingenabled = resp.GetLoggingEnabled();
  986. std::cout << "TargetBucket = " << loggingenabled.GetTargetBucket()
  987. << std::endl;
  988. std::cout << "TargetPrefix = " << loggingenabled.GetTargetPrefix()
  989. << std::endl;
  990. PrintResult(result, resp);
  991. std::cout
  992. << "===================================================================="
  993. << std::endl;
  994. }
  995. // 设置存储桶绑定自定义域名
  996. void PutBucketDomain(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
  997. qcloud_cos::PutBucketDomainReq req(bucket_name);
  998. DomainRule rules;
  999. rules.SetStatus("ENABLED");
  1000. rules.SetName("www.abc.com");
  1001. rules.SetType("REST");
  1002. // rules.SetForcedReplacement();
  1003. req.SetDomainRule(rules);
  1004. qcloud_cos::PutBucketDomainResp resp;
  1005. qcloud_cos::CosResult result = cos.PutBucketDomain(req, &resp);
  1006. std::cout << "===================PutBucketDomainResponse====================="
  1007. << std::endl;
  1008. PrintResult(result, resp);
  1009. std::cout
  1010. << "===================================================================="
  1011. << std::endl;
  1012. }
  1013. // 获取存储桶绑定自定义域名
  1014. void GetBucketDomain(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
  1015. qcloud_cos::GetBucketDomainReq req(bucket_name);
  1016. qcloud_cos::GetBucketDomainResp resp;
  1017. qcloud_cos::CosResult result = cos.GetBucketDomain(req, &resp);
  1018. std::cout << "===================GetBucketDomainResponse====================="
  1019. << std::endl;
  1020. PrintResult(result, resp);
  1021. std::cout
  1022. << "===================================================================="
  1023. << std::endl;
  1024. }
  1025. // 为存储桶配置静态网站
  1026. void PutBucketWebsite(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
  1027. qcloud_cos::PutBucketWebsiteReq req(bucket_name);
  1028. qcloud_cos::PutBucketWebsiteResp resp;
  1029. req.SetSuffix("index.xml"); //必选项
  1030. req.SetProtocol("https");
  1031. req.SetKey("Error.html");
  1032. //设置重定向规则,最多设置100条
  1033. //设置第一条规则
  1034. qcloud_cos::RoutingRule routerule1;
  1035. qcloud_cos::Condition temp_condtion1;
  1036. temp_condtion1.SetHttpErrorCodeReturnedEquals(404); //需要设置,默认404
  1037. routerule1.SetCondition(temp_condtion1);
  1038. qcloud_cos::Redirect temp_redirect1;
  1039. temp_redirect1.SetProtocol("https");
  1040. temp_redirect1.SetReplaceKeyWith("404.htmp");
  1041. routerule1.SetRedirect(temp_redirect1);
  1042. //设置第二条规则
  1043. qcloud_cos::RoutingRule routerule2;
  1044. qcloud_cos::Condition temp_condtion2;
  1045. temp_condtion2.SetHttpErrorCodeReturnedEquals(403); //需要设置,默认404
  1046. routerule2.SetCondition(temp_condtion2);
  1047. qcloud_cos::Redirect temp_redirect2;
  1048. temp_redirect2.SetProtocol("https");
  1049. temp_redirect2.SetReplaceKeyWith("403.htmp");
  1050. routerule2.SetRedirect(temp_redirect2);
  1051. //设置第三条规则
  1052. qcloud_cos::RoutingRule routerule3;
  1053. qcloud_cos::Condition temp_condtion3;
  1054. temp_condtion3.SetKeyPrefixEquals("img/");
  1055. temp_condtion3.SetHttpErrorCodeReturnedEquals(402);
  1056. routerule3.SetCondition(temp_condtion3);
  1057. qcloud_cos::Redirect temp_redirect3;
  1058. temp_redirect3.SetProtocol("https");
  1059. temp_redirect3.SetReplaceKeyWith("401.htmp");
  1060. routerule3.SetRedirect(temp_redirect3);
  1061. qcloud_cos::RoutingRule routerule4;
  1062. qcloud_cos::Condition temp_condtion4;
  1063. temp_condtion4.SetKeyPrefixEquals("img1/");
  1064. routerule4.SetCondition(temp_condtion4);
  1065. qcloud_cos::Redirect temp_redirect4;
  1066. temp_redirect4.SetProtocol("https");
  1067. temp_redirect4.SetReplaceKeyPrefixWith("402.htmp");
  1068. routerule4.SetRedirect(temp_redirect4);
  1069. req.AddRoutingRule(routerule1);
  1070. req.AddRoutingRule(routerule2);
  1071. req.AddRoutingRule(routerule3);
  1072. req.AddRoutingRule(routerule4);
  1073. qcloud_cos::CosResult result = cos.PutBucketWebsite(req, &resp);
  1074. std::cout << "===================PutBucketWebsite====================="
  1075. << std::endl;
  1076. PrintResult(result, resp);
  1077. std::cout
  1078. << "===================================================================="
  1079. << std::endl;
  1080. }
  1081. // 获取与存储桶关联的静态网站配置信息
  1082. void GetBucketWebsite(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
  1083. qcloud_cos::GetBucketWebsiteReq req(bucket_name);
  1084. qcloud_cos::GetBucketWebsiteResp resp;
  1085. qcloud_cos::CosResult result = cos.GetBucketWebsite(req, &resp);
  1086. std::cout << "===================GetBucketWebsite====================="
  1087. << std::endl;
  1088. std::cout << "Suffix:" << resp.GetSuffix() << std::endl;
  1089. std::cout << "Protocol:" << resp.GetProtocol() << std::endl;
  1090. std::cout << "ErrorDocument:" << resp.GetKey() << std::endl;
  1091. std::vector<RoutingRule> routingrules = resp.GetRoutingRules();
  1092. std::vector<RoutingRule>::iterator it = routingrules.begin();
  1093. for (; it != routingrules.end(); ++it) {
  1094. const Condition& condition = it->GetCondition();
  1095. std::cout << "condition httpcode"
  1096. << condition.GetHttpErrorCodeReturnedEquals() << std::endl;
  1097. std::cout << "condition KeyPrefixEquals:" << condition.GetKeyPrefixEquals()
  1098. << std::endl;
  1099. const Redirect& redirect = it->GetRedirect();
  1100. std::cout << "redirect Protocol:" << redirect.GetProtocol() << std::endl;
  1101. std::cout << "redirect ReplaceKeyWith" << redirect.GetReplaceKeyWith()
  1102. << std::endl;
  1103. std::cout << "redirect ReplaceKeyPrefixWith"
  1104. << redirect.GetReplaceKeyPrefixWith() << std::endl;
  1105. std::cout << std::endl;
  1106. }
  1107. PrintResult(result, resp);
  1108. std::cout
  1109. << "===================================================================="
  1110. << std::endl;
  1111. }
  1112. // 删除存储桶中的静态网站配置
  1113. void DeleteBucketWebsite(qcloud_cos::CosAPI& cos,
  1114. const std::string& bucket_name) {
  1115. qcloud_cos::DeleteBucketWebsiteReq req(bucket_name);
  1116. qcloud_cos::DeleteBucketWebsiteResp resp;
  1117. qcloud_cos::CosResult result = cos.DeleteBucketWebsite(req, &resp);
  1118. std::cout << "===================DeleteBucketWebsite====================="
  1119. << std::endl;
  1120. PrintResult(result, resp);
  1121. std::cout
  1122. << "===================================================================="
  1123. << std::endl;
  1124. }
  1125. // 为已存在的 Bucket 设置标签(Tag)
  1126. void PutBucketTagging(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
  1127. std::vector<Tag> tagset;
  1128. Tag tag1;
  1129. tag1.SetKey("age");
  1130. tag1.SetValue("19");
  1131. Tag tag2;
  1132. tag2.SetKey("name");
  1133. tag2.SetValue("xiaoming");
  1134. tagset.push_back(tag1);
  1135. tagset.push_back(tag2);
  1136. qcloud_cos::PutBucketTaggingReq req(bucket_name);
  1137. qcloud_cos::PutBucketTaggingResp resp;
  1138. req.SetTagSet(tagset);
  1139. qcloud_cos::CosResult result = cos.PutBucketTagging(req, &resp);
  1140. std::cout << "===================PutBucketTagging====================="
  1141. << std::endl;
  1142. PrintResult(result, resp);
  1143. std::cout
  1144. << "===================================================================="
  1145. << std::endl;
  1146. }
  1147. //用于查询指定存储桶下已有的存储桶标签
  1148. void GetBucketTagging(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
  1149. qcloud_cos::GetBucketTaggingReq req(bucket_name);
  1150. qcloud_cos::GetBucketTaggingResp resp;
  1151. qcloud_cos::CosResult result = cos.GetBucketTagging(req, &resp);
  1152. std::cout << "===================GetBucketTagging====================="
  1153. << std::endl;
  1154. std::vector<Tag> tagset = resp.GetTagSet();
  1155. for (std::vector<Tag>::iterator it = tagset.begin(); it != tagset.end();
  1156. ++it) {
  1157. std::cout << it->GetKey() << ":" << it->GetValue() << std::endl;
  1158. }
  1159. PrintResult(result, resp);
  1160. std::cout
  1161. << "===================================================================="
  1162. << std::endl;
  1163. }
  1164. //删除指定存储桶下已有的存储桶标签
  1165. void DeleteBucketTagging(qcloud_cos::CosAPI& cos,
  1166. const std::string& bucket_name) {
  1167. qcloud_cos::DeleteBucketTaggingReq req(bucket_name);
  1168. qcloud_cos::DeleteBucketTaggingResp resp;
  1169. qcloud_cos::CosResult result = cos.DeleteBucketTagging(req, &resp);
  1170. std::cout << "===================DeleteBucketTagging====================="
  1171. << std::endl;
  1172. PrintResult(result, resp);
  1173. std::cout
  1174. << "===================================================================="
  1175. << std::endl;
  1176. }
  1177. // 在存储桶中创建清单任务
  1178. void PutBucketInventory(qcloud_cos::CosAPI& cos,
  1179. const std::string& bucket_name) {
  1180. qcloud_cos::PutBucketInventoryReq req(bucket_name);
  1181. req.SetId("list3");
  1182. COSBucketDestination destination;
  1183. destination.SetFormat("CSV");
  1184. // destination.SetAccountId("100010974959");
  1185. destination.SetAccountId("appid");
  1186. std::string target_bucket =
  1187. "qcs::cos:ap-guangzhou::xxxxxx-appid";
  1188. destination.SetBucket(target_bucket);
  1189. destination.SetPrefix("/");
  1190. destination.SetEncryption(true);
  1191. OptionalFields fields;
  1192. fields.SetIsSize(true);
  1193. fields.SetIsLastModified(true);
  1194. fields.SetIsStorageClass(true);
  1195. fields.SetIsMultipartUploaded(true);
  1196. fields.SetIsReplicationStatus(true);
  1197. fields.SetIsEtag(true);
  1198. Inventory inventory;
  1199. inventory.SetIsEnable(true);
  1200. inventory.SetIncludedObjectVersions("All");
  1201. inventory.SetFilter("/");
  1202. inventory.SetId(req.GetId());
  1203. inventory.SetFrequency("Daily");
  1204. inventory.SetCOSBucketDestination(destination);
  1205. inventory.SetOptionalFields(fields);
  1206. req.SetInventory(inventory);
  1207. qcloud_cos::PutBucketInventoryResp resp;
  1208. qcloud_cos::CosResult result = cos.PutBucketInventory(req, &resp);
  1209. std::cout << "===================PutBucketinventory====================="
  1210. << std::endl;
  1211. PrintResult(result, resp);
  1212. std::cout
  1213. << "===================================================================="
  1214. << std::endl;
  1215. }
  1216. // 用于查询存储桶中用户的清单任务信息
  1217. void GetBucketInventory(qcloud_cos::CosAPI& cos,
  1218. const std::string& bucket_name) {
  1219. qcloud_cos::GetBucketInventoryReq req(bucket_name);
  1220. qcloud_cos::GetBucketInventoryResp resp;
  1221. req.SetId("list3");
  1222. qcloud_cos::CosResult result = cos.GetBucketInventory(req, &resp);
  1223. // std::cout << "===================GetBucketinventory====================="
  1224. // << std::endl;
  1225. //
  1226. // const Inventory inventory = resp.GetInventory();
  1227. // std::cout << "inventory isenabled:" << inventory.GetIsEnable() <<
  1228. // std::endl; std::cout << "inventory IncludedObjectVersions:" <<
  1229. // inventory.GetIncludedObjectVersions() << std::endl; std::cout << "inventory
  1230. // filter:" << inventory.GetFilter() << std::endl; std::cout << "inventory
  1231. // id:"
  1232. // << inventory.GetId() << std::endl; std::cout << "inventory Frequency:" <<
  1233. // inventory.GetFrequency() << std::endl;
  1234. // std:: cout << "===================================" << std::endl;
  1235. // COSBucketDestination destination = inventory.GetCOSBucketDestination();
  1236. // std::cout << "destination Format:" << destination.GetFormat() << std::endl;
  1237. // std::cout << "destination AccountID:" << destination.GetAccountId() <<
  1238. // std::endl; std::cout << "destination Bucket:" << destination.GetBucket() <<
  1239. // std::endl; std::cout << "destination Encryption:" <<
  1240. // destination.GetEncryption() << std::endl;
  1241. // std:: cout << "===================================" << std::endl;
  1242. //
  1243. // OptionalFields fields = inventory.GetOptionalFields();
  1244. // std::cout << "fields Size:" << fields.GetIsSize() << std::endl;
  1245. // std::cout << "fields LastModified:" << fields.GetIsLastModified() <<
  1246. // std::endl; std::cout << "fields StorageClass:" <<
  1247. // fields.GetIsStorageClass()
  1248. // << std::endl; std::cout << "fields Region:" <<
  1249. // fields.GetIsMultipartUploaded() << std::endl; std::cout << "fields
  1250. // ReplicationStatus:" << fields.GetIsReplicationStatus() << std::endl;
  1251. // std::cout << "fields Tag:" << fields.GetIsETag() << std::endl;
  1252. PrintResult(result, resp);
  1253. std::cout
  1254. << "===================================================================="
  1255. << std::endl;
  1256. }
  1257. // 请求返回一个存储桶中的所有清单任务
  1258. void ListBucketInventoryConfigurations(qcloud_cos::CosAPI& cos,
  1259. const std::string& bucket_name) {
  1260. qcloud_cos::ListBucketInventoryConfigurationsReq req(bucket_name);
  1261. qcloud_cos::ListBucketInventoryConfigurationsResp resp;
  1262. qcloud_cos::CosResult result =
  1263. cos.ListBucketInventoryConfigurations(req, &resp);
  1264. // std::cout <<
  1265. // "===================ListBucketInventoryConfigurations====================="
  1266. // << std::endl;
  1267. //
  1268. // std::vector<Inventory> inventory_vec = resp.GetInventory();
  1269. //
  1270. // std::cout << resp.GetIsTruncated() << std::endl;
  1271. // std::cout << resp.GetContinuationToken() << std::endl;
  1272. // std::cout << resp.GetNextContinuationToken() << std::endl;
  1273. //
  1274. // std::vector<Inventory>::iterator itr = inventory_vec.begin();
  1275. // for(; itr != inventory_vec.end(); ++itr) {
  1276. //
  1277. // std:: cout << "==============Inventory=============================" <<
  1278. // std::endl; std::cout << "inventory id:" << itr->GetId() << std::endl;
  1279. // std::cout << "inventory isenabled:" << itr->GetIsEnable() << std::endl;
  1280. // std::cout << "inventory IncludedObjectVersions:" <<
  1281. // itr->GetIncludedObjectVersions() << std::endl; std::cout << "inventory
  1282. // filter:" << itr->GetFilter() << std::endl; std::cout << "inventory
  1283. // Frequency:" << itr->GetFrequency() << std::endl;
  1284. // std:: cout << "==============GetCOSBucketDestination=================="
  1285. // << std::endl; COSBucketDestination destination =
  1286. // itr->GetCOSBucketDestination(); std::cout << "destination Format:" <<
  1287. // destination.GetFormat() << std::endl; std::cout << "destination
  1288. // AccountID:" << destination.GetAccountId() << std::endl; std::cout <<
  1289. // "destination Bucket:" << destination.GetBucket() << std::endl; std::cout
  1290. // << "destination Encryption:" << destination.GetEncryption() <<
  1291. // std::endl;
  1292. //
  1293. //
  1294. // std:: cout << "===================OptionalFields======================"
  1295. // << std::endl; OptionalFields fields = itr->GetOptionalFields();
  1296. // std::cout << "fields Size:" << fields.GetIsSize() << std::endl;
  1297. // std::cout << "fields LastModified:" << fields.GetIsLastModified() <<
  1298. // std::endl; std::cout << "fields StorageClass:" <<
  1299. // fields.GetIsStorageClass() << std::endl; std::cout << "fields Region:"
  1300. // << fields.GetIsMultipartUploaded() << std::endl; std::cout << "fields
  1301. // ReplicationStatus:" << fields.GetIsReplicationStatus() << std::endl;
  1302. // std::cout << "fields Tag:" << fields.GetIsETag() << std::endl;
  1303. // }
  1304. // std::cout << "===============ListBucketInventoryConfigurations
  1305. // end================" << std::endl;
  1306. PrintResult(result, resp);
  1307. std::cout
  1308. << "===================================================================="
  1309. << std::endl;
  1310. }
  1311. // 用于删除存储桶中指定的清单任务.
  1312. void DeleteBucketInventory(qcloud_cos::CosAPI& cos,
  1313. const std::string& bucket_name) {
  1314. qcloud_cos::DeleteBucketInventoryReq req(bucket_name);
  1315. qcloud_cos::DeleteBucketInventoryResp resp;
  1316. req.SetId("list");
  1317. qcloud_cos::CosResult result = cos.DeleteBucketInventory(req, &resp);
  1318. std::cout << "===================DeleteBucketinventory====================="
  1319. << std::endl;
  1320. PrintResult(result, resp);
  1321. std::cout
  1322. << "===================================================================="
  1323. << std::endl;
  1324. }
  1325. // 配置存储桶referer
  1326. void PutBucketReferer(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
  1327. qcloud_cos::PutBucketRefererReq req(bucket_name);
  1328. qcloud_cos::PutBucketRefererResp resp;
  1329. req.SetStatus("Enabled");
  1330. req.SetRefererType("White-List");
  1331. req.AddDomain("test1.com");
  1332. req.AddDomain("test2.com");
  1333. qcloud_cos::CosResult result = cos.PutBucketReferer(req, &resp);
  1334. std::cout << "===================PutBucketReferer====================="
  1335. << std::endl;
  1336. PrintResult(result, resp);
  1337. std::cout
  1338. << "===================================================================="
  1339. << std::endl;
  1340. }
  1341. // 获取存储桶referer
  1342. void GetBucketReferer(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
  1343. qcloud_cos::GetBucketRefererReq req(bucket_name);
  1344. qcloud_cos::GetBucketRefererResp resp;
  1345. qcloud_cos::CosResult result = cos.GetBucketReferer(req, &resp);
  1346. std::cout << "===================GetBucketReferer====================="
  1347. << std::endl;
  1348. PrintResult(result, resp);
  1349. if (result.IsSucc()) {
  1350. std::cout << "Status:" << resp.GetStatus() << std::endl;
  1351. std::cout << "RefererType:" << resp.GetRefererType() << std::endl;
  1352. for (auto& domain : resp.GetDomainList()) {
  1353. std::cout << "Domain:" << domain << std::endl;
  1354. }
  1355. std::cout << "EmptyReferConfiguration:" << resp.GetEmptyReferConf()
  1356. << std::endl;
  1357. }
  1358. std::cout
  1359. << "===================================================================="
  1360. << std::endl;
  1361. }
  1362. // 创建直播通道
  1363. void CreateLiveChannel(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  1364. const std::string& channel_name) {
  1365. qcloud_cos::PutLiveChannelReq req(bucket_name, channel_name);
  1366. qcloud_cos::PutLiveChannelResp resp;
  1367. std::map<std::string, std::string> url_params;
  1368. url_params.insert(std::make_pair("a", "b"));
  1369. LiveChannelConfiguration config("test", "Enabled", "HLS", 5, 10,
  1370. "playlist.m3u8");
  1371. req.SetLiveChannelConfig(config);
  1372. req.SetUrlParams(url_params);
  1373. req.SetExpire(1000);
  1374. qcloud_cos::CosResult result = cos.PutLiveChannel(req, &resp);
  1375. std::cout << "===================CreateLiveChannel====================="
  1376. << std::endl;
  1377. PrintResult(result, resp);
  1378. if (result.IsSucc()) {
  1379. std::cout << "PlayUrl:" << resp.GetPlayUrl() << std::endl;
  1380. std::cout << "PublishUrl:" << resp.GetPublishUrl() << std::endl;
  1381. }
  1382. std::cout
  1383. << "===================================================================="
  1384. << std::endl;
  1385. }
  1386. // 获取直播通道配置
  1387. void GetLiveChannel(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  1388. const std::string& channel_name) {
  1389. qcloud_cos::GetLiveChannelReq req(bucket_name, channel_name);
  1390. qcloud_cos::GetLiveChannelResp resp;
  1391. qcloud_cos::CosResult result = cos.GetLiveChannel(req, &resp);
  1392. std::cout << "===================GetLiveChannel====================="
  1393. << std::endl;
  1394. PrintResult(result, resp);
  1395. if (result.IsSucc()) {
  1396. LiveChannelConfiguration chan_conf = resp.GetLiveChannelConf();
  1397. std::stringstream oss;
  1398. oss << "Description:" << chan_conf.GetDescription() << ",";
  1399. oss << "Switch:" << chan_conf.GetSwitch() << ",";
  1400. oss << "Type:" << chan_conf.GetType() << ",";
  1401. oss << "FragDuration:" << chan_conf.GetFragDuration() << ",";
  1402. oss << "FragCount:" << chan_conf.GetFragCount() << ",";
  1403. oss << "PlaylistName:" << chan_conf.GetPlaylistName() << ",";
  1404. oss << "PublishUrls:" << chan_conf.GetPublishUrl() << ",";
  1405. oss << "PlayUrls:" << chan_conf.GetPlayUrl();
  1406. std::cout << "LiveChannelConfiguration:" << oss.str() << std::endl;
  1407. }
  1408. std::cout
  1409. << "===================================================================="
  1410. << std::endl;
  1411. }
  1412. // 获取通道推流URL
  1413. void GetRtmpSignedPublishUrl(qcloud_cos::CosAPI& cos,
  1414. const std::string& bucket_name,
  1415. const std::string& channel_name) {
  1416. std::cout << "===================GetRtmpSignedPublishUrl====================="
  1417. << std::endl;
  1418. std::cout << "PublishUrl:"
  1419. << cos.GetRtmpSignedPublishUrl(bucket_name, channel_name, 3600,
  1420. std::map<std::string, std::string>())
  1421. << std::endl;
  1422. std::cout
  1423. << "===================================================================="
  1424. << std::endl;
  1425. }
  1426. // 修改直播通道开关
  1427. void PutLiveChannelSwitch(qcloud_cos::CosAPI& cos,
  1428. const std::string& bucket_name,
  1429. const std::string& channel_name) {
  1430. qcloud_cos::PutLiveChannelSwitchReq req(bucket_name, channel_name);
  1431. qcloud_cos::PutLiveChannelSwitchResp resp;
  1432. qcloud_cos::CosResult result;
  1433. std::cout
  1434. << "===================PutLiveChannelSwitch disabled====================="
  1435. << std::endl;
  1436. req.SetDisabled();
  1437. result = cos.PutLiveChannelSwitch(req, &resp);
  1438. PrintResult(result, resp);
  1439. std::cout
  1440. << "===================PutLiveChannelSwitch enabled====================="
  1441. << std::endl;
  1442. req.SetEnabled();
  1443. result = cos.PutLiveChannelSwitch(req, &resp);
  1444. PrintResult(result, resp);
  1445. std::cout
  1446. << "===================================================================="
  1447. << std::endl;
  1448. }
  1449. // 获取直播通道推流历史
  1450. void GetLiveChannelHistory(qcloud_cos::CosAPI& cos,
  1451. const std::string& bucket_name,
  1452. const std::string& channel_name) {
  1453. qcloud_cos::GetLiveChannelHistoryReq req(bucket_name, channel_name);
  1454. qcloud_cos::GetLiveChannelHistoryResp resp;
  1455. qcloud_cos::CosResult result = cos.GetLiveChannelHistory(req, &resp);
  1456. std::cout << "===================GetLiveChannelHistory====================="
  1457. << std::endl;
  1458. PrintResult(result, resp);
  1459. if (result.IsSucc()) {
  1460. const std::vector<LiveRecord>& chan_history = resp.GetChanHistory();
  1461. std::vector<LiveRecord>::const_iterator it = chan_history.begin();
  1462. for (; it != chan_history.end(); ++it) {
  1463. std::stringstream oss;
  1464. oss << "StartTime:" << it->m_start_time << ", ";
  1465. oss << "EndTime:" << it->m_end_time << ", ";
  1466. oss << "RemoteAddr:" << it->m_remote_addr << ", ";
  1467. oss << "RequestId:" << it->m_request_id;
  1468. std::cout << oss.str() << std::endl;
  1469. }
  1470. }
  1471. std::cout
  1472. << "===================================================================="
  1473. << std::endl;
  1474. }
  1475. // 获取直播通道推流状态
  1476. void GetLiveChannelStatus(qcloud_cos::CosAPI& cos,
  1477. const std::string& bucket_name,
  1478. const std::string& channel_name) {
  1479. qcloud_cos::GetLiveChannelStatusReq req(bucket_name, channel_name);
  1480. qcloud_cos::GetLiveChannelStatusResp resp;
  1481. qcloud_cos::CosResult result = cos.GetLiveChannelStatus(req, &resp);
  1482. std::cout << "===================GetLiveChannelStatus====================="
  1483. << std::endl;
  1484. PrintResult(result, resp);
  1485. if (result.IsSucc()) {
  1486. const LiveChannelStatus& chan_status = resp.GetLiveChannelStatus();
  1487. if (chan_status.m_status == "Idle") {
  1488. std::cout << "Status:" << chan_status.m_status << std::endl;
  1489. } else {
  1490. std::stringstream oss;
  1491. oss << "Status:" << chan_status.m_status << ", ";
  1492. oss << "ConnectedTime:" << chan_status.m_connected_time << ", ";
  1493. oss << "RemoteAddr:" << chan_status.m_remote_addr << ", ";
  1494. oss << "RequestId:" << chan_status.m_request_id;
  1495. if (chan_status.m_has_video) {
  1496. oss << "Width:" << chan_status.m_video.m_width << ", ";
  1497. oss << "Heigh:" << chan_status.m_video.m_heigh << ", ";
  1498. oss << "FrameRate:" << chan_status.m_video.m_framerate << ", ";
  1499. oss << "Bindwidth:" << chan_status.m_video.m_bandwidth << ", ";
  1500. oss << "Codec:" << chan_status.m_video.m_codec << ", ";
  1501. }
  1502. if (chan_status.m_has_audio) {
  1503. oss << "Bindwidth:" << chan_status.m_audio.m_bandwidth << ", ";
  1504. oss << "SampleRate:" << chan_status.m_audio.m_samplerate << ", ";
  1505. oss << "Codec:" << chan_status.m_audio.m_codec << ", ";
  1506. }
  1507. std::cout << oss.str() << std::endl;
  1508. }
  1509. }
  1510. std::cout
  1511. << "===================================================================="
  1512. << std::endl;
  1513. }
  1514. // 下载直播通道点播播放列表
  1515. void GetLiveChannelVodPlaylist(qcloud_cos::CosAPI& cos,
  1516. const std::string& bucket_name,
  1517. const std::string& channel_name) {
  1518. qcloud_cos::GetLiveChannelVodPlaylistReq req(bucket_name, channel_name);
  1519. qcloud_cos::GetLiveChannelVodPlaylistResp resp;
  1520. req.SetTime(time(NULL) - 10000, time(NULL));
  1521. qcloud_cos::CosResult result = cos.GetLiveChannelVodPlaylist(req, &resp);
  1522. std::cout
  1523. << "===================GetLiveChannelVodPlaylist====================="
  1524. << std::endl;
  1525. PrintResult(result, resp);
  1526. if (result.IsSucc()) {
  1527. resp.WriteResultToFile("./playlist.m3u8");
  1528. }
  1529. std::cout
  1530. << "===================================================================="
  1531. << std::endl;
  1532. }
  1533. // 为直播通道产生点播播放列表
  1534. void PostLiveChannelVodPlaylist(qcloud_cos::CosAPI& cos,
  1535. const std::string& bucket_name,
  1536. const std::string& channel_name) {
  1537. qcloud_cos::PostLiveChannelVodPlaylistReq req(bucket_name, channel_name);
  1538. qcloud_cos::PostLiveChannelVodPlaylistResp resp;
  1539. req.SetTime(time(NULL) - 10000, time(NULL));
  1540. req.SetPlaylistName("newplaylist.m3u8");
  1541. qcloud_cos::CosResult result = cos.PostLiveChannelVodPlaylist(req, &resp);
  1542. std::cout
  1543. << "===================PostLiveChannelVodPlaylist====================="
  1544. << std::endl;
  1545. PrintResult(result, resp);
  1546. std::cout
  1547. << "===================================================================="
  1548. << std::endl;
  1549. }
  1550. // 删除直播通道
  1551. void DeleteLiveChannel(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  1552. const std::string& channel_name) {
  1553. qcloud_cos::DeleteLiveChannelReq req(bucket_name, channel_name);
  1554. qcloud_cos::DeleteLiveChannelResp resp;
  1555. qcloud_cos::CosResult result = cos.DeleteLiveChannel(req, &resp);
  1556. std::cout << "===================DeleteLiveChannel====================="
  1557. << std::endl;
  1558. PrintResult(result, resp);
  1559. std::cout
  1560. << "===================================================================="
  1561. << std::endl;
  1562. }
  1563. // 获取直播通道列表
  1564. void ListLiveChannel(qcloud_cos::CosAPI& cos, const std::string& bucket_name) {
  1565. qcloud_cos::ListLiveChannelReq req(bucket_name);
  1566. qcloud_cos::ListLiveChannelResp resp;
  1567. ListLiveChannelResult list_result;
  1568. qcloud_cos::CosResult result;
  1569. std::cout << "===================ListLiveChannel====================="
  1570. << std::endl;
  1571. do {
  1572. req.SetMaxKeys(10);
  1573. req.SetMarker(list_result.m_next_marker);
  1574. result = cos.ListLiveChannel(req, &resp);
  1575. PrintResult(result, resp);
  1576. if (result.IsSucc()) {
  1577. list_result = resp.GetListResult();
  1578. std::stringstream oss;
  1579. oss << "MaxKeys:" << list_result.m_max_keys << ", ";
  1580. oss << "Marker:" << list_result.m_marker << ", ";
  1581. oss << "Prefix:" << list_result.m_prefix << ", ";
  1582. oss << "IsTruncated:" << list_result.m_is_truncated << ", ";
  1583. oss << "NextMarker:" << list_result.m_next_marker << ", ";
  1584. std::vector<LiveChannel>::const_iterator it =
  1585. list_result.m_channels.begin();
  1586. for (; it != list_result.m_channels.end(); ++it) {
  1587. oss << "Name:" << it->m_name << ", LastModified:" << it->m_last_modified
  1588. << std::endl;
  1589. }
  1590. std::cout << oss.str() << std::endl;
  1591. resp.ClearResult();
  1592. }
  1593. } while (list_result.m_is_truncated == "true" && result.IsSucc());
  1594. std::cout
  1595. << "===================================================================="
  1596. << std::endl;
  1597. }
  1598. void TestIntelligentTiering(qcloud_cos::CosAPI& cos,
  1599. const std::string& bucket_name) {
  1600. {
  1601. qcloud_cos::PutBucketIntelligentTieringReq req(bucket_name);
  1602. qcloud_cos::PutBucketIntelligentTieringResp resp;
  1603. qcloud_cos::CosResult result;
  1604. std::cout << "===================PutIntelligentTiering====================="
  1605. << std::endl;
  1606. req.SetStatus(true);
  1607. req.SetDays(60);
  1608. result = cos.PutBucketIntelligentTiering(req, &resp);
  1609. PrintResult(result, resp);
  1610. std::cout << "============================================================="
  1611. "======="
  1612. << std::endl;
  1613. }
  1614. {
  1615. qcloud_cos::GetBucketIntelligentTieringReq req(bucket_name);
  1616. qcloud_cos::GetBucketIntelligentTieringResp resp;
  1617. qcloud_cos::CosResult result;
  1618. std::cout << "===================GetIntelligentTiering====================="
  1619. << std::endl;
  1620. result = cos.GetBucketIntelligentTiering(req, &resp);
  1621. PrintResult(result, resp);
  1622. if (result.IsSucc()) {
  1623. std::cout << "Status:" << resp.GetStatus() << std::endl;
  1624. std::cout << "Days:" << resp.GetDays() << std::endl;
  1625. std::cout << "RequestFrequent:" << resp.GetRequestFrequent() << std::endl;
  1626. }
  1627. std::cout << "============================================================="
  1628. "======="
  1629. << std::endl;
  1630. }
  1631. }
  1632. static void ResumableGetObject(qcloud_cos::CosAPI& cos,
  1633. const std::string& bucket_name,
  1634. const std::string& object_name,
  1635. const std::string& file_path) {
  1636. qcloud_cos::GetObjectByFileReq req(bucket_name, object_name, file_path);
  1637. qcloud_cos::GetObjectByFileResp resp;
  1638. CosResult resule = cos.ResumableGetObject(req, &resp);
  1639. if (resule.IsSucc()) {
  1640. std::cout << "ResumableGetObject Succ." << std::endl;
  1641. } else {
  1642. std::cout << "ResumableGetObject Fail." << std::endl;
  1643. }
  1644. std::cout
  1645. << "===================ResumableGetObject============================="
  1646. << std::endl;
  1647. PrintResult(resule, resp);
  1648. std::cout << "========================================================"
  1649. << std::endl;
  1650. }
  1651. static void ProgressCallback(uint64_t transferred_size, uint64_t total_size,
  1652. void* user_data) {
  1653. qcloud_cos::ObjectReq* req = static_cast<qcloud_cos::ObjectReq*>(user_data);
  1654. if (0 == transferred_size % 1048576) {
  1655. std::cout << "ObjectName:" << req->GetObjectName()
  1656. << ", TranferedSize:" << transferred_size
  1657. << ",TotalSize:" << total_size << std::endl;
  1658. }
  1659. }
  1660. static void MultiGetObjectAsyncDoneCallback(const SharedAsyncContext& context,
  1661. void* user_data) {
  1662. UNUSED_PARAM(user_data)
  1663. std::cout << "MultiGetObjectAsyncDoneCallback, BucketName:"
  1664. << context->GetBucketName()
  1665. << ", ObjectName:" << context->GetObjectName()
  1666. << ", LocalFile:" << context->GetLocalFilePath() << std::endl;
  1667. // qcloud_cos::MultiGetObjectReq对应的响应为qcloud_cos::GetObjectByFileResp
  1668. if (context->GetResult().IsSucc()) {
  1669. // 获取响应
  1670. std::cout << "AsyncMultiGetObject succeed" << std::endl;
  1671. std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
  1672. AsyncResp resp = context->GetAsyncResp();
  1673. std::cout << "ETag:" << resp.GetEtag() << std::endl;
  1674. std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
  1675. } else {
  1676. std::cout << "AsyncMultiGetObject failed" << std::endl;
  1677. std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;
  1678. }
  1679. }
  1680. // 异步下载对象,支持更新下载进度
  1681. void AsyncMultiGetObject(qcloud_cos::CosAPI& cos,
  1682. const std::string& bucket_name,
  1683. const std::string& object_name,
  1684. const std::string& file_path) {
  1685. qcloud_cos::AsyncMultiGetObjectReq req(bucket_name, object_name, file_path);
  1686. // 设置进度回调
  1687. req.SetTransferProgressCallback(&ProgressCallback);
  1688. // 设置状态回调
  1689. req.SetDoneCallback(&MultiGetObjectAsyncDoneCallback);
  1690. // 设置私有数据
  1691. req.SetUserData(&req);
  1692. // 开始下载
  1693. SharedAsyncContext context = cos.AsyncMultiGetObject(req);
  1694. // 等待下载结束
  1695. context->WaitUntilFinish();
  1696. // 检查结果
  1697. if (context->GetResult().IsSucc()) {
  1698. // 获取响应
  1699. std::cout << "AsyncMultiGetObject succeed" << std::endl;
  1700. std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
  1701. AsyncResp resp = context->GetAsyncResp();
  1702. std::cout << "ETag:" << resp.GetEtag() << std::endl;
  1703. std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
  1704. } else {
  1705. std::cout << "AsyncMultiGetObject failed" << std::endl;
  1706. std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;
  1707. }
  1708. //context->WaitUntilFinish()阻塞完毕的逻辑是:异步线程在下载时文件落盘完毕。
  1709. //此时您的主线程如果立即结束,异步线程可能正在进行资源释放、结束线程等过程。此时可能会产生crash。
  1710. //此问题并不会对文件的下载造成影响,仅仅是体验上可能会出现意料之外的崩溃信息。
  1711. //可以采用此策略避免crash。
  1712. //或者可以采用下方AsyncMultiGetObjectWithTaskManager函数中的方式,使用能透传TaskManager的接口,使用join的方式避免crash。
  1713. //特别注意,此处的TaskManager为全局共用,请务必保证在主线程结束前才能调用
  1714. std::this_thread::sleep_for(std::chrono::seconds(1));
  1715. }
  1716. // 异步下载对象,支持更新下载进度
  1717. // 回传taskManager对象,可用使用它等待异步线程彻底结束
  1718. void AsyncMultiGetObjectWithTaskManager(qcloud_cos::CosAPI& cos,
  1719. const std::string& bucket_name,
  1720. const std::string& object_name,
  1721. const std::string& file_path) {
  1722. qcloud_cos::AsyncMultiGetObjectReq req(bucket_name, object_name, file_path);
  1723. // 设置进度回调
  1724. req.SetTransferProgressCallback(&ProgressCallback);
  1725. // 设置状态回调
  1726. req.SetDoneCallback(&MultiGetObjectAsyncDoneCallback);
  1727. // 设置私有数据
  1728. req.SetUserData(&req);
  1729. // 开始下载
  1730. Poco::TaskManager* taskManager;
  1731. qcloud_cos::SharedAsyncContext context = cos.AsyncMultiGetObject(req,taskManager);
  1732. // 等待下载结束
  1733. context->WaitUntilFinish();
  1734. // 检查结果
  1735. if (context->GetResult().IsSucc()) {
  1736. // 获取响应
  1737. std::cout << "AsyncMultiGetObject succeed" << std::endl;
  1738. std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
  1739. AsyncResp resp = context->GetAsyncResp();
  1740. std::cout << "ETag:" << resp.GetEtag() << std::endl;
  1741. std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
  1742. } else {
  1743. std::cout << "AsyncMultiGetObject failed" << std::endl;
  1744. std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;
  1745. }
  1746. // 使用taskManager等待异步线程彻底结束
  1747. (*taskManager).joinAll();
  1748. }
  1749. static void MultiPutObjectAsyncDoneCallback(const SharedAsyncContext& context,
  1750. void* user_data) {
  1751. UNUSED_PARAM(user_data)
  1752. std::cout << "MultiPutObjectAsyncDoneCallback, BucketName:"
  1753. << context->GetBucketName()
  1754. << ", ObjectName:" << context->GetObjectName()
  1755. << ", LocalFile:" << context->GetLocalFilePath() << std::endl;
  1756. if (context->GetResult().IsSucc()) {
  1757. // 获取响应
  1758. std::cout << "AsyncMultiPutObject succeed" << std::endl;
  1759. std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
  1760. AsyncResp resp = context->GetAsyncResp();
  1761. //std::cout << "Location:" << resp.GetLocation() << std::endl;
  1762. //std::cout << "Bucket:" << resp.GetBucket() << std::endl;
  1763. //std::cout << "Key:" << resp.GetKey() << std::endl;
  1764. std::cout << "ETag:" << resp.GetEtag() << std::endl;
  1765. std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
  1766. } else {
  1767. std::cout << "AsyncMultiGetObject failed" << std::endl;
  1768. std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;
  1769. }
  1770. }
  1771. // 异步多上传对象,支持更新上传进度
  1772. void AsyncMultiPutObject(qcloud_cos::CosAPI& cos,
  1773. const std::string& bucket_name,
  1774. const std::string& object_name,
  1775. const std::string& local_file) {
  1776. qcloud_cos::AsyncMultiPutObjectReq req(bucket_name, object_name, local_file);
  1777. req.SetRecvTimeoutInms(1000 * 60);
  1778. // 设置上传进度回调
  1779. req.SetTransferProgressCallback(&ProgressCallback);
  1780. // 设置上传状态回调
  1781. req.SetDoneCallback(&MultiPutObjectAsyncDoneCallback);
  1782. // 设置私有数据
  1783. req.SetUserData(&req);
  1784. // 开始上传
  1785. SharedAsyncContext context = cos.AsyncMultiPutObject(req);
  1786. // 等待上传结束
  1787. std::cout << "wait finish..." << std::endl;
  1788. context->WaitUntilFinish();
  1789. // 检查结果
  1790. if (context->GetResult().IsSucc()) {
  1791. // 获取响应
  1792. std::cout << "AsyncMultiPutObject succeed" << std::endl;
  1793. std::cout << "Result:" << context->GetResult().DebugString() << std::endl;
  1794. AsyncResp resp = context->GetAsyncResp();
  1795. //std::cout << "Location:" << resp.GetLocation() << std::endl;
  1796. //std::cout << "Bucket:" << resp.GetBucket() << std::endl;
  1797. //std::cout << "Key:" << resp.GetKey() << std::endl;
  1798. std::cout << "ETag:" << resp.GetEtag() << std::endl;
  1799. std::cout << "Crc64:" << resp.GetXCosHashCrc64Ecma() << std::endl;
  1800. } else {
  1801. std::cout << "AsyncMultiPutObject failed" << std::endl;
  1802. std::cout << "ErrorMsg:" << context->GetResult().GetErrorMsg() << std::endl;
  1803. }
  1804. }
  1805. // 从目录上传文件
  1806. void PutObjectsFromDirectory(qcloud_cos::CosAPI& cos,
  1807. const std::string& bucket_name,
  1808. const std::string& directory_name) {
  1809. PutObjectsByDirectoryReq req(bucket_name, directory_name);
  1810. PutObjectsByDirectoryResp resp;
  1811. CosResult result = cos.PutObjects(req, &resp);
  1812. if (result.IsSucc()) {
  1813. std::cout << "PutObjectsFromDirectory Succ." << std::endl;
  1814. for (auto& r : resp.m_succ_put_objs) {
  1815. std::cout << "file_name: " << r.m_file_name
  1816. << ", object_name: " << r.m_object_name
  1817. << ", ETag: " << r.m_cos_resp.GetEtag() << std::endl;
  1818. }
  1819. } else {
  1820. std::cout << "PutObjectsFromDirectory Fail, ErrorMsg: "
  1821. << result.GetErrorMsg() << std::endl;
  1822. }
  1823. }
  1824. // 从目录上传文件到cos指定目录
  1825. void PutObjectsFromDirectoryToCosPath(qcloud_cos::CosAPI& cos,
  1826. const std::string& bucket_name,
  1827. const std::string& directory_name,
  1828. const std::string& cos_path) {
  1829. PutObjectsByDirectoryReq req(bucket_name, directory_name);
  1830. req.SetCosPath(cos_path);
  1831. PutObjectsByDirectoryResp resp;
  1832. CosResult result = cos.PutObjects(req, &resp);
  1833. if (result.IsSucc()) {
  1834. std::cout << "PutObjectsFromDirectoryToCosPath Succ." << std::endl;
  1835. for (auto& r : resp.m_succ_put_objs) {
  1836. std::cout << "file_name: " << r.m_file_name
  1837. << ", object_name: " << r.m_object_name
  1838. << ", ETag: " << r.m_cos_resp.GetEtag() << std::endl;
  1839. }
  1840. } else {
  1841. std::cout << "PutObjectsFromDirectoryToCosPath Fail, ErrorMsg: "
  1842. << result.GetErrorMsg() << std::endl;
  1843. }
  1844. }
  1845. // 创建目录
  1846. void PutDirectory(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  1847. const std::string& directory_name) {
  1848. PutDirectoryReq req(bucket_name, directory_name);
  1849. PutDirectoryResp resp;
  1850. CosResult result = cos.PutDirectory(req, &resp);
  1851. if (result.IsSucc()) {
  1852. std::cout << "PutDirectory Succ." << std::endl;
  1853. } else {
  1854. std::cout << "PutDirectory Fail, ErrorMsg: " << result.GetErrorMsg()
  1855. << std::endl;
  1856. }
  1857. }
  1858. // 删除目录
  1859. void DeleteDirectory(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  1860. const std::string& directory_name) {
  1861. DeleteObjectsByPrefixReq req(bucket_name, directory_name);
  1862. DeleteObjectsByPrefixResp resp;
  1863. CosResult result = cos.DeleteObjects(req, &resp);
  1864. if (result.IsSucc()) {
  1865. std::cout << "DeleteDirectory Succ." << std::endl;
  1866. } else {
  1867. std::cout << "DeleteDirectory Fail, ErrorMsg: " << result.GetErrorMsg()
  1868. << std::endl;
  1869. }
  1870. std::cout << "Succ del objs:" << std::endl;
  1871. for (auto& obj : resp.m_succ_del_objs) {
  1872. std::cout << obj << std::endl;
  1873. }
  1874. }
  1875. // 下载目录到本地
  1876. void DownloadFromDirectory(qcloud_cos::CosAPI& cos,
  1877. const std::string& bucket_name,
  1878. const std::string& server_dir_name,
  1879. const std::string& local_dir_name) {
  1880. qcloud_cos::GetBucketReq get_bucket_req(bucket_name);
  1881. get_bucket_req.SetPrefix(server_dir_name);
  1882. qcloud_cos::CosResult get_bucket_result;
  1883. bool is_truncated = false;
  1884. do {
  1885. qcloud_cos::GetBucketResp get_bucket_resp;
  1886. get_bucket_result = cos.GetBucket(get_bucket_req, &get_bucket_resp);
  1887. if (get_bucket_result.IsSucc()) {
  1888. std::vector<Content> contents = get_bucket_resp.GetContents();
  1889. for (auto& content : contents) {
  1890. std::cout << "====process " << content.m_key << std::endl;
  1891. std::string local_file_name = local_dir_name + content.m_key;
  1892. if (StringUtil::StringEndsWith(local_file_name, "/")) {
  1893. continue;
  1894. }
  1895. size_t found_dir = local_file_name.find_last_of("/");
  1896. if (found_dir) {
  1897. std::string dirname = local_file_name.substr(0, found_dir);
  1898. char* p_dirname = const_cast<char*>(dirname.c_str());
  1899. struct stat buffer;
  1900. if (stat(p_dirname, &buffer) != 0) {
  1901. std::cout << "====mkdir " << dirname << std::endl;
  1902. std::string mkdir_cmd = "mkdir -p " + dirname;
  1903. system(mkdir_cmd.c_str());
  1904. }
  1905. }
  1906. GetObjectByFileReq get_req(bucket_name, content.m_key, local_file_name);
  1907. GetObjectByFileResp get_resp;
  1908. CosResult get_result = cos.GetObject(get_req, &get_resp);
  1909. if (get_result.IsSucc()) {
  1910. std::cout << "====download " << content.m_key << " to "
  1911. << local_file_name << " succeed" << std::endl;
  1912. } else {
  1913. std::cout << "====download " << content.m_key << " to "
  1914. << local_file_name << " failed" << std::endl;
  1915. }
  1916. }
  1917. get_bucket_req.SetMarker(
  1918. get_bucket_resp.GetNextMarker()); // 设置下次列举的起始key
  1919. is_truncated = get_bucket_resp.IsTruncated();
  1920. }
  1921. } while (get_bucket_result.IsSucc() && is_truncated);
  1922. }
  1923. // 指定前缀删除
  1924. void DeleteObjectsByPrefix(qcloud_cos::CosAPI& cos,
  1925. const std::string& bucket_name,
  1926. const std::string& prefix) {
  1927. DeleteObjectsByPrefixReq req(bucket_name, prefix);
  1928. DeleteObjectsByPrefixResp resp;
  1929. CosResult result = cos.DeleteObjects(req, &resp);
  1930. if (result.IsSucc()) {
  1931. std::cout << "DeleteObjectsByPrefix Succ." << std::endl;
  1932. } else {
  1933. std::cout << "DeleteObjectsByPrefix Fail, ErrorMsg: "
  1934. << result.GetErrorMsg() << std::endl;
  1935. }
  1936. std::cout << "Succ del objs:" << std::endl;
  1937. for (auto& obj : resp.m_succ_del_objs) {
  1938. std::cout << obj << std::endl;
  1939. }
  1940. }
  1941. // 移动桶内对象
  1942. void MoveObject(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  1943. const std::string& src_object, const std::string& dst_object) {
  1944. MoveObjectReq req(bucket_name, src_object, dst_object);
  1945. CosResult result = cos.MoveObject(req);
  1946. if (result.IsSucc()) {
  1947. std::cout << "MoveObject Succ." << std::endl;
  1948. } else {
  1949. std::cout << "MoveObject Fail, ErrorMsg: " << result.GetErrorMsg()
  1950. << std::endl;
  1951. }
  1952. }
  1953. /*数据处理*/
  1954. // 图片缩放
  1955. void ImageThumbnail(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  1956. const std::string& object_name,
  1957. const std::string& local_file) {
  1958. GetObjectByFileReq req(bucket_name, object_name, local_file);
  1959. req.AddParam("imageMogr2/thumbnail/!50p", "");
  1960. GetObjectByFileResp resp;
  1961. CosResult result = cos.GetObject(req, &resp);
  1962. if (result.IsSucc()) {
  1963. std::cout << "ImageThumbnail Succ." << std::endl;
  1964. } else {
  1965. std::cout << "ImageThumbnail Fail, ErrorMsg: " << result.GetErrorMsg()
  1966. << std::endl;
  1967. }
  1968. }
  1969. // 上传时处理图片
  1970. void PutImage(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  1971. const std::string& object_name, const std::string& local_file) {
  1972. PutImageByFileReq req(bucket_name, object_name, local_file);
  1973. PutImageByFileResp resp;
  1974. PicOperation pic_operation;
  1975. PicRules rule;
  1976. // 锐化
  1977. rule.fileid = "/" + object_name + "_sharpen.jpg";
  1978. rule.rule = "imageMogr2/sharpen/70";
  1979. pic_operation.AddRule(rule);
  1980. // 旋转
  1981. rule.fileid = "/" + object_name + "_rotate.jpg";
  1982. rule.rule = "imageMogr2/rotate/90";
  1983. pic_operation.AddRule(rule);
  1984. req.SetPicOperation(pic_operation);
  1985. CosResult result = cos.PutImage(req, &resp);
  1986. if (result.IsSucc()) {
  1987. std::cout << "PutImage Succ." << std::endl;
  1988. std::cout << "ProcessResult: " << resp.GetUploadResult().to_string()
  1989. << std::endl;
  1990. } else {
  1991. std::cout << "PutImage Fail, ErrorMsg: " << result.GetErrorMsg()
  1992. << std::endl;
  1993. }
  1994. std::cout << "===================PutImage============================="
  1995. << std::endl;
  1996. PrintResult(result, resp);
  1997. std::cout << "========================================================"
  1998. << std::endl;
  1999. }
  2000. // 添加盲水印
  2001. void AddWatermark(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  2002. const std::string& object_name,
  2003. const std::string& local_file) {
  2004. PutImageByFileReq req(bucket_name, object_name, local_file);
  2005. PutImageByFileResp resp;
  2006. PicOperation pic_operation;
  2007. PicRules rule;
  2008. // 添加盲水印
  2009. rule.fileid = "/" + object_name + "_watermark.jpg";
  2010. // std::string image = "http://" + bucket_name +
  2011. // ".cos.ap-guangzhou.myqcloud.com/" + object_name + "_watermark.jpg";
  2012. rule.rule =
  2013. "watermark/3/type/3/text/" + CodecUtil::Base64Encode("test wartermart");
  2014. pic_operation.AddRule(rule);
  2015. req.SetPicOperation(pic_operation);
  2016. CosResult result = cos.PutImage(req, &resp);
  2017. if (result.IsSucc()) {
  2018. std::cout << "PutImage Succ." << std::endl;
  2019. std::cout << "ProcessResult: " << resp.GetUploadResult().to_string()
  2020. << std::endl;
  2021. } else {
  2022. std::cout << "PutImage Fail, ErrorMsg: " << result.GetErrorMsg()
  2023. << std::endl;
  2024. }
  2025. std::cout << "===================PutImage============================="
  2026. << std::endl;
  2027. PrintResult(result, resp);
  2028. std::cout << "========================================================"
  2029. << std::endl;
  2030. }
  2031. // 下载图片时添加盲水印
  2032. // https://cloud.tencent.com/document/product/436/46782
  2033. void GetImageWithWatermark(qcloud_cos::CosAPI& cos,
  2034. const std::string& bucket_name,
  2035. const std::string& object_name,
  2036. const std::string& local_file) {
  2037. GetObjectByFileReq req(bucket_name, object_name, local_file);
  2038. GetObjectByFileResp resp;
  2039. std::string watermark_text = "test"; // 盲水印文本
  2040. std::string watermark_param =
  2041. "watermark/3/type/3/text/" + CodecUtil::Base64Encode(watermark_text);
  2042. req.AddParam(watermark_param, "");
  2043. CosResult result = cos.GetObject(req, &resp);
  2044. if (result.IsSucc()) {
  2045. std::cout << "GetImageWithWatermark Succ." << std::endl;
  2046. } else {
  2047. std::cout << "GetImageWithWatermark Fail, ErrorMsg: "
  2048. << result.GetErrorMsg() << std::endl;
  2049. }
  2050. std::cout
  2051. << "===================GetImageWithWatermark============================="
  2052. << std::endl;
  2053. PrintResult(result, resp);
  2054. std::cout << "========================================================"
  2055. << std::endl;
  2056. }
  2057. // 提取盲水印
  2058. void ExtractWatermark(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  2059. const std::string& object_name,
  2060. const std::string& local_file) {
  2061. PutImageByFileReq req(bucket_name, object_name, local_file);
  2062. PutImageByFileResp resp;
  2063. PicOperation pic_operation;
  2064. PicRules rule;
  2065. // 提取盲水印
  2066. rule.fileid = "/" + object_name + "_watermark_extract.jpg";
  2067. rule.rule = "watermark/4/type/3";
  2068. pic_operation.AddRule(rule);
  2069. req.SetPicOperation(pic_operation);
  2070. CosResult result = cos.PutImage(req, &resp);
  2071. if (result.IsSucc()) {
  2072. std::cout << "PutImage Succ." << std::endl;
  2073. std::cout << "ProcessResult: " << resp.GetUploadResult().to_string()
  2074. << std::endl;
  2075. } else {
  2076. std::cout << "PutImage Fail, ErrorMsg: " << result.GetErrorMsg()
  2077. << std::endl;
  2078. }
  2079. std::cout << "===================PutImage============================="
  2080. << std::endl;
  2081. PrintResult(result, resp);
  2082. std::cout << "========================================================"
  2083. << std::endl;
  2084. }
  2085. // 云上处理图片
  2086. void CloudImageProcess(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  2087. const std::string& object_name) {
  2088. CloudImageProcessReq req(bucket_name, object_name);
  2089. CloudImageProcessResp resp;
  2090. PicOperation pic_operation;
  2091. PicRules rule;
  2092. rule.fileid = "/" + object_name + "_thumbnail.jpg";
  2093. rule.rule = "imageMogr2/thumbnail/!30p";
  2094. pic_operation.AddRule(rule);
  2095. rule.fileid = "/" + object_name + "_cut.jpg";
  2096. rule.rule = "imageMogr2/cut/300x300";
  2097. pic_operation.AddRule(rule);
  2098. req.SetPicOperation(pic_operation);
  2099. CosResult result = cos.CloudImageProcess(req, &resp);
  2100. if (result.IsSucc()) {
  2101. std::cout << "CloudImageProcess Succ." << std::endl;
  2102. std::cout << "ProcessResult: " << resp.GetUploadResult().to_string()
  2103. << std::endl;
  2104. } else {
  2105. std::cout << "CloudImageProcess Fail, ErrorMsg: " << result.GetErrorMsg()
  2106. << std::endl;
  2107. }
  2108. std::cout
  2109. << "===================CloudImageProcess============================="
  2110. << std::endl;
  2111. PrintResult(result, resp);
  2112. std::cout << "========================================================"
  2113. << std::endl;
  2114. }
  2115. // 上传图片识别二维码
  2116. void PutQRcode(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  2117. const std::string& object_name, const std::string& local_file) {
  2118. PutImageByFileReq req(bucket_name, object_name, local_file);
  2119. PutImageByFileResp resp;
  2120. PicOperation pic_operation;
  2121. PicRules rule1;
  2122. rule1.fileid = "/" + object_name + "_put_qrcode.jpg";
  2123. rule1.rule = "QRcode/cover/1";
  2124. pic_operation.AddRule(rule1);
  2125. req.SetPicOperation(pic_operation);
  2126. CosResult result = cos.PutImage(req, &resp);
  2127. if (result.IsSucc()) {
  2128. std::cout << "PutQRcode Succ." << std::endl;
  2129. std::cout << "ProcessResult: " << resp.GetUploadResult().to_string()
  2130. << std::endl;
  2131. } else {
  2132. std::cout << "PutQRcode Fail, ErrorMsg: " << result.GetErrorMsg()
  2133. << std::endl;
  2134. }
  2135. std::cout << "===================PutQRcode============================="
  2136. << std::endl;
  2137. PrintResult(result, resp);
  2138. std::cout << "========================================================"
  2139. << std::endl;
  2140. }
  2141. // 下载图片时识别二维码
  2142. void GetQRcode(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  2143. const std::string& object_name) {
  2144. GetQRcodeReq req(bucket_name, object_name);
  2145. GetQRcodeResp resp;
  2146. CosResult result = cos.GetQRcode(req, &resp);
  2147. if (result.IsSucc()) {
  2148. std::cout << "GetQRcode Succ." << std::endl;
  2149. std::cout << "Result: " << resp.GetResult().to_string() << std::endl;
  2150. } else {
  2151. std::cout << "GetQRcode Fail, ErrorMsg: " << result.GetErrorMsg()
  2152. << std::endl;
  2153. }
  2154. std::cout << "===================GetQRcode============================="
  2155. << std::endl;
  2156. PrintResult(result, resp);
  2157. std::cout << "========================================================"
  2158. << std::endl;
  2159. }
  2160. // 查询文档预览开通状态
  2161. // https://cloud.tencent.com/document/product/436/54057
  2162. void DescribeDocProcessBuckets(qcloud_cos::CosAPI& cos) {
  2163. DescribeDocProcessBucketsReq req;
  2164. DescribeDocProcessBucketsResp resp;
  2165. req.SetRegions("ap-guangzhou");
  2166. // 设置存储桶名称,以“,”分隔,支持多个存储桶,精确搜索
  2167. // req.SetBucketNames("xxx");
  2168. // 设置存储桶名称前缀,前缀搜索
  2169. // req.SetBucketName("xxx");
  2170. // 设置第几页
  2171. // req.SetPageNumber();
  2172. // 设置每页个数
  2173. // req.SetPageSize();
  2174. CosResult result = cos.DescribeDocProcessBuckets(req, &resp);
  2175. if (result.IsSucc()) {
  2176. std::cout << "DescribeDocProcessBuckets Succ." << std::endl;
  2177. std::cout << "Result: " << resp.GetResult().to_string() << std::endl;
  2178. } else {
  2179. std::cout << "DescribeDocProcessBuckets Fail, ErrorMsg: "
  2180. << result.GetErrorMsg() << std::endl;
  2181. }
  2182. std::cout << "===================DescribeDocProcessBuckets==================="
  2183. "=========="
  2184. << std::endl;
  2185. PrintResult(result, resp);
  2186. std::cout << "========================================================"
  2187. << std::endl;
  2188. }
  2189. // 文档预览
  2190. // https://cloud.tencent.com/document/product/436/54058
  2191. void DocPreview(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  2192. const std::string& object_name, const std::string& local_file) {
  2193. DocPreviewReq req(bucket_name, object_name, local_file);
  2194. req.SetSrcType("docx");
  2195. DocPreviewResp resp;
  2196. CosResult result = cos.DocPreview(req, &resp);
  2197. if (result.IsSucc()) {
  2198. std::cout << "DocPreview Succ." << std::endl;
  2199. } else {
  2200. std::cout << "DocPreview Fail, ErrorMsg: " << result.GetErrorMsg()
  2201. << std::endl;
  2202. }
  2203. std::cout << "===================DocPreview============================="
  2204. << std::endl;
  2205. PrintResult(result, resp);
  2206. std::cout << "========================================================"
  2207. << std::endl;
  2208. }
  2209. // 提交一个文档预览任务
  2210. void CreateDocProcessJobs(qcloud_cos::CosAPI& cos,
  2211. const std::string& bucket_name) {
  2212. CreateDocProcessJobsReq req(bucket_name);
  2213. CreateDocProcessJobsResp resp;
  2214. req.SetVerifyCert(false);
  2215. Input input;
  2216. input.object = "test.docx";
  2217. req.SetInput(input);
  2218. Operation operation;
  2219. Output output;
  2220. output.bucket = "wqingzhang";
  2221. output.region = "ap-chongqing";
  2222. output.object = "/test-ci/test-create-doc-process-${Number}";
  2223. operation.output = output;
  2224. req.SetOperation(operation);
  2225. // req.SetQueueId("xxx");
  2226. CosResult result = cos.CreateDocProcessJobs(req, &resp);
  2227. if (result.IsSucc()) {
  2228. std::cout << "CreateDocProcessJobs Succ." << std::endl;
  2229. std::cout << "jobs_detail: " << resp.GetJobsDetail().to_string()
  2230. << std::endl;
  2231. } else {
  2232. std::cout << "CreateDocProcessJobs Fail, ErrorMsg: " << result.GetErrorMsg()
  2233. << std::endl;
  2234. }
  2235. std::cout
  2236. << "===================CreateDocProcessJobs============================="
  2237. << std::endl;
  2238. PrintResult(result, resp);
  2239. std::cout << "========================================================"
  2240. << std::endl;
  2241. }
  2242. // 查询文档预览任务
  2243. void DescribeDocProcessJob(qcloud_cos::CosAPI& cos,
  2244. const std::string& bucket_name) {
  2245. DescribeDocProcessJobReq req(bucket_name);
  2246. DescribeDocProcessJobResp resp;
  2247. req.SetJobId("xxx");
  2248. CosResult result = cos.DescribeDocProcessJob(req, &resp);
  2249. if (result.IsSucc()) {
  2250. std::cout << "DescribeDocProcessJobs Succ." << std::endl;
  2251. std::cout << "jobs_detail: " << resp.GetJobsDetail().to_string()
  2252. << std::endl;
  2253. } else {
  2254. std::cout << "DescribeDocProcessJobs Fail, ErrorMsg: "
  2255. << result.GetErrorMsg() << std::endl;
  2256. }
  2257. std::cout << "===================DescribeDocProcessJobs======================"
  2258. "======="
  2259. << std::endl;
  2260. PrintResult(result, resp);
  2261. std::cout << "========================================================"
  2262. << std::endl;
  2263. }
  2264. // 拉取符合条件的文档预览任务
  2265. void DescribeDocProcessJobs(qcloud_cos::CosAPI& cos,
  2266. const std::string& bucket_name) {
  2267. DescribeDocProcessJobsReq req(bucket_name);
  2268. DescribeDocProcessJobsResp resp;
  2269. req.SetQueueId("xxx");
  2270. CosResult result = cos.DescribeDocProcessJobs(req, &resp);
  2271. if (result.IsSucc()) {
  2272. std::cout << "DescribeDocProcessJobs Succ." << std::endl;
  2273. std::vector<JobsDetail> jobs_detail = resp.GetJobsDetails();
  2274. for (auto& job : jobs_detail) {
  2275. std::cout << "job: " << job.to_string() << std::endl;
  2276. }
  2277. std::cout << "NextToken: " << resp.GetNextToken() << std::endl;
  2278. } else {
  2279. std::cout << "DescribeDocProcessJobs Fail, ErrorMsg: "
  2280. << result.GetErrorMsg() << std::endl;
  2281. }
  2282. std::cout << "===================DescribeDocProcessJobs======================"
  2283. "======="
  2284. << std::endl;
  2285. // PrintResult(result, resp);
  2286. std::cout << "========================================================"
  2287. << std::endl;
  2288. }
  2289. // 查询文档预览队列
  2290. void DescribeDocProcessQueues(qcloud_cos::CosAPI& cos,
  2291. const std::string& bucket_name) {
  2292. DescribeDocProcessQueuesReq req(bucket_name);
  2293. DescribeDocProcessQueuesResp resp;
  2294. CosResult result = cos.DescribeDocProcessQueues(req, &resp);
  2295. if (result.IsSucc()) {
  2296. std::cout << "DescribeDocProcessQueues Succ." << std::endl;
  2297. std::cout << "ReqeustId: " << resp.GetRequestId()
  2298. << ", PageNumber: " << resp.GetPageNumber()
  2299. << ", PageSize: " << resp.GetPageSize()
  2300. << ", QueueList: " << resp.GetQueueList().to_string()
  2301. << ", NonExistPIDs: " << resp.GetNonExistPIDs().to_string()
  2302. << std::endl;
  2303. } else {
  2304. std::cout << "DescribeDocProcessQueues Fail, ErrorMsg: "
  2305. << result.GetErrorMsg() << std::endl;
  2306. }
  2307. std::cout << "===================DescribeDocProcessQueues===================="
  2308. "========="
  2309. << std::endl;
  2310. PrintResult(result, resp);
  2311. std::cout << "========================================================"
  2312. << std::endl;
  2313. }
  2314. // 更新文档预览队列
  2315. void UpdateDocProcessQueue(qcloud_cos::CosAPI& cos,
  2316. const std::string& bucket_name) {
  2317. UpdateDocProcessQueueReq req(bucket_name);
  2318. UpdateDocProcessQueueResp resp;
  2319. req.SetName("queue-doc-process-1");
  2320. req.SetQueueId("xxx");
  2321. req.SetState("Active");
  2322. NotifyConfig notify_config;
  2323. notify_config.url = "http://example.com";
  2324. notify_config.state = "On";
  2325. notify_config.type = "Url";
  2326. notify_config.event = "TransCodingFinish";
  2327. req.SetNotifyConfig(notify_config);
  2328. CosResult result = cos.UpdateDocProcessQueue(req, &resp);
  2329. if (result.IsSucc()) {
  2330. std::cout << "UpdateDocProcessQueue Succ." << std::endl;
  2331. std::cout << "ReqeustId: " << resp.GetRequestId()
  2332. << ", QueueList: " << resp.GetQueueList().to_string()
  2333. << std::endl;
  2334. } else {
  2335. std::cout << "UpdateDocProcessQueue Fail, ErrorMsg: "
  2336. << result.GetErrorMsg() << std::endl;
  2337. }
  2338. std::cout
  2339. << "===================UpdateDocProcessQueue============================="
  2340. << std::endl;
  2341. PrintResult(result, resp);
  2342. std::cout << "========================================================"
  2343. << std::endl;
  2344. }
  2345. // 获取对象不带签名的URL
  2346. void GetObjectUrl(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  2347. const std::string& object_name) {
  2348. std::cout << "https url: " << cos.GetObjectUrl(bucket_name, object_name)
  2349. << std::endl;
  2350. std::cout << "http url: " << cos.GetObjectUrl(bucket_name, object_name, false)
  2351. << std::endl;
  2352. std::cout << "ap-beijing https url: "
  2353. << cos.GetObjectUrl(bucket_name, object_name, true, "ap-beijing")
  2354. << std::endl;
  2355. std::cout << "ap-beijing http url: "
  2356. << cos.GetObjectUrl(bucket_name, object_name, false, "ap-beijing")
  2357. << std::endl;
  2358. }
  2359. // 查询已经开通媒体处理功能的存储桶
  2360. // https://cloud.tencent.com/document/product/436/48988
  2361. void DescribeMediaBuckets(qcloud_cos::CosAPI& cos) {
  2362. DescribeMediaBucketsReq req;
  2363. DescribeMediaBucketsResp resp;
  2364. req.SetRegions("ap-guangzhou");
  2365. // 设置存储桶名称,以“,”分隔,支持多个存储桶,精确搜索
  2366. // req.SetBucketNames("xxx");
  2367. // 设置存储桶名称前缀,前缀搜索
  2368. // req.SetBucketName("xxx");
  2369. // 设置第几页
  2370. // req.SetPageNumber();
  2371. // 设置每页个数
  2372. // req.SetPageSize();
  2373. CosResult result = cos.DescribeMediaBuckets(req, &resp);
  2374. if (result.IsSucc()) {
  2375. std::cout << "DescribeMediaBuckets Succ." << std::endl;
  2376. std::cout << "Result: " << resp.GetResult().to_string() << std::endl;
  2377. } else {
  2378. std::cout << "DescribeMediaBuckets Fail, ErrorMsg: " << result.GetErrorMsg()
  2379. << std::endl;
  2380. }
  2381. std::cout << "===================DescribeMediaBuckets==================="
  2382. "=========="
  2383. << std::endl;
  2384. PrintResult(result, resp);
  2385. std::cout << "========================================================"
  2386. << std::endl;
  2387. }
  2388. // 获取媒体文件某个时间的截图
  2389. // https://cloud.tencent.com/document/product/436/55671
  2390. void GetSnapshot(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  2391. const std::string& object_name,
  2392. const std::string& local_file) {
  2393. GetSnapshotReq req(bucket_name, object_name, local_file);
  2394. GetSnapshotResp resp;
  2395. req.SetTime(100);
  2396. // req.SetWitdh();
  2397. // req.SetHeight();
  2398. // req.SetFormat();
  2399. CosResult result = cos.GetSnapshot(req, &resp);
  2400. if (result.IsSucc()) {
  2401. std::cout << "GetSnapshot Succ." << std::endl;
  2402. } else {
  2403. std::cout << "GetSnapshot Fail, ErrorMsg: " << result.GetErrorMsg()
  2404. << std::endl;
  2405. }
  2406. std::cout << "===================GetSnapshot==================="
  2407. "=========="
  2408. << std::endl;
  2409. PrintResult(result, resp);
  2410. std::cout << "========================================================"
  2411. << std::endl;
  2412. }
  2413. // 获取媒体信息
  2414. // https://cloud.tencent.com/document/product/436/55672
  2415. void GetMediaInfo(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  2416. const std::string& object_name) {
  2417. GetMediaInfoReq req(bucket_name, object_name);
  2418. GetMediaInfoResp resp;
  2419. CosResult result = cos.GetMediaInfo(req, &resp);
  2420. if (result.IsSucc()) {
  2421. std::cout << "GetMediaInfo Succ." << std::endl;
  2422. std::cout << "Result: " << resp.GetResult().to_string() << std::endl;
  2423. } else {
  2424. std::cout << "GetMediaInfo Fail, ErrorMsg: " << result.GetErrorMsg()
  2425. << std::endl;
  2426. }
  2427. std::cout << "===================GetMediaInfo==================="
  2428. "=========="
  2429. << std::endl;
  2430. PrintResult(result, resp);
  2431. std::cout << "========================================================"
  2432. << std::endl;
  2433. }
  2434. // 获取私有 M3U8 ts 资源的下载授权
  2435. // https://cloud.tencent.com/document/product/436/63740
  2436. void GetPm3u8(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  2437. const std::string& object_name,
  2438. const std::string& local_file) {
  2439. GetPm3u8Req req(bucket_name, object_name, local_file);
  2440. GetPm3u8Resp resp;
  2441. req.SetExpires(3600);
  2442. CosResult result = cos.GetPm3u8(req, &resp);
  2443. if (result.IsSucc()) {
  2444. std::cout << "GetPm3u8 Succ." << std::endl;
  2445. } else {
  2446. std::cout << "GetPm3u8 Fail, ErrorMsg: " << result.GetErrorMsg()
  2447. << std::endl;
  2448. }
  2449. std::cout << "===================GetPm3u8==================="
  2450. "=========="
  2451. << std::endl;
  2452. PrintResult(result, resp);
  2453. std::cout << "========================================================"
  2454. << std::endl;
  2455. }
  2456. // 查询媒体处理队列
  2457. void DescribeMediaQueues(qcloud_cos::CosAPI& cos,
  2458. const std::string& bucket_name) {
  2459. DescribeMediaQueuesReq req(bucket_name);
  2460. DescribeQueuesResp resp;
  2461. CosResult result = cos.DescribeMediaQueues(req, &resp);
  2462. if (result.IsSucc()) {
  2463. std::cout << "DescribeMediaQueues Succ." << std::endl;
  2464. std::cout << "ReqeustId: " << resp.GetRequestId()
  2465. << ", PageNumber: " << resp.GetPageNumber()
  2466. << ", PageSize: " << resp.GetPageSize()
  2467. << ", QueueList: " << resp.GetQueueList().to_string()
  2468. << ", NonExistPIDs: " << resp.GetNonExistPIDs().to_string()
  2469. << std::endl;
  2470. } else {
  2471. std::cout << "DescribeMediaQueues Fail, ErrorMsg: "
  2472. << result.GetErrorMsg() << std::endl;
  2473. }
  2474. std::cout << "===================DescribeMediaQueues===================="
  2475. "========="
  2476. << std::endl;
  2477. PrintResult(result, resp);
  2478. std::cout << "========================================================"
  2479. << std::endl;
  2480. }
  2481. // 更新媒体处理队列
  2482. void UpdateMediaQueue(qcloud_cos::CosAPI& cos,
  2483. const std::string& bucket_name) {
  2484. UpdateMediaQueueReq req(bucket_name);
  2485. UpdateQueueResp resp;
  2486. req.SetName("queue-media-process-1");
  2487. req.SetQueueId("xxx");
  2488. req.SetState("Active");
  2489. NotifyConfig notify_config;
  2490. notify_config.url = "http://example.com";
  2491. notify_config.state = "On";
  2492. notify_config.type = "Url";
  2493. notify_config.event = "TransCodingFinish";
  2494. req.SetNotifyConfig(notify_config);
  2495. CosResult result = cos.UpdateMediaQueue(req, &resp);
  2496. if (result.IsSucc()) {
  2497. std::cout << "UpdateMediaQueue Succ." << std::endl;
  2498. std::cout << "ReqeustId: " << resp.GetRequestId()
  2499. << ", QueueList: " << resp.GetQueueList().to_string()
  2500. << std::endl;
  2501. } else {
  2502. std::cout << "UpdateMediaQueue Fail, ErrorMsg: "
  2503. << result.GetErrorMsg() << std::endl;
  2504. }
  2505. std::cout
  2506. << "===================UpdateMediaQueue============================="
  2507. << std::endl;
  2508. PrintResult(result, resp);
  2509. std::cout << "========================================================"
  2510. << std::endl;
  2511. }
  2512. // 创建文件解压缩任务
  2513. void CreateFileUncompressJobs(qcloud_cos::CosAPI& cos,
  2514. const std::string& bucket_name) {
  2515. CreateDataProcessJobsReq req(bucket_name);
  2516. CreateDataProcessJobsResp resp;
  2517. JobsOptions opt;
  2518. opt.input.bucket = bucket_name;
  2519. opt.input.region = "ap-chongqing";
  2520. opt.input.object = "test.zip";
  2521. opt.tag = "FileUncompress";
  2522. // 文件解压参数
  2523. // 指定解压后输出文件的前缀,不填则默认保存在存储桶根路径,非必选
  2524. opt.operation.file_uncompress_config.prefix = "output/";
  2525. // 指定解压后的文件路径是否需要替换前缀,可选值如下
  2526. // 0:不添加额外的前缀,解压缩将保存在Prefix指定的路径下(不会保留压缩包的名称,仅将压缩包内的文件保存至指定的路径)
  2527. // 1:以压缩包本身的名称作为前缀,解压缩将保存在Prefix指定的路径下
  2528. // 2:以压缩包完整路径作为前缀,此时如果不指定Prefix,就是解压到压缩包所在的当前路径(包含压缩包本身名称)
  2529. // 非必选参数,默认 0
  2530. opt.operation.file_uncompress_config.prefix_replaced = "1";
  2531. // 解压密钥,传入时需先经过 base64 编码,非必选
  2532. // opt.operation.file_uncompress_config.un_compress_key = "MTIzNDU2Nzg5MA==";
  2533. opt.operation.output.bucket = bucket_name;
  2534. opt.operation.output.region = "ap-chongqing";
  2535. req.setOperation(opt);
  2536. CosResult result = cos.CreateDataProcessJobs(req, &resp);
  2537. if (result.IsSucc()) {
  2538. std::cout << "CreateFileProcessJobs Succ." << std::endl;
  2539. } else {
  2540. std::cout << "CreateFileProcessJobs Fail, ErrorMsg: "
  2541. << result.GetErrorMsg() << std::endl;
  2542. }
  2543. std::cout
  2544. << "===================CreateFileProcessJobs============================="
  2545. << std::endl;
  2546. PrintResult(result, resp);
  2547. std::cout << "========================================================"
  2548. << std::endl;
  2549. }
  2550. // 查询异步任务
  2551. void DescribeDataProcessJobs(qcloud_cos::CosAPI& cos,
  2552. const std::string& bucket_name) {
  2553. DescribeDataProcessJobReq req(bucket_name);
  2554. DescribeDataProcessJobResp resp;
  2555. // 任务ID
  2556. req.SetJobId("jf5b1f35c4e2411ee9c3ca7d7d6d3bc6f");
  2557. CosResult result = cos.DescribeDataProcessJob(req, &resp);
  2558. if (result.IsSucc()) {
  2559. std::cout << "DescribeDataProcessJob Succ." << std::endl;
  2560. } else {
  2561. std::cout << "DescribeDataProcessJob Fail, ErrorMsg: "
  2562. << result.GetErrorMsg() << std::endl;
  2563. }
  2564. std::cout
  2565. << "===================DescribeDataProcessJob============================="
  2566. << std::endl;
  2567. PrintResult(result, resp);
  2568. std::cout << "========================================================"
  2569. << std::endl;
  2570. }
  2571. // 取消异步任务
  2572. void CancelDataProcessJob(qcloud_cos::CosAPI& cos,
  2573. const std::string& bucket_name) {
  2574. CancelDataProcessJobReq req(bucket_name);
  2575. CancelDataProcessJobResp resp;
  2576. // 任务ID
  2577. req.SetJobId("XXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
  2578. CosResult result = cos.CancelDataProcessJob(req, &resp);
  2579. if (result.IsSucc()) {
  2580. std::cout << "CancelDataProcessJob Succ." << std::endl;
  2581. } else {
  2582. std::cout << "CancelDataProcessJob Fail, ErrorMsg: "
  2583. << result.GetErrorMsg() << std::endl;
  2584. }
  2585. std::cout
  2586. << "===================CancelDataProcessJob============================="
  2587. << std::endl;
  2588. PrintResult(result, resp);
  2589. std::cout << "========================================================"
  2590. << std::endl;
  2591. }
  2592. // 创建视频截图任务
  2593. void CreateSnapshotJobs(qcloud_cos::CosAPI& cos,
  2594. const std::string& bucket_name) {
  2595. CreateDataProcessJobsReq req(bucket_name);
  2596. CreateDataProcessJobsResp resp;
  2597. JobsOptions opt;
  2598. opt.input.bucket = bucket_name;
  2599. opt.input.region = "ap-chongqing";
  2600. opt.input.object = "test.mp4";
  2601. opt.tag = "Snapshot";
  2602. // opt.operation.template_id = "XXXXXXXXXXXXXXXXXXX";
  2603. opt.operation.snapshot.mode = "Interval";
  2604. opt.operation.snapshot.start = "0";
  2605. opt.operation.snapshot.time_interval = "5";
  2606. opt.operation.snapshot.count = "3";
  2607. opt.operation.output.bucket = bucket_name;
  2608. opt.operation.output.region = "ap-chongqing";
  2609. opt.operation.output.object = "snapshot/test-${number}.jpg";
  2610. req.setOperation(opt);
  2611. CosResult result = cos.CreateDataProcessJobs(req, &resp);
  2612. if (result.IsSucc()) {
  2613. std::cout << "CreateDataProcessJobs Succ." << std::endl;
  2614. } else {
  2615. std::cout << "CreateDataProcessJobs Fail, ErrorMsg: "
  2616. << result.GetErrorMsg() << std::endl;
  2617. }
  2618. std::cout
  2619. << "===================CreateDataProcessJobs============================="
  2620. << std::endl;
  2621. PrintResult(result, resp);
  2622. std::cout << "========================================================"
  2623. << std::endl;
  2624. }
  2625. // 创建视频转码任务
  2626. void CreateTranscodeJobs(qcloud_cos::CosAPI& cos,
  2627. const std::string& bucket_name) {
  2628. CreateDataProcessJobsReq req(bucket_name);
  2629. CreateDataProcessJobsResp resp;
  2630. JobsOptions opt;
  2631. opt.input.bucket = bucket_name;
  2632. opt.input.region = "ap-chongqing";
  2633. opt.input.object = "test.mp4";
  2634. opt.tag = "Transcode";
  2635. // 使用转码模版ID提交任务
  2636. // opt.operation.template_id = "t1460606b9752148c4ab182f55163ba7cd";
  2637. // 使用转码参数提交任务
  2638. opt.operation.transcode.container.format = "mp4";
  2639. opt.operation.transcode.video.codec = "H.264";
  2640. opt.operation.transcode.video.profile = "high";
  2641. opt.operation.transcode.video.bit_rate = "1000";
  2642. opt.operation.transcode.video.width = "1280";
  2643. opt.operation.transcode.video.fps = "30";
  2644. opt.operation.transcode.video.preset = "medium";
  2645. opt.operation.transcode.audio.codec = "aac";
  2646. opt.operation.transcode.audio.sample_format = "fltp";
  2647. opt.operation.transcode.audio.bit_rate = "128";
  2648. opt.operation.transcode.audio.channels = "4";
  2649. opt.operation.transcode.trans_config.adj_dar_method = "scale";
  2650. opt.operation.transcode.trans_config.is_check_audio_bit_rate = "false";
  2651. opt.operation.transcode.trans_config.reso_adj_method = "1";
  2652. opt.operation.transcode.time_interval.start = "0";
  2653. opt.operation.transcode.time_interval.duration = "10";
  2654. // 混音参数
  2655. AudioMix audio_mix_1 = AudioMix();
  2656. audio_mix_1.audio_source = "https://test-12345.cos.ap-chongqing.myqcloud.com/test.wav";
  2657. audio_mix_1.mix_mode = "Once";
  2658. audio_mix_1.replace = "true";
  2659. audio_mix_1.effect_config.enable_start_fade_in = "true";
  2660. audio_mix_1.effect_config.start_fade_in_time = "3";
  2661. audio_mix_1.effect_config.enable_end_fade_out = "false";
  2662. audio_mix_1.effect_config.end_fade_out_time = "0";
  2663. audio_mix_1.effect_config.enable_bgm_fade = "true";
  2664. audio_mix_1.effect_config.bgm_fade_time = "1.7";
  2665. opt.operation.transcode.audio_mix_array.push_back(audio_mix_1);
  2666. AudioMix audio_mix_2 = AudioMix();
  2667. audio_mix_2.audio_source = "https://test-12345.cos.ap-chongqing.myqcloud.com/test.wav";
  2668. audio_mix_2.mix_mode = "Once";
  2669. audio_mix_2.replace = "true";
  2670. audio_mix_2.effect_config.enable_start_fade_in = "true";
  2671. audio_mix_2.effect_config.start_fade_in_time = "3";
  2672. audio_mix_2.effect_config.enable_end_fade_out = "false";
  2673. audio_mix_2.effect_config.end_fade_out_time = "0";
  2674. audio_mix_2.effect_config.enable_bgm_fade = "true";
  2675. audio_mix_2.effect_config.bgm_fade_time = "1.7";
  2676. opt.operation.transcode.audio_mix_array.push_back(audio_mix_2);
  2677. // 去除水印参数
  2678. opt.operation.remove_watermark.dx = "150";
  2679. opt.operation.remove_watermark.dy = "150";
  2680. opt.operation.remove_watermark.width = "75";
  2681. opt.operation.remove_watermark.height = "75";
  2682. // 数字水印参数
  2683. opt.operation.digital_watermark.type = "Text";
  2684. opt.operation.digital_watermark.message = "12345";
  2685. opt.operation.digital_watermark.version = "V1";
  2686. opt.operation.digital_watermark.ignore_error = "false";
  2687. // 水印参数
  2688. // 使用水印模版ID
  2689. // opt.operation.watermark_template_id.push_back("t1318c5f428d474afba1797f84091cbe22");
  2690. // opt.operation.watermark_template_id.push_back("t1318c5f428d474afba1797f84091cbe23");
  2691. // opt.operation.watermark_template_id.push_back("t1318c5f428d474afba1797f84091cbe24");
  2692. // 使用水印参数
  2693. Watermark watermark_1 = Watermark();
  2694. watermark_1.type = "Text";
  2695. watermark_1.loc_mode = "Absolute";
  2696. watermark_1.dx = "128";
  2697. watermark_1.dy = "128";
  2698. watermark_1.pos = "TopRight";
  2699. watermark_1.start_time = "0";
  2700. watermark_1.end_time = "100.5";
  2701. watermark_1.text.text = "水印内容";
  2702. watermark_1.text.font_size = "30";
  2703. watermark_1.text.font_color = "0x0B172F";
  2704. watermark_1.text.font_type = "simfang.ttf";
  2705. watermark_1.text.transparency = "30";
  2706. opt.operation.watermarks.push_back(watermark_1);
  2707. Watermark watermark_2 = Watermark();
  2708. watermark_2.type = "Image";
  2709. watermark_2.loc_mode = "Absolute";
  2710. watermark_2.dx = "128";
  2711. watermark_2.dy = "128";
  2712. watermark_2.pos = "TopRight";
  2713. watermark_2.start_time = "0";
  2714. watermark_2.end_time = "100.5";
  2715. watermark_2.image.url = "http://test-1234567890.ci.ap-chongqing.myqcloud.com/shuiyin_2.png";
  2716. watermark_2.image.mode = "Proportion";
  2717. watermark_2.image.width = "10";
  2718. watermark_2.image.height = "10";
  2719. watermark_2.image.transparency = "30";
  2720. opt.operation.watermarks.push_back(watermark_2);
  2721. opt.operation.output.bucket = bucket_name;
  2722. opt.operation.output.region = "ap-chongqing";
  2723. opt.operation.output.object = "output/test.mp4";
  2724. req.setOperation(opt);
  2725. CosResult result = cos.CreateDataProcessJobs(req, &resp);
  2726. if (result.IsSucc()) {
  2727. std::cout << "CreateDataProcessJobs Succ." << std::endl;
  2728. } else {
  2729. std::cout << "CreateDataProcessJobs Fail, ErrorMsg: "
  2730. << result.GetErrorMsg() << std::endl;
  2731. }
  2732. std::cout
  2733. << "===================CreateDataProcessJobs============================="
  2734. << std::endl;
  2735. PrintResult(result, resp);
  2736. std::cout << "========================================================"
  2737. << std::endl;
  2738. }
  2739. // 创建视频转动图任务
  2740. void CreateAnimationJobs(qcloud_cos::CosAPI& cos,
  2741. const std::string& bucket_name) {
  2742. CreateDataProcessJobsReq req(bucket_name);
  2743. CreateDataProcessJobsResp resp;
  2744. JobsOptions opt;
  2745. opt.input.bucket = bucket_name;
  2746. opt.input.region = "ap-chongqing";
  2747. opt.input.object = "test.mp4";
  2748. opt.tag = "Animation";
  2749. // 使用模版ID提交任务
  2750. // opt.operation.template_id = "XXXXXXXXXXXXXXXXXXX";
  2751. // 使用参数形式提交任务
  2752. opt.operation.animation.container.format = "gif";
  2753. opt.operation.animation.video.codec = "gif";
  2754. opt.operation.animation.video.width = "1280";
  2755. opt.operation.animation.video.height = "960";
  2756. opt.operation.animation.video.fps = "15";
  2757. opt.operation.animation.video.animate_only_keep_key_frame = "true";
  2758. opt.operation.animation.time_interval.start = "0";
  2759. opt.operation.animation.time_interval.duration = "60";
  2760. opt.operation.output.bucket = bucket_name;
  2761. opt.operation.output.region = "ap-chongqing";
  2762. opt.operation.output.object = "output/out.${ext}";
  2763. req.setOperation(opt);
  2764. CosResult result = cos.CreateDataProcessJobs(req, &resp);
  2765. if (result.IsSucc()) {
  2766. std::cout << "CreateDataProcessJobs Succ." << std::endl;
  2767. } else {
  2768. std::cout << "CreateDataProcessJobs Fail, ErrorMsg: "
  2769. << result.GetErrorMsg() << std::endl;
  2770. }
  2771. std::cout
  2772. << "===================CreateDataProcessJobs============================="
  2773. << std::endl;
  2774. PrintResult(result, resp);
  2775. std::cout << "========================================================"
  2776. << std::endl;
  2777. }
  2778. // 创建视频拼接任务
  2779. void CreateConcatJobs(qcloud_cos::CosAPI& cos,
  2780. const std::string& bucket_name) {
  2781. CreateDataProcessJobsReq req(bucket_name);
  2782. CreateDataProcessJobsResp resp;
  2783. JobsOptions opt;
  2784. opt.input.bucket = bucket_name;
  2785. opt.input.region = "ap-chongqing";
  2786. opt.input.object = "test.mp4";
  2787. opt.tag = "Concat";
  2788. // 使用模版ID提交任务
  2789. // opt.operation.template_id = "XXXXXXXXXXXXXXXXXXX";
  2790. // 使用参数形式提交任务
  2791. ConcatFragment fragment1 = ConcatFragment();
  2792. fragment1.url = "http://test-12345.cos.ap-chongqing.myqcloud.com/start.mp4";
  2793. opt.operation.concat.concat_fragment.push_back(fragment1);
  2794. ConcatFragment fragment2 = ConcatFragment();
  2795. fragment2.url = "http://test-12345.cos.ap-chongqing.myqcloud.com/end.mp4";
  2796. opt.operation.concat.concat_fragment.push_back(fragment2);
  2797. opt.operation.concat.audio.codec = "mp3";
  2798. opt.operation.concat.video.codec = "H.264";
  2799. opt.operation.concat.video.bit_rate = "1000";
  2800. opt.operation.concat.video.width = "1280";
  2801. opt.operation.concat.video.height = "720";
  2802. opt.operation.concat.video.fps = "30";
  2803. opt.operation.concat.container.format = "mp4";
  2804. // 混音参数
  2805. AudioMix audio_mix_1 = AudioMix();
  2806. audio_mix_1.audio_source = "https://test-xxx.cos.ap-chongqing.myqcloud.com/mix1.mp3";
  2807. audio_mix_1.mix_mode = "Once";
  2808. audio_mix_1.replace = "true";
  2809. audio_mix_1.effect_config.enable_start_fade_in = "true";
  2810. audio_mix_1.effect_config.start_fade_in_time = "3";
  2811. audio_mix_1.effect_config.enable_end_fade_out = "false";
  2812. audio_mix_1.effect_config.end_fade_out_time = "0";
  2813. audio_mix_1.effect_config.enable_bgm_fade = "true";
  2814. audio_mix_1.effect_config.bgm_fade_time = "1.7";
  2815. opt.operation.transcode.audio_mix_array.push_back(audio_mix_1);
  2816. AudioMix audio_mix_2 = AudioMix();
  2817. audio_mix_2.audio_source = "https://test-xxx.cos.ap-chongqing.myqcloud.com/mix2.mp3";
  2818. audio_mix_2.mix_mode = "Once";
  2819. audio_mix_2.replace = "true";
  2820. audio_mix_2.effect_config.enable_start_fade_in = "true";
  2821. audio_mix_2.effect_config.start_fade_in_time = "3";
  2822. audio_mix_2.effect_config.enable_end_fade_out = "false";
  2823. audio_mix_2.effect_config.end_fade_out_time = "0";
  2824. audio_mix_2.effect_config.enable_bgm_fade = "true";
  2825. audio_mix_2.effect_config.bgm_fade_time = "1.7";
  2826. opt.operation.transcode.audio_mix_array.push_back(audio_mix_2);
  2827. opt.operation.output.bucket = bucket_name;
  2828. opt.operation.output.region = "ap-chongqing";
  2829. opt.operation.output.object = "output/out.${ext}";
  2830. req.setOperation(opt);
  2831. CosResult result = cos.CreateDataProcessJobs(req, &resp);
  2832. if (result.IsSucc()) {
  2833. std::cout << "CreateDataProcessJobs Succ." << std::endl;
  2834. } else {
  2835. std::cout << "CreateDataProcessJobs Fail, ErrorMsg: "
  2836. << result.GetErrorMsg() << std::endl;
  2837. }
  2838. std::cout
  2839. << "===================CreateDataProcessJobs============================="
  2840. << std::endl;
  2841. PrintResult(result, resp);
  2842. std::cout << "========================================================"
  2843. << std::endl;
  2844. }
  2845. // 创建智能封面任务
  2846. void CreateSmartCoverJobs(qcloud_cos::CosAPI& cos,
  2847. const std::string& bucket_name) {
  2848. CreateDataProcessJobsReq req(bucket_name);
  2849. CreateDataProcessJobsResp resp;
  2850. JobsOptions opt;
  2851. opt.input.bucket = bucket_name;
  2852. opt.input.region = "ap-chongqing";
  2853. opt.input.object = "test.mp4";
  2854. opt.tag = "SmartCover";
  2855. // 使用模版ID提交任务
  2856. // opt.operation.template_id = "XXXXXXXXXXXXXXXXXXX";
  2857. // 使用参数形式提交任务
  2858. opt.operation.smart_cover.format = "jpg";
  2859. opt.operation.smart_cover.width = "1280";
  2860. opt.operation.smart_cover.height = "960";
  2861. opt.operation.smart_cover.count = "5";
  2862. opt.operation.smart_cover.delete_duplicates = "true";
  2863. opt.operation.output.bucket = bucket_name;
  2864. opt.operation.output.region = "ap-chongqing";
  2865. opt.operation.output.object = "output/out-${number}.jpg";
  2866. req.setOperation(opt);
  2867. CosResult result = cos.CreateDataProcessJobs(req, &resp);
  2868. if (result.IsSucc()) {
  2869. std::cout << "CreateDataProcessJobs Succ." << std::endl;
  2870. } else {
  2871. std::cout << "CreateDataProcessJobs Fail, ErrorMsg: "
  2872. << result.GetErrorMsg() << std::endl;
  2873. }
  2874. std::cout
  2875. << "===================CreateDataProcessJobs============================="
  2876. << std::endl;
  2877. PrintResult(result, resp);
  2878. std::cout << "========================================================"
  2879. << std::endl;
  2880. }
  2881. // 创建数字水印任务任务
  2882. void CreateDigitalWatermarkJobs(qcloud_cos::CosAPI& cos,
  2883. const std::string& bucket_name) {
  2884. CreateDataProcessJobsReq req(bucket_name);
  2885. CreateDataProcessJobsResp resp;
  2886. JobsOptions opt;
  2887. opt.input.bucket = bucket_name;
  2888. opt.input.region = "ap-chongqing";
  2889. opt.input.object = "test.mp4";
  2890. opt.tag = "DigitalWatermark";
  2891. // 使用参数形式提交任务
  2892. opt.operation.digital_watermark.type = "Text";
  2893. opt.operation.digital_watermark.version = "V1";
  2894. opt.operation.digital_watermark.message = "水印内容";
  2895. opt.operation.output.bucket = bucket_name;
  2896. opt.operation.output.region = "ap-chongqing";
  2897. opt.operation.output.object = "output/out.${ext}";
  2898. req.setOperation(opt);
  2899. CosResult result = cos.CreateDataProcessJobs(req, &resp);
  2900. if (result.IsSucc()) {
  2901. std::cout << "CreateDataProcessJobs Succ." << std::endl;
  2902. } else {
  2903. std::cout << "CreateDataProcessJobs Fail, ErrorMsg: "
  2904. << result.GetErrorMsg() << std::endl;
  2905. }
  2906. std::cout
  2907. << "===================CreateDataProcessJobs============================="
  2908. << std::endl;
  2909. PrintResult(result, resp);
  2910. std::cout << "========================================================"
  2911. << std::endl;
  2912. }
  2913. // 创建提取数字水印任务任务
  2914. void CreateExtractDigitalWatermarkJobs(qcloud_cos::CosAPI& cos,
  2915. const std::string& bucket_name) {
  2916. CreateDataProcessJobsReq req(bucket_name);
  2917. CreateDataProcessJobsResp resp;
  2918. JobsOptions opt;
  2919. opt.input.bucket = bucket_name;
  2920. opt.input.region = "ap-chongqing";
  2921. opt.input.object = "test.mp4";
  2922. opt.tag = "ExtractDigitalWatermark";
  2923. // 使用参数形式提交任务
  2924. opt.operation.extract_digital_watermark.type = "Text";
  2925. opt.operation.extract_digital_watermark.version = "V1";
  2926. req.setOperation(opt);
  2927. CosResult result = cos.CreateDataProcessJobs(req, &resp);
  2928. if (result.IsSucc()) {
  2929. std::cout << "CreateDataProcessJobs Succ." << std::endl;
  2930. } else {
  2931. std::cout << "CreateDataProcessJobs Fail, ErrorMsg: "
  2932. << result.GetErrorMsg() << std::endl;
  2933. }
  2934. std::cout
  2935. << "===================CreateDataProcessJobs============================="
  2936. << std::endl;
  2937. PrintResult(result, resp);
  2938. std::cout << "========================================================"
  2939. << std::endl;
  2940. }
  2941. // 创建精彩集锦任务任务
  2942. void CreateVideoMontageJobs(qcloud_cos::CosAPI& cos,
  2943. const std::string& bucket_name) {
  2944. CreateDataProcessJobsReq req(bucket_name);
  2945. CreateDataProcessJobsResp resp;
  2946. JobsOptions opt;
  2947. opt.input.bucket = bucket_name;
  2948. opt.input.region = "ap-chongqing";
  2949. opt.input.object = "test.mp4";
  2950. opt.tag = "VideoMontage";
  2951. // 使用模版ID提交任务
  2952. // opt.operation.template_id = "XXXXXXXXXXXXXXXXXXX";
  2953. // 使用参数形式提交任务
  2954. opt.operation.video_montage.container.format = "mp4";
  2955. opt.operation.video_montage.video.codec = "H.264";
  2956. opt.operation.video_montage.video.width = "1280";
  2957. opt.operation.video_montage.video.bit_rate = "1000";
  2958. opt.operation.video_montage.audio.codec = "aac";
  2959. opt.operation.video_montage.audio.sample_format = "fltp";
  2960. opt.operation.video_montage.audio.bit_rate = "128";
  2961. opt.operation.video_montage.audio.channels = "4";
  2962. // 混音参数
  2963. AudioMix audio_mix_1 = AudioMix();
  2964. audio_mix_1.audio_source = "https://test-xxx.cos.ap-chongqing.myqcloud.com/mix1.mp3";
  2965. audio_mix_1.mix_mode = "Once";
  2966. audio_mix_1.replace = "true";
  2967. audio_mix_1.effect_config.enable_start_fade_in = "true";
  2968. audio_mix_1.effect_config.start_fade_in_time = "3";
  2969. audio_mix_1.effect_config.enable_end_fade_out = "false";
  2970. audio_mix_1.effect_config.end_fade_out_time = "0";
  2971. audio_mix_1.effect_config.enable_bgm_fade = "true";
  2972. audio_mix_1.effect_config.bgm_fade_time = "1.7";
  2973. opt.operation.transcode.audio_mix_array.push_back(audio_mix_1);
  2974. AudioMix audio_mix_2 = AudioMix();
  2975. audio_mix_2.audio_source = "https://test-xxx.cos.ap-chongqing.myqcloud.com/mix2.mp3";
  2976. audio_mix_2.mix_mode = "Once";
  2977. audio_mix_2.replace = "true";
  2978. audio_mix_2.effect_config.enable_start_fade_in = "true";
  2979. audio_mix_2.effect_config.start_fade_in_time = "3";
  2980. audio_mix_2.effect_config.enable_end_fade_out = "false";
  2981. audio_mix_2.effect_config.end_fade_out_time = "0";
  2982. audio_mix_2.effect_config.enable_bgm_fade = "true";
  2983. audio_mix_2.effect_config.bgm_fade_time = "1.7";
  2984. opt.operation.transcode.audio_mix_array.push_back(audio_mix_2);
  2985. opt.operation.output.bucket = bucket_name;
  2986. opt.operation.output.region = "ap-chongqing";
  2987. opt.operation.output.object = "output/out.${ext}";
  2988. req.setOperation(opt);
  2989. CosResult result = cos.CreateDataProcessJobs(req, &resp);
  2990. if (result.IsSucc()) {
  2991. std::cout << "CreateDataProcessJobs Succ." << std::endl;
  2992. } else {
  2993. std::cout << "CreateDataProcessJobs Fail, ErrorMsg: "
  2994. << result.GetErrorMsg() << std::endl;
  2995. }
  2996. std::cout
  2997. << "===================CreateDataProcessJobs============================="
  2998. << std::endl;
  2999. PrintResult(result, resp);
  3000. std::cout << "========================================================"
  3001. << std::endl;
  3002. }
  3003. // 创建人声分离任务
  3004. void CreateVoiceSeparateJobs(qcloud_cos::CosAPI& cos,
  3005. const std::string& bucket_name) {
  3006. CreateDataProcessJobsReq req(bucket_name);
  3007. CreateDataProcessJobsResp resp;
  3008. JobsOptions opt;
  3009. opt.input.bucket = bucket_name;
  3010. opt.input.region = "ap-chongqing";
  3011. opt.input.object = "test.mp3";
  3012. opt.tag = "VoiceSeparate";
  3013. // 使用模版ID提交任务
  3014. // opt.operation.template_id = "XXXXXXXXXXXXXXXXXXX";
  3015. // 使用参数形式提交任务
  3016. opt.operation.voice_separate.audio_mode = "IsAudio";
  3017. opt.operation.voice_separate.audio_config.codec = "aac";
  3018. opt.operation.voice_separate.audio_config.sample_rate = "11025";
  3019. opt.operation.voice_separate.audio_config.bit_rate = "16";
  3020. opt.operation.voice_separate.audio_config.channels = "2";
  3021. opt.operation.output.bucket = bucket_name;
  3022. opt.operation.output.region = "ap-chongqing";
  3023. opt.operation.output.object = "output/out.${ext}";
  3024. opt.operation.output.au_object = "output/au_object.${ext}";
  3025. req.setOperation(opt);
  3026. CosResult result = cos.CreateDataProcessJobs(req, &resp);
  3027. if (result.IsSucc()) {
  3028. std::cout << "CreateDataProcessJobs Succ." << std::endl;
  3029. } else {
  3030. std::cout << "CreateDataProcessJobs Fail, ErrorMsg: "
  3031. << result.GetErrorMsg() << std::endl;
  3032. }
  3033. std::cout
  3034. << "===================CreateDataProcessJobs============================="
  3035. << std::endl;
  3036. PrintResult(result, resp);
  3037. std::cout << "========================================================"
  3038. << std::endl;
  3039. }
  3040. // 创建转封装任务
  3041. void CreateSegmentJobs(qcloud_cos::CosAPI& cos,
  3042. const std::string& bucket_name) {
  3043. CreateDataProcessJobsReq req(bucket_name);
  3044. CreateDataProcessJobsResp resp;
  3045. JobsOptions opt;
  3046. opt.input.bucket = bucket_name;
  3047. opt.input.region = "ap-chongqing";
  3048. opt.input.object = "test.mp4";
  3049. opt.tag = "Segment";
  3050. // 使用模版ID提交任务
  3051. // opt.operation.template_id = "XXXXXXXXXXXXXXXXXXX";
  3052. // 使用参数形式提交任务
  3053. opt.operation.segment.duration = "15";
  3054. opt.operation.segment.format = "mp4";
  3055. opt.operation.output.bucket = bucket_name;
  3056. opt.operation.output.region = "ap-chongqing";
  3057. opt.operation.output.object = "output/out-${number}";
  3058. req.setOperation(opt);
  3059. CosResult result = cos.CreateDataProcessJobs(req, &resp);
  3060. if (result.IsSucc()) {
  3061. std::cout << "CreateDataProcessJobs Succ." << std::endl;
  3062. } else {
  3063. std::cout << "CreateDataProcessJobs Fail, ErrorMsg: "
  3064. << result.GetErrorMsg() << std::endl;
  3065. }
  3066. std::cout
  3067. << "===================CreateDataProcessJobs============================="
  3068. << std::endl;
  3069. PrintResult(result, resp);
  3070. std::cout << "========================================================"
  3071. << std::endl;
  3072. }
  3073. // 查询文件解压缩任务
  3074. void DescribeFileUncompressJobs(qcloud_cos::CosAPI& cos,
  3075. const std::string& bucket_name) {
  3076. DescribeDataProcessJobReq req(bucket_name);
  3077. DescribeDataProcessJobResp resp;
  3078. // 任务ID
  3079. req.SetJobId("XXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
  3080. CosResult result = cos.DescribeDataProcessJob(req, &resp);
  3081. if (result.IsSucc()) {
  3082. std::cout << "DescribeDataProcessJob Succ." << std::endl;
  3083. } else {
  3084. std::cout << "DescribeDataProcessJob Fail, ErrorMsg: "
  3085. << result.GetErrorMsg() << std::endl;
  3086. }
  3087. std::cout
  3088. << "===================DescribeDataProcessJob============================="
  3089. << std::endl;
  3090. PrintResult(result, resp);
  3091. std::cout << "========================================================"
  3092. << std::endl;
  3093. }
  3094. // 图片同步审核
  3095. // https://cloud.tencent.com/document/product/436/45434
  3096. void GetImageAuditing(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  3097. const std::string& object_name) {
  3098. GetImageAuditingReq req(bucket_name);
  3099. GetImageAuditingResp resp;
  3100. // 审核对象
  3101. req.SetObjectKey(object_name);
  3102. // 审核图片链接,Object和URL只用设置一个,设置URL时默认审核URL
  3103. // req.SetDetectUrl("https://exampleurl.com");
  3104. // 审核策略,不填为默认审核策略
  3105. // req.SetBizType("xxxxxxxxxxxxxxxxxxxxxxxxx");
  3106. // 该字段在结果中返回原始内容
  3107. req.SetDataId("data_id_example");
  3108. // 审核场景类型
  3109. req.SetDetectType("ads,porn,politics,terrorism");
  3110. // GIF截帧间隔,单位秒
  3111. req.SetInterval(1);
  3112. // GIF最大截帧数量
  3113. req.SetMaxFrames(2);
  3114. // 超过大小限制的图片是否压缩,0 不压缩, 1 压缩
  3115. // req.SetLargeImageDetect(0);
  3116. CosResult result = cos.GetImageAuditing(req, &resp);
  3117. if (result.IsSucc()) {
  3118. std::cout << "GetImageAuditing Succ." << std::endl;
  3119. std::cout << "Result: \n" << resp.GetJobsDetail().to_string() << std::endl;
  3120. } else {
  3121. std::cout << "GetImageAuditing Fail, ErrorMsg: " << result.GetErrorMsg()
  3122. << std::endl;
  3123. }
  3124. std::cout << "===================GetImageAuditing==================="
  3125. "=========="
  3126. << std::endl;
  3127. PrintResult(result, resp);
  3128. std::cout << "========================================================"
  3129. << std::endl;
  3130. }
  3131. // 图片批量审核
  3132. // https://cloud.tencent.com/document/product/436/63593
  3133. void BatchImageAuditing(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  3134. const std::vector<std::string>& object_names) {
  3135. BatchImageAuditingReq req(bucket_name);
  3136. BatchImageAuditingResp resp;
  3137. for(std::vector<std::string>::const_iterator it = object_names.begin();
  3138. it != object_names.end(); ++it) {
  3139. AuditingInput input = AuditingInput();
  3140. input.SetObject(*it);
  3141. // 审核图片链接,Object和URL只用设置一个,设置URL时默认审核URL
  3142. // input.SetUrl("https://exampleurl.com");
  3143. // 图片标识,该字段在结果中返回原始内容
  3144. input.SetDataId("data_id_example");
  3145. // GIF截帧间隔,单位秒
  3146. // input.SetInterval(5);
  3147. // GIF最大截帧数量
  3148. // input.SetMaxFrames(20);
  3149. // 超过大小限制的图片是否压缩,0 不压缩, 1 压缩
  3150. // input.SetLargeImageDetect(0);
  3151. req.AddInput(input);
  3152. }
  3153. // 审核策略,不填为默认审核策略
  3154. // req.SetBizType("biz_type_example");
  3155. // 审核场景类型
  3156. req.SetDetectType("Ads,Porn,Politics,Terrorism");
  3157. CosResult result = cos.BatchImageAuditing(req, &resp);
  3158. if (result.IsSucc()) {
  3159. std::cout << "BatchImageAuditing Succ." << std::endl;
  3160. std::cout << "RequestId: " << resp.GetRequestId() << std::endl;
  3161. std::vector<ImageAuditingJobsDetail> jobsDetails = resp.GetJobsDetails();
  3162. for (std::vector<ImageAuditingJobsDetail>::const_iterator it = jobsDetails.begin();
  3163. it != jobsDetails.end(); ++it) {
  3164. std::cout << "JobsDetails: \n" << it->to_string() << std::endl;
  3165. }
  3166. } else {
  3167. std::cout << "BatchImageAuditing Fail, ErrorMsg: " << result.GetErrorMsg()
  3168. << std::endl;
  3169. }
  3170. std::cout << "===================BatchImageAuditing==================="
  3171. "=========="
  3172. << std::endl;
  3173. PrintResult(result, resp);
  3174. std::cout << "========================================================"
  3175. << std::endl;
  3176. }
  3177. // 图片审核任务结果查询
  3178. // https://cloud.tencent.com/document/product/436/68904
  3179. void DescribeImageAuditingJob(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  3180. const std::string& job_id) {
  3181. DescribeImageAuditingJobReq req(bucket_name);
  3182. DescribeImageAuditingJobResp resp;
  3183. req.SetJobId(job_id);
  3184. CosResult result = cos.DescribeImageAuditingJob(req, &resp);
  3185. if (result.IsSucc()) {
  3186. std::cout << "DescribeImageAuditingJob Succ." << std::endl;
  3187. std::cout << "RequestId: " << resp.GetRequestId() << std::endl;
  3188. std::cout << "JobsDetail: \n" << resp.GetJobsDetail().to_string() << std::endl;
  3189. } else {
  3190. std::cout << "DescribeImageAuditingJob Fail, ErrorMsg: " << result.GetErrorMsg()
  3191. << std::endl;
  3192. }
  3193. std::cout << "===================DescribeImageAuditingJob==================="
  3194. "=========="
  3195. << std::endl;
  3196. PrintResult(result, resp);
  3197. std::cout << "========================================================"
  3198. << std::endl;
  3199. }
  3200. // 提交视频审核任务
  3201. // https://cloud.tencent.com/document/product/436/47316
  3202. void CreateVideoAuditingJob(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  3203. const std::string& object_name) {
  3204. CreateVideoAuditingJobReq req(bucket_name);
  3205. CreateVideoAuditingJobResp resp;
  3206. // input
  3207. // 需要审核的视频object
  3208. req.SetObject(object_name);
  3209. // 审核视频链接,Object和URL只用设置一个,设置URL时默认审核URL
  3210. //req.SetUrl("https://exampleurl.com");
  3211. // 视频标识,该字段在结果中返回原始内容
  3212. req.SetDataId("DataId");
  3213. // 用户业务字段
  3214. // UserInfo user_info = UserInfo();
  3215. // user_info.SetIp("ip_example");
  3216. // req.SetUserInfo(user_info);
  3217. // conf
  3218. SnapShotConf snap_shot = SnapShotConf();
  3219. // 截图数量
  3220. snap_shot.SetCount(100);
  3221. // 截图类型,Interval表示间隔模式、Average平均模式、Fps固定帧率模式,默认为Interval,
  3222. snap_shot.SetMode("Interval");
  3223. req.SetSnapShot(snap_shot);
  3224. // 审核场景
  3225. req.SetDetectType("Porn,Ads,Politics,Terrorism,Abuse,Illegal");
  3226. // 审核策略
  3227. // req.SetBizType("xxxxxxxxxxxxxxxxxxxxxxxxx");
  3228. // 回调地址
  3229. req.SetCallBack("https://callback.com");
  3230. // 回调信息内容 默认为Simple
  3231. req.SetCallBackVersion("Simple");
  3232. // 是否审核视频声音,1 审核视频画面截图和声音,0 审核视频画面,默认0
  3233. req.SetDetectContent(1);
  3234. CosResult result = cos.CreateVideoAuditingJob(req, &resp);
  3235. if (result.IsSucc()) {
  3236. std::cout << "CreateVideoAuditingJob Succ." << std::endl;
  3237. std::cout << "RequestId: " << resp.GetRequestId() << std::endl;
  3238. std::cout << "JobsDetail: " << resp.GetJobsDetail().to_string() << std::endl;
  3239. } else {
  3240. std::cout << "CreateVideoAuditingJob Fail, ErrorMsg: " << result.GetErrorMsg()
  3241. << std::endl;
  3242. }
  3243. std::cout << "===================CreateVideoAuditingJob==================="
  3244. "=========="
  3245. << std::endl;
  3246. PrintResult(result, resp);
  3247. std::cout << "========================================================"
  3248. << std::endl;
  3249. }
  3250. // 查询视频审核任务结果
  3251. // https://cloud.tencent.com/document/product/436/47317
  3252. void DescribeVideoAuditingJob(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  3253. const std::string& job_id) {
  3254. DescribeVideoAuditingJobReq req(bucket_name);
  3255. DescribeVideoAuditingJobResp resp;
  3256. req.SetJobId(job_id);
  3257. CosResult result = cos.DescribeVideoAuditingJob(req, &resp);
  3258. if (result.IsSucc()) {
  3259. std::cout << "DescribeVideoAuditingJob Succ." << std::endl;
  3260. std::cout << "RequestId: " << resp.GetRequestId() << std::endl;
  3261. std::cout << "JobsDetail: " << resp.GetJobsDetail().to_string() << std::endl;
  3262. } else {
  3263. std::cout << "DescribeVideoAuditingJob Fail, ErrorMsg: " << result.GetErrorMsg()
  3264. << std::endl;
  3265. }
  3266. std::cout << "===================DescribeVideoAuditingJob==================="
  3267. "=========="
  3268. << std::endl;
  3269. PrintResult(result, resp);
  3270. std::cout << "========================================================"
  3271. << std::endl;
  3272. }
  3273. // 提交音频审核任务
  3274. // https://cloud.tencent.com/document/product/436/54063
  3275. void CreateAudioAuditingJob(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  3276. const std::string& object_name) {
  3277. CreateAudioAuditingJobReq req(bucket_name);
  3278. CreateAudioAuditingJobResp resp;
  3279. // input
  3280. // 需要审核的音频object
  3281. req.SetObject(object_name);
  3282. // 审核音频链接,Object和URL只用设置一个,设置URL时默认审核URL
  3283. //req.SetUrl("https://exampleurl.com");
  3284. // 音频标识,该字段在结果中返回原始内容
  3285. req.SetDataId("DataId");
  3286. // 用户业务字段
  3287. // UserInfo user_info = UserInfo();
  3288. // user_info.SetType("type");
  3289. // req.SetUserInfo(user_info);
  3290. // conf
  3291. // 审核场景
  3292. req.SetDetectType("Porn,Ads,Politics,Terrorism,Abuse,Illegal");
  3293. // 审核策略
  3294. // req.SetBizType("xxxxxxxxxxxxxxxxxxxxxxxxx");
  3295. // 回调地址
  3296. req.SetCallBack("https://callback.com");
  3297. // 回调信息内容 默认为Simple
  3298. req.SetCallBackVersion("Simple");
  3299. CosResult result = cos.CreateAudioAuditingJob(req, &resp);
  3300. if (result.IsSucc()) {
  3301. std::cout << "CreateAudioAuditingJob Succ." << std::endl;
  3302. std::cout << "RequestId: " << resp.GetRequestId() << std::endl;
  3303. std::cout << "JobsDetail: " << resp.GetJobsDetail().to_string() << std::endl;
  3304. } else {
  3305. std::cout << "CreateAudioAuditingJob Fail, ErrorMsg: " << result.GetErrorMsg()
  3306. << std::endl;
  3307. }
  3308. std::cout << "===================CreateAudioAuditingJob==================="
  3309. "=========="
  3310. << std::endl;
  3311. PrintResult(result, resp);
  3312. std::cout << "========================================================"
  3313. << std::endl;
  3314. }
  3315. // 查询音频审核任务结果
  3316. // https://cloud.tencent.com/document/product/436/54064
  3317. void DescribeAudioAuditingJob(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  3318. const std::string& job_id) {
  3319. DescribeAudioAuditingJobReq req(bucket_name);
  3320. DescribeAudioAuditingJobResp resp;
  3321. req.SetJobId(job_id);
  3322. CosResult result = cos.DescribeAudioAuditingJob(req, &resp);
  3323. if (result.IsSucc()) {
  3324. std::cout << "DescribeAudioAuditingJob Succ." << std::endl;
  3325. std::cout << "RequestId: " << resp.GetRequestId() << std::endl;
  3326. std::cout << "JobsDetails: \n" << resp.GetJobsDetail().to_string() << std::endl;
  3327. } else {
  3328. std::cout << "DescribeAudioAuditingJob Fail, ErrorMsg: " << result.GetErrorMsg()
  3329. << std::endl;
  3330. }
  3331. std::cout << "===================DescribeAudioAuditingJob==================="
  3332. "=========="
  3333. << std::endl;
  3334. PrintResult(result, resp);
  3335. std::cout << "========================================================"
  3336. << std::endl;
  3337. }
  3338. // 提交文本审核任务
  3339. // https://cloud.tencent.com/document/product/436/56289
  3340. void CreateTextAuditingJob(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  3341. const std::string& object_name) {
  3342. CreateTextAuditingJobReq req(bucket_name);
  3343. CreateTextAuditingJobResp resp;
  3344. // input
  3345. // 需要审核的文本object
  3346. req.SetObject(object_name);
  3347. // 审核文本链接,Object和URL只用设置一个,设置URL时默认审核URL
  3348. //req.SetUrl("https://exampleurl.com");
  3349. // 音频标识,该字段在结果中返回原始内容
  3350. req.SetDataId("DataId");
  3351. // 需要审核的文本
  3352. // req.SetContent("需要审核的文本");
  3353. // 用户业务字段
  3354. // UserInfo user_info = UserInfo();
  3355. // user_info.SetType("type");
  3356. // req.SetUserInfo(user_info);
  3357. // conf
  3358. // 审核场景
  3359. req.SetDetectType("Porn,Ads,Politics,Terrorism,Abuse,Illegal");
  3360. // 审核策略
  3361. // req.SetBizType("xxxxxxxxxxxxxxxxxxxxxxxxx");
  3362. // 回调地址
  3363. req.SetCallBack("https://callback.com");
  3364. // 回调信息内容 默认为Simple
  3365. req.SetCallBackVersion("Simple");
  3366. CosResult result = cos.CreateTextAuditingJob(req, &resp);
  3367. if (result.IsSucc()) {
  3368. std::cout << "CreateTextAuditingJob Succ." << std::endl;
  3369. std::cout << "RequestId: " << resp.GetRequestId() << std::endl;
  3370. std::cout << "JobsDetail: " << resp.GetJobsDetail().to_string() << std::endl;
  3371. } else {
  3372. std::cout << "CreateTextAuditingJob Fail, ErrorMsg: " << result.GetErrorMsg()
  3373. << std::endl;
  3374. }
  3375. std::cout << "===================CreateTextAuditingJob==================="
  3376. "=========="
  3377. << std::endl;
  3378. PrintResult(result, resp);
  3379. std::cout << "========================================================"
  3380. << std::endl;
  3381. }
  3382. // 查询文本审核任务结果
  3383. // https://cloud.tencent.com/document/product/436/56288
  3384. void DescribeTextAuditingJob(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  3385. const std::string& job_id) {
  3386. DescribeTextAuditingJobReq req(bucket_name);
  3387. DescribeTextAuditingJobResp resp;
  3388. req.SetJobId(job_id);
  3389. CosResult result = cos.DescribeTextAuditingJob(req, &resp);
  3390. if (result.IsSucc()) {
  3391. std::cout << "DescribeTextAuditingJob Succ." << std::endl;
  3392. std::cout << "RequestId: " << resp.GetRequestId() << std::endl;
  3393. std::cout << "JobsDetails: " << resp.GetJobsDetail().to_string() << std::endl;
  3394. } else {
  3395. std::cout << "DescribeTextAuditingJob Fail, ErrorMsg: " << result.GetErrorMsg()
  3396. << std::endl;
  3397. }
  3398. std::cout << "===================DescribeTextAuditingJob==================="
  3399. "=========="
  3400. << std::endl;
  3401. PrintResult(result, resp);
  3402. std::cout << "========================================================"
  3403. << std::endl;
  3404. }
  3405. // 提交文档审核任务
  3406. // https://cloud.tencent.com/document/product/436/59381
  3407. void CreateDocumentAuditingJob(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  3408. const std::string& object_name) {
  3409. CreateDocumentAuditingJobReq req(bucket_name);
  3410. CreateDocumentAuditingJobResp resp;
  3411. // input
  3412. // 需要审核的音频object
  3413. req.SetObject(object_name);
  3414. // 审核音频链接,Object和URL只用设置一个,设置URL时默认审核URL
  3415. //req.SetUrl("https://exampleurl.com");
  3416. // 文档标识,该字段在结果中返回原始内容
  3417. req.SetDataId("DataId");
  3418. // 指定文档后缀
  3419. req.SetType("docx");
  3420. // 用户业务字段
  3421. // UserInfo user_info = UserInfo();
  3422. // user_info.SetType("type");
  3423. // req.SetUserInfo(user_info);
  3424. // conf
  3425. // 审核场景
  3426. req.SetDetectType("Porn,Ads,Politics,Terrorism,Abuse,Illegal");
  3427. // 审核策略
  3428. // req.SetBizType("biz_type");
  3429. // 回调地址
  3430. req.SetCallBack("https://callback.com");
  3431. CosResult result = cos.CreateDocumentAuditingJob(req, &resp);
  3432. if (result.IsSucc()) {
  3433. std::cout << "CreateDocumentAuditingJob Succ." << std::endl;
  3434. std::cout << "RequestId: " << resp.GetRequestId() << std::endl;
  3435. std::cout << "JobsDetail: " << resp.GetJobsDetail().to_string() << std::endl;
  3436. } else {
  3437. std::cout << "CreateDocumentAuditingJob Fail, ErrorMsg: " << result.GetErrorMsg()
  3438. << std::endl;
  3439. }
  3440. std::cout << "===================CreateTextAuditingJob==================="
  3441. "=========="
  3442. << std::endl;
  3443. PrintResult(result, resp);
  3444. std::cout << "========================================================"
  3445. << std::endl;
  3446. }
  3447. // 查询文档审核任务结果
  3448. // https://cloud.tencent.com/document/product/436/59382
  3449. void DescribeDocumentAuditingJob(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  3450. const std::string& job_id) {
  3451. DescribeDocumentAuditingJobReq req(bucket_name);
  3452. DescribeDocumentAuditingJobResp resp;
  3453. req.SetJobId(job_id);
  3454. CosResult result = cos.DescribeDocumentAuditingJob(req, &resp);
  3455. if (result.IsSucc()) {
  3456. std::cout << "DescribeDocumentAuditingJob Succ." << std::endl;
  3457. std::cout << "RequestId: " << resp.GetRequestId() << std::endl;
  3458. std::cout << "JobsDetails: \n" << resp.GetJobsDetail().to_string() << std::endl;
  3459. } else {
  3460. std::cout << "DescribeDocumentAuditingJob Fail, ErrorMsg: " << result.GetErrorMsg()
  3461. << std::endl;
  3462. }
  3463. std::cout << "===================DescribeDocumentAuditingJob==================="
  3464. "=========="
  3465. << std::endl;
  3466. PrintResult(result, resp);
  3467. std::cout << "========================================================"
  3468. << std::endl;
  3469. }
  3470. // 提交网页审核任务
  3471. // https://cloud.tencent.com/document/product/436/63958
  3472. void CreateWebPageAuditingJob(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  3473. const std::string& url) {
  3474. CreateWebPageAuditingJobReq req(bucket_name);
  3475. CreateWebPageAuditingJobResp resp;
  3476. // input
  3477. // 审核网页链接
  3478. req.SetUrl(url);
  3479. // 音频标识,该字段在结果中返回原始内容
  3480. req.SetDataId("DataId");
  3481. // 用户业务字段
  3482. // UserInfo user_info = UserInfo();
  3483. // user_info.SetType("type");
  3484. // req.SetUserInfo(user_info);
  3485. // conf
  3486. // 审核场景
  3487. req.SetDetectType("Porn,Ads,Politics,Terrorism,Abuse,Illegal");
  3488. // 是否需要高亮展示网页内的违规文本
  3489. req.SetReturnHighligthHtml(true);
  3490. // 回调地址
  3491. req.SetCallBack("https://callback.com");
  3492. CosResult result = cos.CreateWebPageAuditingJob(req, &resp);
  3493. if (result.IsSucc()) {
  3494. std::cout << "CreateWebPageAuditingJob Succ." << std::endl;
  3495. std::cout << "RequestId: " << resp.GetRequestId() << std::endl;
  3496. std::cout << "JobsDetail: \n" << resp.GetJobsDetail().to_string() << std::endl;
  3497. } else {
  3498. std::cout << "CreateWebPageAuditingJob Fail, ErrorMsg: " << result.GetErrorMsg()
  3499. << std::endl;
  3500. }
  3501. std::cout << "===================CreateWebPageAuditingJob==================="
  3502. "=========="
  3503. << std::endl;
  3504. PrintResult(result, resp);
  3505. std::cout << "========================================================"
  3506. << std::endl;
  3507. }
  3508. // 查询网页审核任务结果
  3509. // https://cloud.tencent.com/document/product/436/63959
  3510. void DescribeWebPageAuditingJob(qcloud_cos::CosAPI& cos, const std::string& bucket_name,
  3511. const std::string& job_id) {
  3512. DescribeWebPageAuditingJobReq req(bucket_name);
  3513. DescribeWebPageAuditingJobResp resp;
  3514. req.SetJobId(job_id);
  3515. CosResult result = cos.DescribeWebPageAuditingJob(req, &resp);
  3516. if (result.IsSucc()) {
  3517. std::cout << "DescribeWebPageAuditingJob Succ." << std::endl;
  3518. std::cout << "RequestId: " << resp.GetRequestId() << std::endl;
  3519. std::cout << "JobsDetails: " << resp.GetJobsDetail().to_string() << std::endl;
  3520. } else {
  3521. std::cout << "DescribeWebPageAuditingJob Fail, ErrorMsg: " << result.GetErrorMsg()
  3522. << std::endl;
  3523. }
  3524. std::cout << "===================DescribeWebPageAuditingJob==================="
  3525. "=========="
  3526. << std::endl;
  3527. PrintResult(result, resp);
  3528. std::cout << "========================================================"
  3529. << std::endl;
  3530. }
  3531. void TestLogCallback(const std::string& log) {
  3532. std::ofstream ofs;
  3533. ofs.open("test.log", std::ios_base::app);
  3534. ofs << log;
  3535. ofs.close();
  3536. }
  3537. /*
  3538. export CPP_SDK_ACCESS_KEY=xxx
  3539. export CPP_SDK_SECRET_KEY=xxx
  3540. export CPP_SDK_REGION=xxx
  3541. export CPP_SDK_APPID=xxx
  3542. export CPP_SDK_BUCKET=xxx
  3543. */
  3544. int main(int argc, char** argv) {
  3545. UNUSED_PARAM(argc)
  3546. UNUSED_PARAM(argv)
  3547. // config.json中字段的说明,可以参考https://cloud.tencent.com/document/product/436/12301
  3548. // qcloud_cos::CosConfig config("./config.json"); //配置文件方式初始化
  3549. std::string appid_str = getenv("CPP_SDK_APPID");
  3550. uint64_t appId = std::stoll(appid_str); // 替换为用户appid
  3551. qcloud_cos::CosConfig config(appId,
  3552. getenv("CPP_SDK_ACCESS_KEY"),
  3553. getenv("CPP_SDK_SECRET_KEY"),
  3554. getenv("CPP_SDK_REGION")); //替换为用户ak,sk 和 region
  3555. config.SetLogCallback(&TestLogCallback);
  3556. // config.SetTmpToken("TmpToken"); //替换为用户临时密钥
  3557. qcloud_cos::CosAPI cos(config);
  3558. std::string bucket_name = getenv("CPP_SDK_BUCKET"); //替换为用户的存储桶名,由bucketname-appid
  3559. ///组成,appid必须填入,可以在COS控制台查看存储桶名称。
  3560. /// https://console.cloud.tencent.com/cos5/bucket
  3561. CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
  3562. GetService(cos);
  3563. PutBucket(cos, bucket_name);
  3564. GetBucket(cos, bucket_name);
  3565. HeadBucket(cos, bucket_name);
  3566. PutBucketVersioning(cos, bucket_name);
  3567. GetBucketVersioning(cos, bucket_name);
  3568. PutBucketReplication(cos, bucket_name);
  3569. GetBucketReplication(cos, bucket_name);
  3570. DeleteBucketReplication(cos, bucket_name);
  3571. PutBucketLifecycle(cos, bucket_name);
  3572. GetBucketLifecycle(cos, bucket_name);
  3573. DeleteBucketLifecycle(cos, bucket_name);
  3574. PutBucketACL(cos, bucket_name);
  3575. GetBucketACL(cos, bucket_name);
  3576. PutBucketPolicy(cos, bucket_name);
  3577. GetBucketPolicy(cos, bucket_name);
  3578. DeleteBucketPolicy(cos, bucket_name);
  3579. PutBucketCORS(cos, bucket_name);
  3580. GetBucketCORS(cos, bucket_name);
  3581. DeleteBucketCORS(cos, bucket_name);
  3582. PutBucketDomain(cos, bucket_name);
  3583. GetBucketDomain(cos, bucket_name);
  3584. PutBucketLogging(cos, bucket_name, " xxxxx-appid", "prefix"); //替换为用户的日志投递存储桶名和投递路径,由bucketname-appid
  3585. GetBucketLogging(cos, bucket_name);
  3586. PutBucketWebsite(cos, bucket_name);
  3587. GetBucketWebsite(cos, bucket_name);
  3588. DeleteBucketWebsite(cos, bucket_name);
  3589. PutBucketTagging(cos, bucket_name);
  3590. GetBucketTagging(cos, bucket_name);
  3591. DeleteBucketTagging(cos, bucket_name);
  3592. PutBucketInventory(cos, bucket_name);
  3593. GetBucketInventory(cos,bucket_name);
  3594. // 简单上传(文件)
  3595. PutObjectByFile(cos, bucket_name, "test/test1", "./temp/test1"); //替换为用户 object key 和本地文件路径
  3596. HeadObject(cos, bucket_name, "test/test1"); //替换为用户 object key
  3597. GetObjectByFile(cos, bucket_name, "test/test1", "./temp/test1.down"); //替换为用户 object key 和本地文件路径
  3598. PutObjectByStream(cos, bucket_name);
  3599. GetObjectByStream(cos, bucket_name, "test/sevenyou_10m");//替换为用户 object key
  3600. MultiUpload(cos, bucket_name); //用户自定义复合上传
  3601. MultiUploadObject(cos, bucket_name, "test/test2", "./temp/testfilebig"); // sdk 封装好的多线程上传接口,替换为用户 object key 和本地文件路径
  3602. MultiGetObject(cos, bucket_name, "test/test2", "./temp/testfilebig.down"); // 替换为用户 object key 和本地文件路径
  3603. PutObjectACL(cos, bucket_name, "test/sevenyou_10m"); // 替换为用户 object key
  3604. GetObjectACL(cos, bucket_name, "test/sevenyou_10m"); // 替换为用户 object key
  3605. PutObjectTagging(cos, bucket_name, "test/sevenyou_10m"); // 替换为用户 object key
  3606. GetObjectTagging(cos, bucket_name, "test/sevenyou_10m"); // 替换为用户 object key
  3607. DeleteObjectTagging(cos, bucket_name, "test/sevenyou_10m"); // 替换为 用户object key
  3608. PutObjectCopy(cos, bucket_name,
  3609. "test/sevenyou_e3_put_obj_copy_from_north_normal",
  3610. "xxxx-appid.cos.region.myqcloud.com/objectkey"); //此处替换为用户拷贝源
  3611. DeleteObject(cos, bucket_name,"test/sevenyou_e3_put_obj_copy_from_north_normal"); // 替换为用户 object key
  3612. // Batch Delete
  3613. {
  3614. PutObjectByFile(cos, bucket_name, "test/test4", "./temp/test1");
  3615. PutObjectByFile(cos, bucket_name, "test/test5", "./temp/test1");
  3616. PutObjectByFile(cos, bucket_name, "test/test6", "./temp/test1");
  3617. PutObjectByFile(cos, bucket_name, "test/test7", "./temp/test1");
  3618. std::vector<std::string> objects;
  3619. std::vector<ObjectVersionPair> to_be_deleted;
  3620. objects.push_back("test/test4"); // 替换为用户 object key
  3621. objects.push_back("test/test5"); // 替换为用户 object key
  3622. objects.push_back("test/test6"); // 替换为用户 object key
  3623. objects.push_back("test/test7"); // 替换为用户 object key
  3624. for (size_t idx = 0; idx < objects.size(); ++idx) {
  3625. ObjectVersionPair pair;
  3626. pair.m_object_name = objects[idx];
  3627. // if (idx == 2) {
  3628. // pair.m_version_id = "MTg0NDY3NDI1NTg4Mjc3NzExNjI";
  3629. // } else if (idx == 3) {
  3630. // pair.m_version_id = "MTg0NDY3NDI1NTg4Mjc3NzA3Nzc";
  3631. // } // 有需要可以指定删除的版本
  3632. to_be_deleted.push_back(pair);
  3633. }
  3634. DeleteObjects(cos, bucket_name, to_be_deleted);
  3635. }
  3636. UploadCopy(cos, bucket_name); //用户自定义分块拷贝
  3637. Copy(cos, bucket_name, "test/testCopy2",
  3638. "xxxx-appid.cos.region.myqcloud.com/objectkey"); //此处替换为用户拷贝源
  3639. GeneratePresignedUrl(cos, bucket_name);
  3640. GetBucketLocation(cos, bucket_name);
  3641. IsBucketExist(cos, bucket_name);
  3642. IsObjectExist(cos, bucket_name);
  3643. ResumableGetObject(cos, bucket_name, "test/test2", "./temp/testfilebig_resumable_dowm"); // 替换为用户 object key 和本地文件路径
  3644. AsyncMultiPutObject(cos, bucket_name, "test/testfilebig_async", "./temp/testfilebig"); // 替换为用户 object key 和本地文件路径
  3645. AsyncMultiGetObject(cos, bucket_name, "test/testfilebig_async", "./temp/testfilebig_async_down"); // 替换为用户 object key 和本地文件路径
  3646. PutObjectsFromDirectoryToCosPath(cos, bucket_name,
  3647. "./temp/testdir/", "test/testdir/"); // 替换为用户 object文件夹路径 和本地路径
  3648. DownloadFromDirectory(cos, bucket_name, "test/testdir/", "./temp/testdir_down"); // 替换为用户 object文件夹路径 和本地路径
  3649. MoveObject(cos, bucket_name, "test/test1", "test/test10"); // 替换为用户move 的两个 object key
  3650. DeleteDirectory(cos, bucket_name, "test/testdir/"); // 替换为用户 object文件夹路径
  3651. DeleteObjectsByPrefix(cos, bucket_name, "test/test"); // 替换为用户 object key 的前缀
  3652. DeleteDirectory(cos, bucket_name, "test/"); // 替换为用户 object文件夹路径
  3653. DeleteBucket(cos, bucket_name);
  3654. // 图片处理
  3655. // {
  3656. // ImageThumbnail(cos, bucket_name, "flower.jpg", "flower_thumbnail.jpg");
  3657. // PutImage(cos, bucket_name, "test_ci/flower.jpg", "./flower.jpg");
  3658. // AddWatermark(cos, bucket_name, "test_ci/flower.jpg", "./flower.jpg");
  3659. // GetImageWithWatermark(cos, bucket_name, "test_ci/flower.jpg",
  3660. // "./flower_download_with_matermark.jpg"); ExtractWatermark(cos,
  3661. // bucket_name, "test_ci/flower_download_with_matermark.jpg",
  3662. // "./flower_download_with_matermark.jpg"); CloudImageProcess(cos,
  3663. // bucket_name, "test_ci/flower.jpg"); PutQRcode(cos, bucket_name,
  3664. // "test_ci/qrcode.png", "./qrcode.png"); GetQRcode(cos, bucket_name,
  3665. // "test_ci/qrcode.png");
  3666. // }
  3667. // 文档接口
  3668. //{
  3669. // DescribeDocProcessBuckets(cos);
  3670. // DocPreview(cos, bucket_name, "test.docx", "test_preview.jpg");
  3671. // CreateDocProcessJobs(cos, bucket_name);
  3672. // DescribeDocProcessJob(cos, bucket_name);
  3673. // DescribeDocProcessJobs(cos, bucket_name);
  3674. // DescribeDocProcessQueues(cos, bucket_name);
  3675. // UpdateDocProcessQueue(cos, bucket_name);
  3676. //}
  3677. // GetObjectUrl(cos, bucket_name, "test_object");
  3678. // 媒体接口
  3679. //{
  3680. // DescribeMediaBuckets(cos);
  3681. // GetSnapshot(cos, bucket_name, "1920_1080.mp4", "snapshot.jpg");
  3682. // GetMediaInfo(cos, bucket_name, "1920_1080.mp4");
  3683. // GetPm3u8(cos, bucket_name, "video.m3u8");
  3684. // DescribeMediaQueues(cos, bucket_name);
  3685. // UpdateMediaQueue(cos, bucket_name);
  3686. // CreateSnapshotJobs(cos, bucket_name);
  3687. // CreateAnimationJobs(cos, bucket_name);
  3688. // CreateConcatJobs(cos, bucket_name);
  3689. // CreateTranscodeJobs(cos, bucket_name);
  3690. // CreateDigitalWatermarkJobs(cos, bucket_name);
  3691. // CreateExtractDigitalWatermarkJobs(cos, bucket_name);
  3692. // CreateSegmentJobs(cos, bucket_name);
  3693. // CreateVoiceSeparateJobs(cos, bucket_name);
  3694. // CreateVideoMontageJobs(cos, bucket_name);
  3695. // CreateSmartCoverJobs(cos, bucket_name);
  3696. // DescribeDataProcessJobs(cos, bucket_name);
  3697. // CancelDataProcessJob(cos, bucket_name);
  3698. //}
  3699. // 文件处理接口
  3700. {
  3701. // CreateFileUncompressJobs(cos, bucket_name);
  3702. // DescribeFileUncompressJobs(cos, bucket_name);
  3703. }
  3704. // 图片审核
  3705. //{
  3706. // GetImageAuditing(cos, bucket_name, "data/audit/gif/test.gif");
  3707. // std::vector<std::string> objects = {"data/audit/ads/clipboard.png", "data/audit/git/test.jpg"};
  3708. // BatchImageAuditing(cos, bucket_name, objects);
  3709. // DescribeImageAuditingJob(cos, bucket_name, "sife0bdb79d72211ecb174525400bc4ce1");
  3710. //}
  3711. // 视频审核
  3712. //{
  3713. // CreateVideoAuditingJob(cos, bucket_name, "data/audit/video/test.mp4");
  3714. // DescribeVideoAuditingJob(cos, bucket_name, "avda91c516d81a11ec933c525400863904");
  3715. //}
  3716. // 音频审核
  3717. //{
  3718. // CreateAudioAuditingJob(cos, bucket_name, "data/audit/audio/test.mp3");
  3719. // DescribeAudioAuditingJob(cos, bucket_name, "sa3be08072d81f11ecb378525400bc4ce1");
  3720. //}
  3721. // 文本审核
  3722. //{
  3723. // CreateTextAuditingJob(cos, bucket_name, "data/audit/text/test.txt");
  3724. // DescribeTextAuditingJob(cos, bucket_name, "stf8d06a90d82211ecb378525400bc4ce1");
  3725. //}
  3726. // 文档审核
  3727. //{
  3728. // CreateDocumentAuditingJob(cos, bucket_name, "data/audit/documents/test.docx");
  3729. // DescribeDocumentAuditingJob(cos, bucket_name, "sd5609cbe0db1211ec9bdf525400863904");
  3730. //}
  3731. // 网页审核
  3732. //{
  3733. // CreateWebPageAuditingJob(cos, bucket_name, "https://www.test.com/");
  3734. // DescribeWebPageAuditingJob(cos, bucket_name, "shea80c920db2a11ec954f525400bc4ce1");
  3735. //}
  3736. #if defined(_WIN32)
  3737. // MultiUploadObjectWideChar(cos, bucket_name);
  3738. system("pause");
  3739. #endif
  3740. return 0;
  3741. }