object_req.h 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947
  1. // Copyright (c) 2017, Tencent Inc.
  2. // All rights reserved.
  3. //
  4. // Author: sevenyou <sevenyou@tencent.com>
  5. // Created: 07/21/17
  6. // Description:
  7. #pragma once
  8. #include <map>
  9. #include <sstream>
  10. #include <vector>
  11. #if defined(_WIN32)
  12. #include <codecvt>
  13. #endif
  14. #include "cos_sys_config.h"
  15. #include "request/base_req.h"
  16. #include "trsf/async_context.h"
  17. #include "util/file_util.h"
  18. #include "util/illegal_intercept.h"
  19. namespace qcloud_cos {
  20. class ObjectReq : public BaseReq {
  21. public:
  22. ObjectReq(const std::string& bucket_name, const std::string& object_name)
  23. : m_bucket_name(bucket_name), m_progress_cb(NULL), m_done_cb(NULL),
  24. m_user_data(NULL) {
  25. if (!IllegalIntercept::CheckBucket(bucket_name)) {
  26. throw std::invalid_argument("Invalid bucket_name argument :" + bucket_name);
  27. }
  28. SetObjectName(object_name);
  29. }
  30. ObjectReq() {}
  31. virtual ~ObjectReq() {}
  32. // getter and setter
  33. std::string GetBucketName() const { return m_bucket_name; }
  34. void SetBucketName(const std::string& bucket_name) {
  35. if (!IllegalIntercept::CheckBucket(bucket_name)) {
  36. throw std::invalid_argument("Invalid bucket_name argument :" + bucket_name);
  37. }
  38. m_bucket_name = bucket_name;
  39. }
  40. std::string GetObjectName() const { return m_object_name; }
  41. void SetObjectName(const std::string& object_name) {
  42. if (StringUtil::StringStartsWith(object_name, "/")) {
  43. m_object_name = object_name.substr(1);
  44. } else {
  45. m_object_name = object_name;
  46. }
  47. m_path = "/" + m_object_name;
  48. }
  49. #if 1
  50. /// @brief 设置进度回调函数
  51. void SetTransferProgressCallback(const TransferProgressCallback& process_cb) {
  52. m_progress_cb = process_cb;
  53. }
  54. /// @brief 设置完成回调函数
  55. void SetDoneCallback(const DoneCallback& done_cb) { m_done_cb = done_cb; }
  56. /// @brief 设置回调私有数据
  57. void SetUserData(void* user_data) { m_user_data = user_data; }
  58. TransferProgressCallback GetTransferProgressCallback() const {
  59. return m_progress_cb;
  60. }
  61. DoneCallback GetDoneCallback() const { return m_done_cb; }
  62. void* GetUserData() const { return m_user_data; }
  63. //virtual uint64_t GetLocalFileSize() const { return 0; }
  64. virtual std::string GetLocalFilePath() const { return ""; }
  65. #endif
  66. private:
  67. std::string m_bucket_name;
  68. std::string m_object_name;
  69. TransferProgressCallback m_progress_cb; // 进度回调
  70. DoneCallback m_done_cb; // 完成回调
  71. void* m_user_data; // 私有数据
  72. };
  73. class GetObjectReq : public ObjectReq {
  74. public:
  75. /// \brief 设置响应头部中的 Content-Type 参数
  76. void SetResponseContentType(const std::string& str) {
  77. AddParam("response-content-type", str);
  78. }
  79. /// \brief 设置响应头部中的 Content-Language 参数
  80. void SetResponseContentLang(const std::string& str) {
  81. AddParam("response-content-language", str);
  82. }
  83. /// \brief 设置响应头部中的 Content-Expires 参数
  84. void SetResponseExpires(const std::string& str) {
  85. AddParam("response-expires", str);
  86. }
  87. /// \brief 设置响应头部中的 Cache-Control 参数
  88. void SetResponseCacheControl(const std::string& str) {
  89. AddParam("response-cache-control", str);
  90. }
  91. /// \brief 设置响应头部中的 Content-Disposition 参数
  92. void SetResponseContentEncoding(const std::string& str) {
  93. AddParam("response-content-encoding", str);
  94. }
  95. /// \brief 设置响应头部中的 Content-Encoding 参数
  96. void SetResponseContentDisposition(const std::string& str) {
  97. AddParam("response-content-disposition", str);
  98. }
  99. /// \brief 设置单链接限速
  100. /// 参考https://cloud.tencent.com/document/product/436/40140
  101. void SetTrafficLimit(const uint64_t traffic_limit) {
  102. AddHeader("x-cos-traffic-limit", std::to_string(traffic_limit));
  103. }
  104. /// \brief 设置单链接限速
  105. /// 参考https://cloud.tencent.com/document/product/436/40140
  106. void SetTrafficLimit(const std::string& traffic_limit) {
  107. AddHeader("x-cos-traffic-limit", traffic_limit);
  108. }
  109. protected:
  110. GetObjectReq(const std::string& bucket_name, const std::string& object_name)
  111. : ObjectReq(bucket_name, object_name) {
  112. m_method = "GET";
  113. }
  114. virtual ~GetObjectReq() {}
  115. };
  116. class GetObjectByStreamReq : public GetObjectReq {
  117. public:
  118. GetObjectByStreamReq(const std::string& bucket_name,
  119. const std::string& object_name, std::ostream& os)
  120. : GetObjectReq(bucket_name, object_name), m_os(os) {
  121. m_method = "GET";
  122. }
  123. virtual ~GetObjectByStreamReq() {}
  124. std::ostream& GetStream() const { return m_os; }
  125. private:
  126. std::ostream& m_os;
  127. };
  128. class GetObjectByFileReq : public GetObjectReq {
  129. public:
  130. GetObjectByFileReq(const std::string& bucket_name,
  131. const std::string& object_name,
  132. const std::string& local_file_path = "")
  133. : GetObjectReq(bucket_name, object_name),
  134. m_local_file_path(local_file_path) {
  135. if (m_local_file_path.empty()) {
  136. m_local_file_path = "./" + object_name;
  137. }
  138. }
  139. virtual ~GetObjectByFileReq() {}
  140. void SetLocalFilePath(const std::string& local_file_path) {
  141. m_local_file_path = local_file_path;
  142. }
  143. std::string GetLocalFilePath() const { return m_local_file_path; }
  144. uint64_t GetLocalFileSize() const { return 0; }
  145. #if defined(_WIN32)
  146. /// \brief 如果本地文件路径为宽字符(中文,韩文等),需要调用该函数
  147. void SetWideCharPath() { mb_is_widechar_path = true; }
  148. bool IsWideCharPath() const { return mb_is_widechar_path; }
  149. std::wstring GetWideCharLocalFilePath() const {
  150. std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
  151. return converter.from_bytes(m_local_file_path);
  152. }
  153. #endif
  154. private:
  155. #if defined(_WIN32)
  156. bool mb_is_widechar_path;
  157. #endif
  158. std::string m_local_file_path;
  159. };
  160. #if 0
  161. class MultiGetObjectReq : public GetObjectReq {
  162. public:
  163. MultiGetObjectReq(const std::string& bucket_name,
  164. const std::string& object_name,
  165. const std::string& local_file_path = "")
  166. : GetObjectReq(bucket_name, object_name) {
  167. // 默认使用配置文件配置的分块大小和线程池大小
  168. m_slice_size = CosSysConfig::GetDownSliceSize();
  169. m_thread_pool_size = CosSysConfig::GetDownThreadPoolSize();
  170. if (local_file_path.empty()) {
  171. m_local_file_path = "./" + object_name;
  172. } else {
  173. m_local_file_path = local_file_path;
  174. }
  175. }
  176. virtual ~MultiGetObjectReq() {}
  177. void SetLocalFilePath(const std::string& local_file_path) {
  178. m_local_file_path = local_file_path;
  179. }
  180. std::string GetLocalFilePath() const { return m_local_file_path; }
  181. /// \brief 设置分片大小
  182. void SetSliceSize(uint64_t bytes) { m_slice_size = bytes; }
  183. /// \brief 获取分片大小
  184. uint64_t GetSliceSize() const { return m_slice_size; }
  185. /// \brief 设置线程池大小
  186. void SetThreadPoolSize(int size) {
  187. assert(size > 0);
  188. m_thread_pool_size = size;
  189. }
  190. /// \brief 获取线程池大小
  191. int GetThreadPoolSize() const { return m_thread_pool_size; }
  192. private:
  193. std::string m_local_file_path;
  194. uint64_t m_slice_size;
  195. int m_thread_pool_size;
  196. };
  197. #endif
  198. class PutObjectReq : public ObjectReq {
  199. public:
  200. /// Cache-Control RFC 2616 中定义的缓存策略,将作为 Object 元数据保存
  201. void SetCacheControl(const std::string& str) {
  202. AddHeader("Cache-Control", str);
  203. }
  204. /// Content-Disposition RFC 2616 中定义的文件名称,将作为 Object 元数据保存
  205. void SetContentDisposition(const std::string& str) {
  206. AddHeader("Content-Disposition", str);
  207. }
  208. /// Content-Encoding RFC 2616 中定义的编码格式,将作为 Object 元数据保存
  209. void SetContentEncoding(const std::string& str) {
  210. AddHeader("Content-Encoding", str);
  211. }
  212. /// Content-Type RFC 2616 中定义的内容类型(MIME),将作为 Object
  213. /// 元数据保存
  214. void SetContentType(const std::string& str) {
  215. AddHeader("Content-Type", str);
  216. }
  217. /// Expect 当使用 Expect: 100-continue
  218. /// 时,在收到服务端确认后,才会发送请求内容
  219. void SetExpect(const std::string& str) { AddHeader("Expect", str); }
  220. /// Expires RFC 2616 中定义的过期时间,将作为 Object 元数据保存
  221. void SetExpires(const std::string& str) { AddHeader("Expires", str); }
  222. /// 允许用户自定义的头部信息,将作为 Object 元数据返回.大小限制2K
  223. void SetXCosMeta(const std::string& key, const std::string& value) {
  224. AddHeader("x-cos-meta-" + key, value);
  225. }
  226. /// 设置 Object 的存储类型
  227. /// 枚举值:MAZ_STANDARD,MAZ_STANDARD_IA,INTELLIGENT_TIERING,MAZ_INTELLIGENT_TIERING,STANDARD_IA,ARCHIVE,DEEP_ARCHIVE
  228. /// 默认值:STANDARD
  229. void SetXCosStorageClass(const std::string& storage_class) {
  230. AddHeader("x-cos-storage-class", storage_class);
  231. }
  232. /// 定义Object的ACL属性,有效值:default,private,public-read
  233. /// 默认值:private
  234. void SetXCosAcl(const std::string& str) { AddHeader("x-cos-acl", str); }
  235. /// 赋予被授权者读的权限.格式:x-cos-grant-read: id=" ",id=" ".
  236. /// 当需要给子账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<SubUin>"
  237. /// 当需要给根账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<OwnerUin>"
  238. void SetXCosGrantRead(const std::string& str) {
  239. AddHeader("x-cos-grant-read", str);
  240. }
  241. /// 赋予被授权者写的权限,格式:x-cos-grant-write: id=" ",id=" "./
  242. /// 当需要给子账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<SubUin>",
  243. /// 当需要给根账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<OwnerUin>"
  244. void SetXCosGrantWrite(const std::string& str) {
  245. AddHeader("x-cos-grant-write", str);
  246. }
  247. /// 赋予被授权者读写权限.格式:x-cos-grant-full-control: id=" ",id=" ".
  248. /// 当需要给子账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<SubUin>",
  249. /// 当需要给根账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<OwnerUin>"
  250. void SetXCosGrantFullControl(const std::string& str) {
  251. AddHeader("x-cos-grant-full-control", str);
  252. }
  253. /// 设置Server端加密使用的算法, 目前支持AES256
  254. void SetXCosServerSideEncryption(const std::string& str) {
  255. AddHeader("x-cos-server-side-encryption", str);
  256. }
  257. /// \brief 设置单链接限速
  258. /// 参考https://cloud.tencent.com/document/product/436/40140
  259. void SetTrafficLimit(const uint64_t traffic_limit) {
  260. AddHeader("x-cos-traffic-limit", std::to_string(traffic_limit));
  261. }
  262. /// \brief 设置单链接限速
  263. /// 参考https://cloud.tencent.com/document/product/436/40140
  264. void SetTrafficLimit(const std::string& traffic_limit) {
  265. AddHeader("x-cos-traffic-limit", traffic_limit);
  266. }
  267. /// \brief 关闭MD5上传校验
  268. void TurnOffComputeConentMd5() { m_need_compute_contentmd5 = false; }
  269. bool ShouldComputeContentMd5() const { return m_need_compute_contentmd5; }
  270. bool ShouldCheckETag() const { return m_need_check_etag; }
  271. protected:
  272. PutObjectReq(const std::string& bucket_name, const std::string& object_name)
  273. : ObjectReq(bucket_name, object_name) {
  274. m_method = "PUT";
  275. m_need_compute_contentmd5 = true;
  276. m_need_check_etag = true;
  277. }
  278. PutObjectReq() {}
  279. virtual ~PutObjectReq() {}
  280. /// \brief 不检查ETag
  281. void TurnOffCheckETag() { m_need_check_etag = false; }
  282. private:
  283. bool m_need_compute_contentmd5;
  284. bool m_need_check_etag; // 某些api,比如append操作不检查响应中的ETag
  285. };
  286. class PutObjectByStreamReq : public PutObjectReq {
  287. public:
  288. PutObjectByStreamReq(const std::string& bucket_name,
  289. const std::string& object_name, std::istream& in_stream)
  290. : PutObjectReq(bucket_name, object_name), m_in_stream(in_stream) {}
  291. virtual ~PutObjectByStreamReq() {}
  292. std::istream& GetStream() const { return m_in_stream; }
  293. private:
  294. std::istream& m_in_stream;
  295. };
  296. class PutObjectByFileReq : public PutObjectReq {
  297. public:
  298. PutObjectByFileReq(const std::string& bucket_name,
  299. const std::string& object_name,
  300. const std::string& local_file_path = "")
  301. : PutObjectReq(bucket_name, object_name) {
  302. if (local_file_path.empty()) {
  303. m_local_file_path = "./" + object_name;
  304. } else {
  305. m_local_file_path = local_file_path;
  306. }
  307. #if defined(_WIN32)
  308. mb_is_widechar_path = false;
  309. #endif
  310. mb_check_part_crc64 = false;
  311. }
  312. virtual ~PutObjectByFileReq() {}
  313. virtual uint64_t GetLocalFileSize() const {
  314. #if defined(_WIN32)
  315. if (mb_is_widechar_path)
  316. return FileUtil::GetFileLen(GetWideCharLocalFilePath());
  317. else
  318. #endif
  319. return FileUtil::GetFileLen(m_local_file_path);
  320. }
  321. void SetLocalFilePath(const std::string& local_file_path) {
  322. m_local_file_path = local_file_path;
  323. }
  324. // MultiPutObjectReq: use crc64 instead of md5 for consistency check
  325. void SetCheckPartCrc64(bool part_crc64) {
  326. mb_check_part_crc64 = part_crc64;
  327. }
  328. bool CheckPartCrc64() const {
  329. return mb_check_part_crc64;
  330. }
  331. std::string GetLocalFilePath() const { return m_local_file_path; }
  332. #if defined(_WIN32)
  333. /// \brief 如果本地文件路径为宽字符(中文,韩文等),需要调用该函数
  334. void SetWideCharPath() { mb_is_widechar_path = true; }
  335. bool IsWideCharPath() const { return mb_is_widechar_path; }
  336. std::wstring GetWideCharLocalFilePath() const {
  337. std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
  338. return converter.from_bytes(m_local_file_path);
  339. }
  340. #endif
  341. private:
  342. std::string m_local_file_path;
  343. #if defined(_WIN32)
  344. bool mb_is_widechar_path; // 标识文件路径是否为宽字符
  345. #endif
  346. bool mb_check_part_crc64;
  347. };
  348. class DeleteObjectReq : public ObjectReq {
  349. public:
  350. DeleteObjectReq(const std::string& bucket_name,
  351. const std::string& object_name)
  352. : ObjectReq(bucket_name, object_name) {
  353. SetMethod("DELETE");
  354. }
  355. DeleteObjectReq(const std::string& bucket_name,
  356. const std::string& object_name, const std::string& version_id)
  357. : ObjectReq(bucket_name, object_name) {
  358. SetMethod("DELETE");
  359. AddParam("versionId", version_id);
  360. }
  361. ~DeleteObjectReq() {}
  362. /// \brief 删除指定版本号的对象
  363. void SetXCosVersionId(const std::string& version_id) {
  364. AddParam("versionId", version_id);
  365. }
  366. };
  367. class DeleteObjectsReq : public BaseReq {
  368. public:
  369. explicit DeleteObjectsReq(const std::string& bucket_name)
  370. : m_is_quiet(false), m_bucket_name(bucket_name) {
  371. if (!IllegalIntercept::CheckBucket(bucket_name)) {
  372. throw std::invalid_argument("Invalid bucket_name argument :" + bucket_name);
  373. }
  374. SetMethod("POST");
  375. AddParam("delete", "");
  376. }
  377. DeleteObjectsReq(const std::string& bucket_name,
  378. const std::vector<ObjectVersionPair>& objects)
  379. : m_is_quiet(false), m_bucket_name(bucket_name) {
  380. if (!IllegalIntercept::CheckBucket(bucket_name)) {
  381. throw std::invalid_argument("Invalid bucket_name argument :" + bucket_name);
  382. }
  383. SetMethod("POST");
  384. AddParam("delete", "");
  385. m_objvers = objects;
  386. }
  387. ~DeleteObjectsReq() {}
  388. bool GenerateRequestBody(std::string* body) const;
  389. void SetQuiet() { m_is_quiet = true; }
  390. // getter and setter
  391. std::string GetBucketName() const { return m_bucket_name; }
  392. void SetBucketName(const std::string& bucket_name) {
  393. if (!IllegalIntercept::CheckBucket(bucket_name)) {
  394. throw std::invalid_argument("Invalid bucket_name argument :" + bucket_name);
  395. }
  396. m_bucket_name = bucket_name;
  397. }
  398. std::vector<ObjectVersionPair> GetObjectVersions() const { return m_objvers; }
  399. uint32_t GetObjectVerionsSize() const { return m_objvers.size(); }
  400. void AddObjectVersion(const std::string& object, const std::string& version) {
  401. ObjectVersionPair pair;
  402. pair.m_object_name = object;
  403. pair.m_version_id = version;
  404. m_objvers.push_back(pair);
  405. }
  406. void AddObject(const std::string& object) {
  407. ObjectVersionPair pair;
  408. pair.m_object_name = object;
  409. pair.m_version_id = "";
  410. m_objvers.push_back(pair);
  411. }
  412. private:
  413. bool m_is_quiet;
  414. std::string m_bucket_name;
  415. std::vector<ObjectVersionPair> m_objvers;
  416. };
  417. class HeadObjectReq : public ObjectReq {
  418. public:
  419. HeadObjectReq(const std::string& bucket_name, const std::string& object_name)
  420. : ObjectReq(bucket_name, object_name) {
  421. m_method = "HEAD";
  422. }
  423. virtual ~HeadObjectReq() {}
  424. };
  425. class InitMultiUploadReq : public ObjectReq {
  426. public:
  427. InitMultiUploadReq(const std::string& bucket_name,
  428. const std::string& object_name)
  429. : ObjectReq(bucket_name, object_name) {
  430. m_method = "POST";
  431. }
  432. virtual ~InitMultiUploadReq() {}
  433. /// Cache-Control RFC 2616 中定义的缓存策略,将作为 Object 元数据保存
  434. void SetCacheControl(const std::string& str) {
  435. AddHeader("Cache-Control", str);
  436. }
  437. /// Content-Disposition RFC 2616 中定义的文件名称,将作为 Object 元数据保存
  438. void SetContentDisposition(const std::string& str) {
  439. AddHeader("Content-Disposition", str);
  440. }
  441. /// Content-Encoding RFC 2616 中定义的编码格式,将作为 Object 元数据保存
  442. void SetContentEncoding(const std::string& str) {
  443. AddHeader("Content-Encoding", str);
  444. }
  445. /// Content-Type RFC 2616 中定义的内容类型(MIME),将作为 Object
  446. /// 元数据保存
  447. void SetContentType(const std::string& str) {
  448. AddHeader("Content-Type", str);
  449. }
  450. /// Expires RFC 2616 中定义的过期时间,将作为 Object 元数据保存
  451. void SetExpires(const std::string& str) { AddHeader("Expires", str); }
  452. /// 允许用户自定义的头部信息,将作为 Object 元数据返回.大小限制2K
  453. void SetXCosMeta(const std::string& key, const std::string& value) {
  454. AddHeader("x-cos-meta-" + key, value);
  455. }
  456. /// x-cos-storage-class 设置 Object 的存储级别,枚举值:STANDARD,STANDARD_IA,
  457. /// 默认值:STANDARD
  458. void SetXCosStorageClass(const std::string& storage_class) {
  459. AddHeader("x-cos-storage-class", storage_class);
  460. }
  461. /// 定义Object的ACL属性,有效值:default,private,public-read
  462. /// 默认值:private
  463. void SetXCosAcl(const std::string& str) { AddHeader("x-cos-acl", str); }
  464. /// 赋予被授权者读的权限.格式:x-cos-grant-read: id=" ",id=" ".
  465. /// 当需要给子账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<SubUin>"
  466. /// 当需要给根账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<OwnerUin>"
  467. void SetXCosGrantRead(const std::string& str) {
  468. AddHeader("x-cos-grant-read", str);
  469. }
  470. /// 赋予被授权者写的权限,格式:x-cos-grant-write: id=" ",id=" "./
  471. /// 当需要给子账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<SubUin>",
  472. /// 当需要给根账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<OwnerUin>"
  473. void SetXCosGrantWrite(const std::string& str) {
  474. AddHeader("x-cos-grant-write", str);
  475. }
  476. /// 赋予被授权者读写权限.格式:x-cos-grant-full-control: id=" ",id=" ".
  477. /// 当需要给子账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<SubUin>",
  478. /// 当需要给根账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<OwnerUin>"
  479. void SetXCosGrantFullControl(const std::string& str) {
  480. AddHeader("x-cos-grant-full-control", str);
  481. }
  482. /// 设置Server端加密使用的算法, 目前支持AES256
  483. void SetXCosServerSideEncryption(const std::string& str) {
  484. AddHeader("x-cos-server-side-encryption", str);
  485. }
  486. };
  487. class UploadPartDataReq : public ObjectReq {
  488. public:
  489. UploadPartDataReq(const std::string& bucket_name,
  490. const std::string& object_name,
  491. const std::string& upload_id, std::istream& in_stream)
  492. : ObjectReq(bucket_name, object_name), m_in_stream(in_stream),
  493. m_upload_id(upload_id), m_part_number(1) {
  494. m_method = "PUT";
  495. m_need_compute_contentmd5 = true;
  496. }
  497. /// \brief 设置本次分块上传的ID
  498. void SetUploadId(const std::string& upload_id) { m_upload_id = upload_id; }
  499. /// \brief 设置本次分块上传的编号
  500. void SetPartNumber(uint64_t part_number) { m_part_number = part_number; }
  501. std::string GetUploadId() const { return m_upload_id; }
  502. uint64_t GetPartNumber() const { return m_part_number; }
  503. std::istream& GetStream() const { return m_in_stream; }
  504. // 默认开启MD5上传校验
  505. void TurnOnComputeConentMd5() { m_need_compute_contentmd5 = true; }
  506. void TurnOffComputeConentMd5() { m_need_compute_contentmd5 = false; }
  507. bool ShouldComputeContentMd5() const { return m_need_compute_contentmd5; }
  508. /// \brief 设置单链接限速
  509. /// 参考https://cloud.tencent.com/document/product/436/40140
  510. void SetTrafficLimit(const uint64_t traffic_limit) {
  511. AddHeader("x-cos-traffic-limit", std::to_string(traffic_limit));
  512. }
  513. /// \brief 设置单链接限速
  514. /// 参考https://cloud.tencent.com/document/product/436/40140
  515. void SetTrafficLimit(const std::string& traffic_limit) {
  516. AddHeader("x-cos-traffic-limit", traffic_limit);
  517. }
  518. private:
  519. std::istream& m_in_stream;
  520. std::string m_upload_id;
  521. uint64_t m_part_number;
  522. bool m_need_compute_contentmd5;
  523. };
  524. class UploadPartCopyDataReq : public ObjectReq {
  525. public:
  526. UploadPartCopyDataReq(const std::string& bucket_name,
  527. const std::string& object_name,
  528. const std::string& upload_id)
  529. : ObjectReq(bucket_name, object_name), m_upload_id(upload_id),
  530. m_part_number(1) {
  531. m_method = "PUT";
  532. }
  533. UploadPartCopyDataReq(const std::string& bucket_name,
  534. const std::string& object_name,
  535. const std::string& upload_id, uint64_t part_number)
  536. : ObjectReq(bucket_name, object_name), m_upload_id(upload_id),
  537. m_part_number(part_number) {
  538. m_method = "PUT";
  539. }
  540. /// \brief 设置本次分块复制的ID
  541. void SetUploadId(const std::string& upload_id) { m_upload_id = upload_id; }
  542. /// \brief 设置本次分块复制的编号
  543. void SetPartNumber(uint64_t part_number) { m_part_number = part_number; }
  544. std::string GetUploadId() const { return m_upload_id; }
  545. uint64_t GetPartNumber() const { return m_part_number; }
  546. /// \brief 设置本次分块复制的源文件 URL 路径,可以通过 versionid
  547. /// 子资源指定历史版本
  548. void SetXCosCopySource(const std::string& src) {
  549. AddHeader("x-cos-copy-source", src);
  550. }
  551. /// \brief 设置源文件的字节范围,
  552. /// 范围值必须使用 bytes=first-last 格式,first 和 last 都是基于 0
  553. /// 开始的偏移量。 例如 bytes=0-9
  554. /// 表示你希望拷贝源文件的开头10个字节的数据,如果不指定,则表示拷贝整个文件
  555. void SetXCosCopySourceRange(const std::string& range) {
  556. AddHeader("x-cos-copy-source-range", range);
  557. }
  558. /// \brief 当 Object 在指定时间后被修改,则执行操作,否则返回 412
  559. void SetXCosCopySourceIfModifiedSince(const std::string& date) {
  560. AddHeader("x-cos-copy-source-If-Modified-Since", date);
  561. }
  562. /// \brief 当 Object 在指定时间后未被修改,则执行操作,否则返回 412
  563. void SetXCosCopySourceIfUnmodifiedSince(const std::string& date) {
  564. AddHeader("x-cos-copy-source-If-Unmodified-Since", date);
  565. }
  566. /// \brief 当 Object 的 Etag 和给定一致时,则执行操作,否则返回 412
  567. void SetXCosCopySourceIfMatch(const std::string& etag) {
  568. AddHeader("x-cos-copy-source-If-Match", etag);
  569. }
  570. /// \brief 当 Object 的 Etag 和给定不一致时,则执行操作,否则返回 412
  571. void SetXCosCopySourceIfNoneMatch(const std::string& etag) {
  572. AddHeader("x-cos-copy-source-If-None-Match", etag);
  573. }
  574. private:
  575. std::string m_upload_id;
  576. uint64_t m_part_number;
  577. };
  578. class CompleteMultiUploadReq : public ObjectReq {
  579. public:
  580. CompleteMultiUploadReq(const std::string& bucket_name,
  581. const std::string& object_name,
  582. const std::string& upload_id)
  583. : ObjectReq(bucket_name, object_name), m_upload_id(upload_id) {
  584. m_method = "POST";
  585. }
  586. virtual ~CompleteMultiUploadReq() {}
  587. std::string GetUploadId() const { return m_upload_id; }
  588. /// \brief 设置上传的ID
  589. void SetUploadId(const std::string& upload_id) { m_upload_id = upload_id; }
  590. std::vector<uint64_t> GetPartNumbers() const { return m_part_numbers; }
  591. /// \brief 设置上传的所有分块编号
  592. void SetPartNumbers(const std::vector<uint64_t>& part_numbers) {
  593. m_part_numbers.clear();
  594. m_part_numbers.insert(m_part_numbers.end(), part_numbers.begin(),
  595. part_numbers.end());
  596. }
  597. std::vector<std::string> GetEtags() const { return m_etags; }
  598. /// \brief 设置上传的所有分块etag
  599. void SetEtags(const std::vector<std::string>& etags) {
  600. m_etags.clear();
  601. m_etags.insert(m_etags.end(), etags.begin(), etags.end());
  602. }
  603. void AddPartEtagPair(uint64_t part_number, const std::string& etag) {
  604. m_part_numbers.push_back(part_number);
  605. m_etags.push_back(etag);
  606. }
  607. bool GenerateRequestBody(std::string* body) const;
  608. private:
  609. std::string m_upload_id;
  610. std::vector<uint64_t> m_part_numbers;
  611. std::vector<std::string> m_etags;
  612. };
  613. /*
  614. class PutObjectByFile : public ObjectReq {
  615. public:
  616. PutObjectByFile(const std::string& bucket_name,
  617. const std::string& object_name,
  618. const std::string& local_file_path = "")
  619. : ObjectReq(bucket_name, object_name)
  620. #if defined(_WIN32)
  621. ,
  622. mb_is_widechar_path(false)
  623. #endif
  624. {
  625. // 默认使用配置文件配置的分块大小和线程池大小
  626. m_part_size = CosSysConfig::GetUploadPartSize();
  627. m_thread_pool_size = CosSysConfig::GetUploadThreadPoolSize();
  628. mb_set_meta = false;
  629. // 默认打开当前路径下object的同名文件
  630. if (local_file_path.empty()) {
  631. m_local_file_path = "./" + object_name;
  632. } else {
  633. m_local_file_path = local_file_path;
  634. }
  635. // 分块上传默认检查crc64
  636. SetCheckCRC64(true);
  637. }
  638. virtual ~PutObjectByFile() {}
  639. void SetLocalFilePath(const std::string& local_file_path) {
  640. m_local_file_path = local_file_path;
  641. }
  642. std::string GetLocalFilePath() const { return m_local_file_path; }
  643. /// \brief 设置分块大小,若小于1M,则按1M计算;若大于5G,则按5G计算
  644. void SetPartSize(uint64_t bytes) {
  645. if (bytes <= kPartSize1M) {
  646. m_part_size = kPartSize1M;
  647. } else if (bytes >= kPartSize5G) {
  648. m_part_size = kPartSize5G;
  649. } else {
  650. m_part_size = bytes;
  651. }
  652. }
  653. /// \brief 获取分块大小
  654. uint64_t GetPartSize() const { return m_part_size; }
  655. void SetThreadPoolSize(int size) {
  656. assert(size > 0);
  657. m_thread_pool_size = size;
  658. }
  659. int GetThreadPoolSize() const { return m_thread_pool_size; }
  660. /// \brief 设置Server端加密使用的算法, 目前支持AES256
  661. void SetXCosServerSideEncryption(const std::string& str) {
  662. AddHeader("x-cos-server-side-encryption", str);
  663. }
  664. /// \brief 允许用户自定义的头部信息,将作为 Object 元数据返回.大小限制2K
  665. void SetXCosMeta(const std::string& key, const std::string& value) {
  666. mb_set_meta = true;
  667. m_xcos_meta.insert(std::pair<std::string, std::string>(key, value));
  668. }
  669. bool IsSetXCosMeta() const { return mb_set_meta; }
  670. std::map<std::string, std::string> GetXCosMeta() const { return m_xcos_meta; }
  671. /// \brief 请求参数中设置单链接限速, 参考
  672. /// https://cloud.tencent.com/document/product/436/40140
  673. void SetTrafficLimitByParam(const std::string& str) {
  674. if (GetHeader("x-cos-traffic-limit") == "") {
  675. AddParam("x-cos-traffic-limit", str);
  676. }
  677. }
  678. /// \brief 请求头中参数中设置单链接限速
  679. void SetTrafficLimitByHeader(const std::string& str) {
  680. if (GetParam("x-cos-traffic-limit") == "") {
  681. AddHeader("x-cos-traffic-limit", str);
  682. }
  683. }
  684. /// \brief 设置 Object 的存储类型
  685. ///
  686. 枚举值:MAZ_STANDARD,MAZ_STANDARD_IA,INTELLIGENT_TIERING,MAZ_INTELLIGENT_TIERING,STANDARD_IA,ARCHIVE,DEEP_ARCHIVE
  687. /// 默认值:STANDARD
  688. void SetXCosStorageClass(const std::string& storage_class) {
  689. AddHeader("x-cos-storage-class", storage_class);
  690. }
  691. /// \brief 设置Object的ACL属性
  692. void SetXCosAcl(const std::string& str) { AddHeader("x-cos-acl", str); }
  693. /// \briefCache-Control RFC 2616 中定义的缓存策略,将作为 Object 元数据保存
  694. void SetCacheControl(const std::string& str) {
  695. AddHeader("Cache-Control", str);
  696. }
  697. /// \brief Content-Disposition RFC 2616 中定义的文件名称,将作为 Object
  698. /// 元数据保存
  699. void SetContentDisposition(const std::string& str) {
  700. AddHeader("Content-Disposition", str);
  701. }
  702. /// \brief Content-Encoding RFC 2616 中定义的编码格式,将作为 Object
  703. /// 元数据保存
  704. void SetContentEncoding(const std::string& str) {
  705. AddHeader("Content-Encoding", str);
  706. }
  707. /// \brief Content-Type RFC 2616 中定义的内容类型(MIME),将作为 Object
  708. /// 元数据保存
  709. void SetContentType(const std::string& str) {
  710. AddHeader("Content-Type", str);
  711. }
  712. /// \brief Expect 当使用 Expect: 100-continue
  713. /// 时,在收到服务端确认后,才会发送请求内容
  714. void SetExpect(const std::string& str) { AddHeader("Expect", str); }
  715. /// \brief Expires RFC 2616 中定义的过期时间,将作为 Object 元数据保存
  716. void SetExpires(const std::string& str) { AddHeader("Expires", str); }
  717. void SetUploadID(const std::string& uploadid) {
  718. if (!uploadid.empty()) {
  719. m_uploadid = uploadid;
  720. }
  721. }
  722. std::string GetUploadID() const { return m_uploadid; }
  723. #if defined(_WIN32)
  724. /// \brief 如果本地文件路径为宽字符(中文,韩文等),需要调用该函数
  725. void SetWideCharPath() { mb_is_widechar_path = true; }
  726. bool IsWideCharPath() const { return mb_is_widechar_path; }
  727. std::wstring GetWideCharLocalFilePath() const {
  728. std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
  729. return converter.from_bytes(m_local_file_path);
  730. }
  731. #endif
  732. private:
  733. std::string m_local_file_path;
  734. uint64_t m_part_size;
  735. int m_thread_pool_size;
  736. std::map<std::string, std::string> m_xcos_meta;
  737. bool mb_set_meta;
  738. std::string m_uploadid;
  739. #if defined(_WIN32)
  740. bool mb_is_widechar_path; // 标识文件路径是否为宽字符
  741. #endif
  742. }; // namespace qcloud_cos
  743. */
  744. class AbortMultiUploadReq : public ObjectReq {
  745. public:
  746. AbortMultiUploadReq(const std::string& bucket_name,
  747. const std::string& object_name,
  748. const std::string& upload_id)
  749. : ObjectReq(bucket_name, object_name), m_upload_id(upload_id) {
  750. m_method = "DELETE";
  751. }
  752. virtual ~AbortMultiUploadReq() {}
  753. std::string GetUploadId() const { return m_upload_id; }
  754. /// \brief 设置本次终止上传的uploadId
  755. void SetUploadId(const std::string& upload_id) { m_upload_id = upload_id; }
  756. private:
  757. std::string m_upload_id;
  758. };
  759. class ListPartsReq : public ObjectReq {
  760. public:
  761. ListPartsReq(const std::string& bucket_name, const std::string& object_name,
  762. const std::string& upload_id)
  763. : ObjectReq(bucket_name, object_name) {
  764. m_method = "GET";
  765. AddParam("uploadId", upload_id);
  766. }
  767. virtual ~ListPartsReq() {}
  768. /// \brief 规定返回值的编码方式
  769. void SetEncodingType(const std::string& encoding_type) {
  770. AddParam("encoding-type", encoding_type);
  771. }
  772. /// \brief 单次返回最大的条目数量,若不设置,默认 1000
  773. void SetMaxParts(uint64_t max_parts) {
  774. AddParam("max-parts", StringUtil::Uint64ToString(max_parts));
  775. }
  776. /// \brief 默认以 UTF-8 二进制顺序列出条目,所有列出条目从 marker 开始
  777. void SetPartNumberMarker(const std::string& part_number_marker) {
  778. AddParam("part-number-marker", part_number_marker);
  779. }
  780. };
  781. class GetObjectACLReq : public ObjectReq {
  782. public:
  783. GetObjectACLReq(const std::string& bucket_name,
  784. const std::string& object_name)
  785. : ObjectReq(bucket_name, object_name) {
  786. m_method = "GET";
  787. AddParam("acl", "");
  788. }
  789. virtual ~GetObjectACLReq() {}
  790. };
  791. class PutObjectACLReq : public ObjectReq {
  792. public:
  793. PutObjectACLReq(const std::string& bucket_name,
  794. const std::string& object_name)
  795. : ObjectReq(bucket_name, object_name) {
  796. m_method = "PUT";
  797. AddParam("acl", "");
  798. }
  799. virtual ~PutObjectACLReq() {}
  800. /// 定义Object的ACL属性,有效值:default,private,public-read
  801. /// 默认值:private
  802. void SetXCosAcl(const std::string& str) { AddHeader("x-cos-acl", str); }
  803. /// 赋予被授权者读的权限.格式:x-cos-grant-read: id=" ",id=" ".
  804. /// 当需要给子账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<SubUin>"
  805. /// 当需要给根账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<OwnerUin>"
  806. void SetXCosGrantRead(const std::string& str) {
  807. AddHeader("x-cos-grant-read", str);
  808. }
  809. /// 赋予被授权者写的权限,格式:x-cos-grant-write: id=" ",id=" ".
  810. /// 当需要给子账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<SubUin>",
  811. /// 当需要给根账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<OwnerUin>"
  812. void SetXCosGrantWrite(const std::string& str) {
  813. AddHeader("x-cos-grant-write", str);
  814. }
  815. /// 赋予被授权者读写权限.格式:x-cos-grant-full-control: id=" ",id=" ".
  816. /// 当需要给子账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<SubUin>",
  817. /// 当需要给根账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<OwnerUin>"
  818. void SetXCosGrantFullControl(const std::string& str) {
  819. AddHeader("x-cos-grant-full-control", str);
  820. }
  821. /// Object Owner设置
  822. void SetOwner(const Owner& owner) { m_owner = owner; }
  823. /// 设置被授权者信息与权限信息
  824. void SetAccessControlList(const std::vector<Grant>& grants) {
  825. m_acl = grants;
  826. }
  827. /// 添加单个 Bucket 的授权信息
  828. void AddAccessControlList(const Grant& grant) { m_acl.push_back(grant); }
  829. /// 清空权限信息
  830. void ClearAccessControlList() {
  831. std::vector<Grant> tmp;
  832. m_acl.swap(tmp);
  833. }
  834. bool GenerateRequestBody(std::string* body) const;
  835. private:
  836. Owner m_owner;
  837. std::vector<Grant> m_acl;
  838. };
  839. class PutObjectTaggingReq : public ObjectReq {
  840. public:
  841. PutObjectTaggingReq(const std::string& bucket_name, const std::string& object_name)
  842. : ObjectReq(bucket_name, object_name) {
  843. SetMethod("PUT");
  844. AddParam("tagging", "");
  845. }
  846. void SetTagSet(std::vector<Tag>& tagset) { m_tagset = tagset; }
  847. std::vector<Tag> GetTagSet() { return m_tagset; }
  848. //清除tag规则.
  849. void ClearTagSet() {
  850. std::vector<Tag> temp;
  851. m_tagset.swap(temp);
  852. }
  853. /// 添加单个tag.
  854. void AddTag(const Tag& tag) { m_tagset.push_back(tag); }
  855. void SetVersionId(const std::string& str) {
  856. AddParam("VersionId", str);
  857. }
  858. bool GenerateRequestBody(std::string* body) const;
  859. virtual ~PutObjectTaggingReq() {}
  860. private:
  861. std::vector<Tag> m_tagset;
  862. };
  863. class GetObjectTaggingReq : public ObjectReq {
  864. public:
  865. GetObjectTaggingReq(const std::string& bucket_name, const std::string& object_name)
  866. : ObjectReq(bucket_name, object_name) {
  867. SetMethod("GET");
  868. AddParam("tagging", "");
  869. }
  870. void SetVersionId(const std::string& str) {
  871. AddParam("VersionId", str);
  872. }
  873. virtual ~GetObjectTaggingReq() {}
  874. };
  875. class DeleteObjectTaggingReq : public ObjectReq {
  876. public:
  877. DeleteObjectTaggingReq(const std::string& bucket_name, const std::string& object_name)
  878. : ObjectReq(bucket_name, object_name) {
  879. SetMethod("DELETE");
  880. AddParam("tagging", "");
  881. }
  882. void SetVersionId(const std::string& str) {
  883. AddParam("VersionId", str);
  884. }
  885. virtual ~DeleteObjectTaggingReq() {}
  886. };
  887. class PutObjectCopyReq : public ObjectReq {
  888. public:
  889. PutObjectCopyReq(const std::string& bucket_name,
  890. const std::string& object_name)
  891. : ObjectReq(bucket_name, object_name) {
  892. m_method = "PUT";
  893. }
  894. virtual ~PutObjectCopyReq() {}
  895. /// 源文件 URL 路径,可以通过 versionid 子资源指定历史版本
  896. void SetXCosCopySource(const std::string& str) {
  897. AddHeader("x-cos-copy-source", str);
  898. }
  899. /// 是否拷贝元数据,枚举值:Copy, Replaced,默认值 Copy。
  900. /// 假如标记为 Copy,忽略 Header 中的用户元数据信息直接复制;
  901. /// 假如标记为 Replaced,按 Header 信息修改元数据。
  902. /// 当目标路径和原路径一致,即用户试图修改元数据时,必须为 Replaced
  903. void SetXCosMetadataDirective(const std::string& str) {
  904. AddHeader("x-cos-metadata-directive", str);
  905. }
  906. /// 当 Object 在指定时间后被修改,则执行操作,否则返回 412。
  907. /// 可与 x-cos-copy-source-If-None-Match
  908. /// 一起使用,与其他条件联合使用返回冲突。
  909. void SetXCosCopySourceIfModifiedSince(const std::string& str) {
  910. AddHeader("x-cos-copy-source-If-Modified-Since", str);
  911. }
  912. /// 当 Object 在指定时间后未被修改,则执行操作,否则返回 412。
  913. /// 可与 x-cos-copy-source-If-Match 一起使用,与其他条件联合使用返回冲突。
  914. void SetXCosCopySourceIfUnmodifiedSince(const std::string& str) {
  915. AddHeader("x-cos-copy-source-If-Unmodified-Since", str);
  916. }
  917. /// 当 Object 的 Etag 和给定一致时,则执行操作,否则返回 412。
  918. /// 可与x-cos-copy-source-If-Unmodified-Since
  919. /// 一起使用,与其他条件联合使用返回冲突
  920. void SetXCosCopySourceIfMatch(const std::string& str) {
  921. AddHeader("x-cos-copy-source-If-Match", str);
  922. }
  923. /// 当 Object 的 Etag 和给定不一致时,则执行操作,否则返回 412。
  924. /// 可与 x-cos-copy-source-If-Modified-Since
  925. /// 一起使用,与其他条件联合使用返回冲突。
  926. void SetXCosCopySourceIfNoneMatch(const std::string& str) {
  927. AddHeader("x-cos-copy-source-If-None-Match", str);
  928. }
  929. /// 设置 Object 的存储类型
  930. /// 枚举值:MAZ_STANDARD,MAZ_STANDARD_IA,INTELLIGENT_TIERING,MAZ_INTELLIGENT_TIERING,STANDARD_IA,ARCHIVE,DEEP_ARCHIVE
  931. /// 默认值:STANDARD
  932. void SetXCosStorageClass(const std::string& storage_class) {
  933. AddHeader("x-cos-storage-class", storage_class);
  934. }
  935. /// 定义Object的ACL属性,有效值:default,private,public-read
  936. /// 默认值:private
  937. void SetXCosAcl(const std::string& str) { AddHeader("x-cos-acl", str); }
  938. /// 赋予被授权者读的权限.格式:x-cos-grant-read: id=" ",id=" ".
  939. /// 当需要给子账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<SubUin>"
  940. /// 当需要给根账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<OwnerUin>"
  941. void SetXCosGrantRead(const std::string& str) {
  942. AddHeader("x-cos-grant-read", str);
  943. }
  944. /// 赋予被授权者写的权限,格式:x-cos-grant-write: id=" ",id=" "./
  945. /// 当需要给子账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<SubUin>",
  946. /// 当需要给根账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<OwnerUin>"
  947. void SetXCosGrantWrite(const std::string& str) {
  948. AddHeader("x-cos-grant-write", str);
  949. }
  950. /// 赋予被授权者读写权限.格式:x-cos-grant-full-control: id=" ",id=" ".
  951. /// 当需要给子账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<SubUin>",
  952. /// 当需要给根账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<OwnerUin>"
  953. void SetXCosGrantFullControl(const std::string& str) {
  954. AddHeader("x-cos-grant-full-control", str);
  955. }
  956. /// 允许用户自定义的头部信息,将作为 Object 元数据返回.大小限制2K
  957. void SetXCosMeta(const std::string& key, const std::string& value) {
  958. AddHeader("x-cos-meta-" + key, value);
  959. }
  960. /// 设置Server端加密使用的算法, 目前支持AES256
  961. void SetXCosServerSideEncryption(const std::string& str) {
  962. AddHeader("x-cos-server-side-encryption", str);
  963. }
  964. };
  965. class CopyReq : public ObjectReq {
  966. public:
  967. CopyReq(const std::string& bucket_name, const std::string& object_name)
  968. : ObjectReq(bucket_name, object_name) {
  969. // 默认使用配置文件配置的分块大小和线程池大小
  970. m_part_size = CosSysConfig::GetUploadCopyPartSize();
  971. m_thread_pool_size = CosSysConfig::GetUploadThreadPoolSize();
  972. }
  973. virtual ~CopyReq() {}
  974. /// 源文件 URL 路径,可以通过 versionid 子资源指定历史版本
  975. void SetXCosCopySource(const std::string& str) {
  976. AddHeader("x-cos-copy-source", str);
  977. }
  978. /// 是否拷贝元数据,枚举值:Copy, Replaced,默认值 Copy。
  979. /// 假如标记为 Copy,忽略 Header 中的用户元数据信息直接复制;
  980. /// 假如标记为 Replaced,按 Header 信息修改元数据。
  981. /// 当目标路径和原路径一致,即用户试图修改元数据时,必须为 Replaced
  982. void SetXCosMetadataDirective(const std::string& str) {
  983. AddHeader("x-cos-metadata-directive", str);
  984. }
  985. /// 当 Object 在指定时间后被修改,则执行操作,否则返回 412。
  986. /// 可与 x-cos-copy-source-If-None-Match
  987. /// 一起使用,与其他条件联合使用返回冲突。
  988. void SetXCosCopySourceIfModifiedSince(const std::string& str) {
  989. AddHeader("x-cos-copy-source-If-Modified-Since", str);
  990. }
  991. /// 当 Object 在指定时间后未被修改,则执行操作,否则返回 412。
  992. /// 可与 x-cos-copy-source-If-Match 一起使用,与其他条件联合使用返回冲突。
  993. void SetXCosCopySourceIfUnmodifiedSince(const std::string& str) {
  994. AddHeader("x-cos-copy-source-If-Unmodified-Since", str);
  995. }
  996. /// 当 Object 的 Etag 和给定一致时,则执行操作,否则返回 412。
  997. /// 可与x-cos-copy-source-If-Unmodified-Since
  998. /// 一起使用,与其他条件联合使用返回冲突
  999. void SetXCosCopySourceIfMatch(const std::string& str) {
  1000. AddHeader("x-cos-copy-source-If-Match", str);
  1001. }
  1002. /// 当 Object 的 Etag 和给定不一致时,则执行操作,否则返回 412。
  1003. /// 可与 x-cos-copy-source-If-Modified-Since
  1004. /// 一起使用,与其他条件联合使用返回冲突。
  1005. void SetXCosCopySourceIfNoneMatch(const std::string& str) {
  1006. AddHeader("x-cos-copy-source-If-None-Match", str);
  1007. }
  1008. /// 设置 Object 的存储类型
  1009. /// 枚举值:MAZ_STANDARD,MAZ_STANDARD_IA,INTELLIGENT_TIERING,MAZ_INTELLIGENT_TIERING,STANDARD_IA,ARCHIVE,DEEP_ARCHIVE
  1010. /// 默认值:STANDARD
  1011. void SetXCosStorageClass(const std::string& storage_class) {
  1012. AddHeader("x-cos-storage-class", storage_class);
  1013. }
  1014. /// 定义Object的ACL属性,有效值:default,private,public-read
  1015. /// 默认值:private
  1016. void SetXCosAcl(const std::string& str) { AddHeader("x-cos-acl", str); }
  1017. /// 赋予被授权者读的权限.格式:x-cos-grant-read: id=" ",id=" ".
  1018. /// 当需要给子账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<SubUin>"
  1019. /// 当需要给根账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<OwnerUin>"
  1020. void SetXCosGrantRead(const std::string& str) {
  1021. AddHeader("x-cos-grant-read", str);
  1022. }
  1023. /// 赋予被授权者写的权限,格式:x-cos-grant-write: id=" ",id=" "./
  1024. /// 当需要给子账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<SubUin>",
  1025. /// 当需要给根账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<OwnerUin>"
  1026. void SetXCosGrantWrite(const std::string& str) {
  1027. AddHeader("x-cos-grant-write", str);
  1028. }
  1029. /// 赋予被授权者读写权限.格式:x-cos-grant-full-control: id=" ",id=" ".
  1030. /// 当需要给子账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<SubUin>",
  1031. /// 当需要给根账户授权时,id="qcs::cam::uin/<OwnerUin>:uin/<OwnerUin>"
  1032. void SetXCosGrantFullControl(const std::string& str) {
  1033. AddHeader("x-cos-grant-full-control", str);
  1034. }
  1035. /// 允许用户自定义的头部信息,将作为 Object 元数据返回.大小限制2K
  1036. void SetXCosMeta(const std::string& key, const std::string& value) {
  1037. AddHeader("x-cos-meta-" + key, value);
  1038. }
  1039. std::map<std::string, std::string> GetPartCopyHeader() const;
  1040. std::map<std::string, std::string> GetInitHeader() const;
  1041. // 设置分块大小,若小于1M,则按1M计算;若大于5G,则按5G计算
  1042. void SetPartSize(uint64_t bytes) {
  1043. if (bytes <= kPartSize1M) {
  1044. m_part_size = kPartSize1M;
  1045. } else if (bytes >= kPartSize5G) {
  1046. m_part_size = kPartSize5G;
  1047. } else {
  1048. m_part_size = bytes;
  1049. }
  1050. }
  1051. // 获取分块大小
  1052. uint64_t GetPartSize() const { return m_part_size; }
  1053. void SetThreadPoolSize(unsigned size) {
  1054. assert(size > 0);
  1055. m_thread_pool_size = size;
  1056. }
  1057. unsigned GetThreadPoolSize() const { return m_thread_pool_size; }
  1058. private:
  1059. uint64_t m_part_size;
  1060. unsigned m_thread_pool_size;
  1061. };
  1062. class PostObjectRestoreReq : public ObjectReq {
  1063. public:
  1064. PostObjectRestoreReq(const std::string& bucket_name,
  1065. const std::string& object_name)
  1066. : ObjectReq(bucket_name, object_name), m_expiry_days(0),
  1067. m_tier("Standard") {
  1068. m_method = "POST";
  1069. AddParam("restore", "");
  1070. }
  1071. PostObjectRestoreReq(const std::string& bucket_name,
  1072. const std::string& object_name,
  1073. const std::string& version_id)
  1074. : ObjectReq(bucket_name, object_name), m_expiry_days(0),
  1075. m_tier("Standard") {
  1076. m_method = "POST";
  1077. AddParam("restore", "");
  1078. if (!version_id.empty()) {
  1079. AddHeader("x-cos-version-id", version_id);
  1080. }
  1081. }
  1082. PostObjectRestoreReq(const std::string& bucket_name,
  1083. const std::string& object_name,
  1084. const std::string& version_id, const std::string& tier)
  1085. : ObjectReq(bucket_name, object_name), m_expiry_days(0), m_tier(tier) {
  1086. m_method = "POST";
  1087. AddParam("restore", "");
  1088. if (!version_id.empty()) {
  1089. AddHeader("x-cos-version-id", version_id);
  1090. }
  1091. }
  1092. virtual ~PostObjectRestoreReq() {}
  1093. /// \brief 设置临时副本的过期时间
  1094. void SetExiryDays(uint64_t days) { m_expiry_days = days; }
  1095. /// \brief 指定版本号的对象
  1096. void SetXCosVersionId(const std::string& version_id) {
  1097. AddHeader("x-cos-version-id", version_id);
  1098. }
  1099. // \brief 具体复原过程类型,枚举值: Expedited ,Standard
  1100. // ,Bulk;默认值:Standard
  1101. void SetTier(const std::string& tier) { m_tier = tier; }
  1102. bool GenerateRequestBody(std::string* body) const;
  1103. private:
  1104. uint64_t m_expiry_days;
  1105. std::string m_tier;
  1106. };
  1107. class GeneratePresignedUrlReq : public ObjectReq {
  1108. public:
  1109. GeneratePresignedUrlReq(const std::string& bucket_name,
  1110. const std::string& object_name,
  1111. HTTP_METHOD http_method)
  1112. : ObjectReq(bucket_name, object_name), m_start_time_in_s(0),
  1113. m_expired_time_in_s(0), m_use_https(true) {
  1114. m_path = "/" + object_name;
  1115. m_method = StringUtil::HttpMethodToString(http_method);
  1116. }
  1117. virtual ~GeneratePresignedUrlReq() {}
  1118. uint64_t GetStartTimeInSec() const { return m_start_time_in_s; }
  1119. uint64_t GetExpiredTimeInSec() const { return m_expired_time_in_s; }
  1120. void SetStartTimeInSec(uint64_t start_time) {
  1121. m_start_time_in_s = start_time;
  1122. }
  1123. void SetExpiredTimeInSec(uint64_t expired_time) {
  1124. m_expired_time_in_s = expired_time;
  1125. }
  1126. /// \brief 是否生成https链接,默认为true
  1127. void SetUseHttps(bool use_https) { m_use_https = use_https; }
  1128. bool UseHttps() const { return m_use_https; }
  1129. private:
  1130. uint64_t m_start_time_in_s;
  1131. uint64_t m_expired_time_in_s;
  1132. bool m_use_https; // 是否启用https
  1133. };
  1134. class OptionsObjectReq : public ObjectReq {
  1135. public:
  1136. OptionsObjectReq(const std::string& bucket_name,
  1137. const std::string& object_name)
  1138. : ObjectReq(bucket_name, object_name) {
  1139. m_method = "OPTIONS";
  1140. m_path = "/" + object_name;
  1141. }
  1142. virtual ~OptionsObjectReq() {}
  1143. /// \brief
  1144. /// 添加Origin头部,参考官网https://cloud.tencent.com/document/product/436/8288
  1145. void SetOrigin(const std::string& origin) { AddHeader("Origin", origin); }
  1146. /// \brief 添加跨域访问的请求HTTP方法的头部
  1147. void SetAccessControlRequestMethod(const std::string& method) {
  1148. AddHeader("Access-Control-Request-Method", method);
  1149. }
  1150. /// \brief 添加跨域访问的请求头部的头部
  1151. void SetAccessControlRequestHeaders(const std::string& headers) {
  1152. AddHeader("Access-Control-Request-Headers", headers);
  1153. }
  1154. };
  1155. class SelectObjectContentReq : public ObjectReq {
  1156. public:
  1157. /// \brief
  1158. /// input_file_type: 待检索对象的格式为csv或者json
  1159. /// out_file_type: 输出格式为csv或者json
  1160. SelectObjectContentReq(const std::string& bucket_name,
  1161. const std::string& object_name,
  1162. int input_file_type = CSV,
  1163. int input_compress_type = COMPRESS_NONE,
  1164. int out_file_type = CSV)
  1165. : ObjectReq(bucket_name, object_name), m_input_file_type(input_file_type),
  1166. m_input_compress_type(input_compress_type),
  1167. m_output_file_type(out_file_type), m_expression_type("SQL"),
  1168. m_request_progress(false) {
  1169. m_method = "POST";
  1170. m_path = "/" + object_name;
  1171. AddParam("select", "");
  1172. AddParam("select-type", "2");
  1173. GenDefaultInputSerialization();
  1174. GenDefaultOutputSerialization();
  1175. }
  1176. virtual ~SelectObjectContentReq() {}
  1177. /// \brief
  1178. /// 设置SQL表达式,参考官网https://cloud.tencent.com/document/product/436/37641
  1179. void SetSqlExpression(const std::string& sql_expression) {
  1180. m_sql_expression = sql_expression;
  1181. }
  1182. /// \brief 设置SQL表达式类型,目前只支持SQL
  1183. void SetExpressionType(const std::string& expression_type) {
  1184. m_sql_expression = expression_type;
  1185. }
  1186. /// \brief input描述待检索对象的格式
  1187. void SetInputSerialization(const std::string& input) {
  1188. m_inputserialization = input;
  1189. }
  1190. /// \brief output描述检索结果的输出格式
  1191. void SetOutputSerialization(const std::string& output) {
  1192. m_outputserialization = output;
  1193. }
  1194. /// \brief 是否需要返回查询进度 QueryProgress 信息,如果选中 COS Select
  1195. /// 将周期性返回查询进度
  1196. void SetRequestProgress(bool progress) { m_request_progress = progress; }
  1197. bool GenerateRequestBody(std::string* body) const;
  1198. private:
  1199. /// \brief 根据m_input_csv产生默认的待检索对象的格式
  1200. void GenDefaultInputSerialization() {
  1201. std::ostringstream input_xml;
  1202. input_xml << "<InputSerialization>";
  1203. if (m_input_compress_type == COMPRESS_GZIP) {
  1204. input_xml << "<CompressionType>GZIP</CompressionType>";
  1205. } else if (m_input_compress_type == COMPRESS_BZIP2) {
  1206. input_xml << "<CompressionType>BZIP2</CompressionType>";
  1207. } else {
  1208. input_xml << "<CompressionType>NONE</CompressionType>";
  1209. }
  1210. if (m_input_file_type == CSV) {
  1211. input_xml << "<CSV>";
  1212. input_xml << "<FileHeaderInfo>NONE</FileHeaderInfo>";
  1213. input_xml << "<RecordDelimiter>\\n</RecordDelimiter>";
  1214. input_xml << "<FieldDelimiter>,</FieldDelimiter>";
  1215. input_xml << "<QuoteCharacter>\"</QuoteCharacter>";
  1216. input_xml << "<QuoteEscapeCharacter>\"</QuoteEscapeCharacter>";
  1217. input_xml << "<Comments>#</Comments>";
  1218. input_xml
  1219. << "<AllowQuotedRecordDelimiter>FALSE</AllowQuotedRecordDelimiter>";
  1220. input_xml << "</CSV>";
  1221. input_xml << "</InputSerialization>";
  1222. } else if (m_input_file_type == JSON) {
  1223. input_xml << "<JSON>";
  1224. input_xml << "<Type>DOCUMENT</Type>";
  1225. input_xml << "</JSON>";
  1226. input_xml << "</InputSerialization>";
  1227. }
  1228. m_inputserialization = input_xml.str();
  1229. }
  1230. /// \brief 根据m_out_csv产生默认的检索结果的输出格式
  1231. void GenDefaultOutputSerialization() {
  1232. std::ostringstream output_xml;
  1233. if ((m_output_file_type == CSV)) {
  1234. output_xml << "<OutputSerialization>";
  1235. output_xml << "<CSV>";
  1236. output_xml << "<QuoteFields>ASNEEDED</QuoteFields>";
  1237. output_xml << "<RecordDelimiter>\\n</RecordDelimiter>";
  1238. output_xml << "<FieldDelimiter>,</FieldDelimiter>";
  1239. output_xml << "<QuoteCharacter>\"</QuoteCharacter>";
  1240. output_xml << "<QuoteEscapeCharacter>\"</QuoteEscapeCharacter>";
  1241. output_xml << "</CSV>";
  1242. output_xml << "</OutputSerialization>";
  1243. } else if (m_output_file_type == JSON) {
  1244. output_xml << "<OutputSerialization>";
  1245. output_xml << "<JSON>";
  1246. output_xml << "<RecordDelimiter>\\n</RecordDelimiter>";
  1247. output_xml << "</JSON>";
  1248. output_xml << "</OutputSerialization>";
  1249. }
  1250. m_outputserialization = output_xml.str();
  1251. }
  1252. int m_input_file_type; // csv or json
  1253. int m_input_compress_type; // NONE,GZIP,BZIP2
  1254. int m_output_file_type; // csv or json
  1255. std::string m_sql_expression; // like "Select * from COSObject"
  1256. std::string m_expression_type; // default is "SQL"
  1257. std::string m_inputserialization; // xml format
  1258. std::string m_outputserialization; // xml format
  1259. bool m_request_progress; // default is false
  1260. };
  1261. class AppendObjectReq : public PutObjectByStreamReq {
  1262. public:
  1263. AppendObjectReq(const std::string& bucket_name,
  1264. const std::string& object_name, std::istream& in_stream)
  1265. : PutObjectByStreamReq(bucket_name, object_name, in_stream) {
  1266. m_method = "POST";
  1267. m_path = "/" + object_name;
  1268. AddParam("append", "");
  1269. AddParam("position", "0");
  1270. // append返回的ETag不是body的MD5,不需要检查Etag
  1271. TurnOffCheckETag();
  1272. }
  1273. virtual ~AppendObjectReq() {}
  1274. /// \brief
  1275. /// 添加position参数,参考官网https://cloud.tencent.com/document/product/436/7741
  1276. void SetPosition(const std::string& position) {
  1277. AddParam("position", position);
  1278. }
  1279. };
  1280. /// \brief: 创建直播通道请求
  1281. class PutLiveChannelReq : public ObjectReq {
  1282. public:
  1283. /// \brief
  1284. /// channel_name: 通道名
  1285. PutLiveChannelReq(const std::string& bucket_name,
  1286. const std::string& channel_name)
  1287. : ObjectReq(bucket_name, channel_name) {
  1288. m_method = "PUT";
  1289. m_path = "/" + channel_name;
  1290. AddParam("live", "");
  1291. m_expire_sec = 3600;
  1292. }
  1293. virtual ~PutLiveChannelReq() {}
  1294. void SetLiveChannelConfig(const LiveChannelConfiguration& config) {
  1295. m_config = config;
  1296. }
  1297. /// brief: 设置url参数
  1298. void SetUrlParams(const std::map<std::string, std::string>& url_params) {
  1299. m_url_params = url_params;
  1300. }
  1301. const std::map<std::string, std::string>& GetUrlParams() const {
  1302. return m_url_params;
  1303. }
  1304. /// brief: 设置推流签名过期时间
  1305. void SetExpire(int expire) { m_expire_sec = expire; }
  1306. int GetExpire() const { return m_expire_sec; }
  1307. bool GenerateRequestBody(std::string* body) const;
  1308. private:
  1309. LiveChannelConfiguration m_config;
  1310. std::map<std::string, std::string> m_url_params;
  1311. int m_expire_sec;
  1312. };
  1313. /// \brief: 启用或者禁用直播通道请求
  1314. class PutLiveChannelSwitchReq : public ObjectReq {
  1315. public:
  1316. /// \brief
  1317. /// channel_name: 通道名
  1318. PutLiveChannelSwitchReq(const std::string& bucket_name,
  1319. const std::string& channel_name)
  1320. : ObjectReq(bucket_name, channel_name) {
  1321. m_method = "PUT";
  1322. m_path = "/" + channel_name;
  1323. AddParam("live", "");
  1324. AddParam("switch", "enabled");
  1325. }
  1326. virtual ~PutLiveChannelSwitchReq() {}
  1327. /// \brief 启用通道
  1328. void SetEnabled() { AddParam("switch", "enabled"); }
  1329. /// \brief 禁用通道
  1330. void SetDisabled() { AddParam("switch", "disabled"); }
  1331. };
  1332. /// \brief: 获取直播通道配置
  1333. class GetLiveChannelReq : public ObjectReq {
  1334. public:
  1335. GetLiveChannelReq(const std::string& bucket_name,
  1336. const std::string& channel_name)
  1337. : ObjectReq(bucket_name, channel_name) {
  1338. m_method = "GET";
  1339. m_path = "/" + channel_name;
  1340. AddParam("live", "");
  1341. }
  1342. virtual ~GetLiveChannelReq() {}
  1343. };
  1344. /// \brief: 获取直播通道推流历史
  1345. class GetLiveChannelHistoryReq : public ObjectReq {
  1346. public:
  1347. GetLiveChannelHistoryReq(const std::string& bucket_name,
  1348. const std::string& channel_name)
  1349. : ObjectReq(bucket_name, channel_name) {
  1350. m_method = "GET";
  1351. m_path = "/" + channel_name;
  1352. AddParam("live", "");
  1353. AddParam("comp", "history");
  1354. }
  1355. virtual ~GetLiveChannelHistoryReq() {}
  1356. };
  1357. /// \brief: 获取直播通道推流状态
  1358. class GetLiveChannelStatusReq : public ObjectReq {
  1359. public:
  1360. GetLiveChannelStatusReq(const std::string& bucket_name,
  1361. const std::string& channel_name)
  1362. : ObjectReq(bucket_name, channel_name) {
  1363. m_method = "GET";
  1364. m_path = "/" + channel_name;
  1365. AddParam("live", "");
  1366. AddParam("comp", "status");
  1367. }
  1368. virtual ~GetLiveChannelStatusReq() {}
  1369. };
  1370. /// \brief: 删除直播通道
  1371. class DeleteLiveChannelReq : public ObjectReq {
  1372. public:
  1373. DeleteLiveChannelReq(const std::string& bucket_name,
  1374. const std::string& channel_name)
  1375. : ObjectReq(bucket_name, channel_name) {
  1376. m_method = "DELETE";
  1377. m_path = "/" + channel_name;
  1378. AddParam("live", "");
  1379. }
  1380. virtual ~DeleteLiveChannelReq() {}
  1381. };
  1382. /// \brief: 查询指定通道在指定时间段推流生成的播放列表
  1383. class GetLiveChannelVodPlaylistReq : public ObjectReq {
  1384. public:
  1385. GetLiveChannelVodPlaylistReq(const std::string& bucket_name,
  1386. const std::string& channel_name)
  1387. : ObjectReq(bucket_name, channel_name) {
  1388. m_method = "GET";
  1389. m_path = "/" + channel_name;
  1390. AddParam("vod", "");
  1391. }
  1392. virtual ~GetLiveChannelVodPlaylistReq() {}
  1393. /// \brief time格式为unix时间戳,必须调用该接口设置开始时间和结束时间
  1394. void SetTime(uint64_t starttime, uint64_t endtime) {
  1395. AddParam("starttime", StringUtil::Uint64ToString(starttime));
  1396. AddParam("endtime", StringUtil::Uint64ToString(endtime));
  1397. }
  1398. };
  1399. /// \brief: 为指定通道生成一个可供点播例用的播放列表
  1400. class PostLiveChannelVodPlaylistReq : public ObjectReq {
  1401. public:
  1402. PostLiveChannelVodPlaylistReq(const std::string& bucket_name,
  1403. const std::string& channel_name)
  1404. : ObjectReq(bucket_name, channel_name) {
  1405. m_method = "POST";
  1406. m_path = "/" + channel_name;
  1407. AddParam("vod", "");
  1408. }
  1409. virtual ~PostLiveChannelVodPlaylistReq() {}
  1410. /// \brief time格式为unix时间戳,必须调用该接口设置开始时间和结束时间
  1411. void SetTime(uint64_t starttime, uint64_t endtime) {
  1412. AddParam("starttime", StringUtil::Uint64ToString(starttime));
  1413. AddParam("endtime", StringUtil::Uint64ToString(endtime));
  1414. }
  1415. /// \brief 必须调用该接口设置播放列表名称
  1416. void SetPlaylistName(const std::string& playlist_name) {
  1417. m_path.append("/" + playlist_name);
  1418. }
  1419. };
  1420. /* Multithread接口 */
  1421. class MultiPutObjectReq : public PutObjectByFileReq {
  1422. public:
  1423. MultiPutObjectReq(const std::string& bucket_name,
  1424. const std::string& object_name,
  1425. const std::string& local_file_path)
  1426. : PutObjectByFileReq(bucket_name, object_name, local_file_path) {
  1427. SetCheckCRC64(true);
  1428. }
  1429. virtual ~MultiPutObjectReq() {}
  1430. };
  1431. class MultiGetObjectReq : public GetObjectByFileReq {
  1432. public:
  1433. MultiGetObjectReq(const std::string& bucket_name,
  1434. const std::string& object_name,
  1435. const std::string& local_file_path)
  1436. : GetObjectByFileReq(bucket_name, object_name, local_file_path) {}
  1437. virtual ~MultiGetObjectReq() {}
  1438. };
  1439. class PutObjectResumableSingleSyncReq : public PutObjectByFileReq {
  1440. public:
  1441. PutObjectResumableSingleSyncReq(const std::string& bucket_name,
  1442. const std::string& object_name,
  1443. const std::string& local_file_path)
  1444. : PutObjectByFileReq(bucket_name, object_name, local_file_path) {
  1445. SetCheckCRC64(true);
  1446. }
  1447. virtual ~PutObjectResumableSingleSyncReq() {}
  1448. };
  1449. /* Async接口 */
  1450. #if 0
  1451. class AsyncReq {
  1452. public:
  1453. AsyncReq(const std::string& bucket_name, const std::string& object_name,
  1454. const std::string& local_file_path, bool wide_char_path = false)
  1455. : m_bucket_name(bucket_name), m_object_name(object_name),
  1456. m_local_file_path(local_file_path) {}
  1457. virtual ~AsyncReq() {}
  1458. std::string GetBucketName() const { return m_bucket_name; }
  1459. std::string GetObjectName() const { return m_object_name; }
  1460. std::string GetLocalFilePath() const { return m_local_file_path; }
  1461. virtual uint64_t GetLocalFileSize() const { return 0; }
  1462. #if defined(_WIN32)
  1463. /// \brief 如果本地文件路径为宽字符(中文,韩文等),需要调用该函数
  1464. void SetWideCharPath() { mb_is_widechar_path = true; }
  1465. bool IsWideCharPath() const { return mb_is_widechar_path; }
  1466. /// \brief 获取宽字符路径
  1467. std::wstring GetWideCharLocalFilePath() const {
  1468. std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
  1469. return converter.from_bytes(m_local_file_path);
  1470. }
  1471. #endif
  1472. /// @brief 设置进度回调函数
  1473. void SetTransferProgressCallback(const TransferProgressCallback& process_cb) {
  1474. m_progress_cb = process_cb;
  1475. }
  1476. /// @brief 设置完成回调函数
  1477. void SetDoneCallback(const DoneCallback& done_cb) { m_done_cb = done_cb; }
  1478. /// @brief 设置回调私有数据
  1479. void SetUserData(void* user_data) { m_user_data = user_data; }
  1480. TransferProgressCallback GetTransferProgressCallback() const {
  1481. return m_progress_cb;
  1482. }
  1483. DoneCallback GetDoneCallback() const { return m_done_cb; }
  1484. void* GetUserData() const { return m_user_data; }
  1485. protected:
  1486. std::string m_bucket_name;
  1487. std::string m_object_name;
  1488. bool mb_is_widechar_path;
  1489. std::string m_local_file_path;
  1490. TransferProgressCallback m_progress_cb; // 进度回调
  1491. DoneCallback m_done_cb; // 完成回调
  1492. void* m_user_data;
  1493. };
  1494. #endif
  1495. class AsyncPutObjectReq : public PutObjectByFileReq {
  1496. public:
  1497. AsyncPutObjectReq(const std::string& bucket_name, const std::string& object_name,
  1498. const std::string& local_file_path)
  1499. : PutObjectByFileReq(bucket_name, object_name, local_file_path) {}
  1500. virtual ~AsyncPutObjectReq() {}
  1501. };
  1502. class AsyncPutObjectByStreamReq : public PutObjectByStreamReq {
  1503. public:
  1504. AsyncPutObjectByStreamReq(const std::string& bucket_name, const std::string& object_name,
  1505. std::istream& in_stream)
  1506. : PutObjectByStreamReq(bucket_name, object_name, in_stream) {}
  1507. virtual ~AsyncPutObjectByStreamReq() {}
  1508. };
  1509. class AsyncMultiPutObjectReq : public PutObjectByFileReq {
  1510. public:
  1511. AsyncMultiPutObjectReq(const std::string& bucket_name, const std::string& object_name,
  1512. const std::string& local_file_path)
  1513. : PutObjectByFileReq(bucket_name, object_name, local_file_path) {}
  1514. virtual ~AsyncMultiPutObjectReq() {}
  1515. };
  1516. class AsyncGetObjectReq : public GetObjectByFileReq {
  1517. public:
  1518. AsyncGetObjectReq(const std::string& bucket_name, const std::string& object_name,
  1519. const std::string& local_file_path)
  1520. : GetObjectByFileReq(bucket_name, object_name, local_file_path) {}
  1521. virtual ~AsyncGetObjectReq() {}
  1522. };
  1523. class AsyncMultiGetObjectReq : public GetObjectByFileReq {
  1524. public:
  1525. AsyncMultiGetObjectReq(const std::string& bucket_name, const std::string& object_name,
  1526. const std::string& local_file_path)
  1527. : GetObjectByFileReq(bucket_name, object_name, local_file_path) {}
  1528. virtual ~AsyncMultiGetObjectReq() {}
  1529. };
  1530. /* 批量及目录操作接口 */
  1531. class PutObjectsByDirectoryReq : public PutObjectReq {
  1532. public:
  1533. PutObjectsByDirectoryReq(const std::string& bucket_name,
  1534. const std::string& directory_name)
  1535. : m_bucket_name(bucket_name), m_directory_name(directory_name),
  1536. m_cos_path("") {
  1537. if (!IllegalIntercept::CheckBucket(bucket_name)) {
  1538. throw std::invalid_argument("Invalid bucket_name argument :" + bucket_name);
  1539. }
  1540. m_need_compute_contentmd5 = true; // 默认打开
  1541. }
  1542. PutObjectsByDirectoryReq(const std::string& bucket_name,
  1543. const std::string& directory_name,
  1544. const std::string& cos_path)
  1545. : m_bucket_name(bucket_name), m_directory_name(directory_name),
  1546. m_cos_path(cos_path) {
  1547. if (!IllegalIntercept::CheckBucket(bucket_name)) {
  1548. throw std::invalid_argument("Invalid bucket_name argument :" + bucket_name);
  1549. }
  1550. m_need_compute_contentmd5 = true; // 默认打开
  1551. }
  1552. virtual ~PutObjectsByDirectoryReq() {}
  1553. std::string GetBucketName() const { return m_bucket_name; }
  1554. std::string GetDirectoryName() const { return m_directory_name; }
  1555. // 设置上传到cos的路径
  1556. void SetCosPath(const std::string& cos_path) { m_cos_path = cos_path; }
  1557. std::string GetCosPath() const { return m_cos_path; }
  1558. void TurnOnComputeConentMd5() { m_need_compute_contentmd5 = true; }
  1559. void TurnOffComputeConentMd5() { m_need_compute_contentmd5 = false; }
  1560. bool ShouldComputeContentMd5() const { return m_need_compute_contentmd5; }
  1561. private:
  1562. // TODO 支持前缀和后缀
  1563. std::string m_bucket_name;
  1564. std::string m_directory_name;
  1565. std::string m_cos_path;
  1566. bool m_need_compute_contentmd5;
  1567. };
  1568. class PutDirectoryReq : public PutObjectReq {
  1569. public:
  1570. PutDirectoryReq(const std::string& bucket_name,
  1571. const std::string& directory_name)
  1572. : PutObjectReq(bucket_name, directory_name) {}
  1573. virtual ~PutDirectoryReq() {}
  1574. };
  1575. class MoveObjectReq : public ObjectReq {
  1576. public:
  1577. MoveObjectReq(const std::string& bucket_name,
  1578. const std::string& src_object_name,
  1579. const std::string& dst_object_name)
  1580. : m_bucket_name(bucket_name), m_src_object_name(src_object_name),
  1581. m_dst_object_name(dst_object_name) {
  1582. if (!IllegalIntercept::CheckBucket(bucket_name)) {
  1583. throw std::invalid_argument("Invalid bucket_name argument :" + bucket_name);
  1584. }
  1585. }
  1586. virtual ~MoveObjectReq() {}
  1587. std::string GetBucketName() const { return m_bucket_name; }
  1588. std::string GetSrcObjectName() const { return m_src_object_name; }
  1589. std::string GetDstObjectName() const { return m_dst_object_name; }
  1590. private:
  1591. std::string m_bucket_name;
  1592. std::string m_src_object_name;
  1593. std::string m_dst_object_name;
  1594. };
  1595. class DeleteObjectsByPrefixReq {
  1596. public:
  1597. DeleteObjectsByPrefixReq(const std::string& bucket_name,
  1598. const std::string& prefix)
  1599. : m_bucket_name(bucket_name), m_prefix(prefix) {
  1600. if (!IllegalIntercept::CheckBucket(bucket_name)) {
  1601. throw std::invalid_argument("Invalid bucket_name argument :" + bucket_name);
  1602. }
  1603. }
  1604. virtual ~DeleteObjectsByPrefixReq() {}
  1605. std::string GetBucketName() const { return m_bucket_name; }
  1606. std::string GetPrefix() const { return m_prefix; }
  1607. private:
  1608. std::string m_bucket_name;
  1609. std::string m_prefix;
  1610. };
  1611. } // namespace qcloud_cos