object_resp.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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. #ifndef OBJECT_RESP_H
  8. #define OBJECT_RESP_H
  9. #pragma once
  10. #include <iostream>
  11. #include <vector>
  12. #include "cos_config.h"
  13. #include "cos_params.h"
  14. #include "response/base_resp.h"
  15. namespace qcloud_cos {
  16. class GetObjectResp : public BaseResp {
  17. public:
  18. /// \brief 获取object type, 表示object是否可以被追加上传,枚举值:normal 或者
  19. /// appendable
  20. std::string GetXCosObjectType() const { return m_x_cos_object_type; }
  21. void SetXCosObjectType(const std::string& x_cos_object_type) {
  22. m_x_cos_object_type = x_cos_object_type;
  23. }
  24. /// \brief 获取自定义的元数据
  25. std::map<std::string, std::string> GetXCosMetas() const {
  26. return m_x_cos_metas;
  27. }
  28. /// \brief 获取自定义的元数据
  29. std::string GetXCosMeta(const std::string& key) const {
  30. std::map<std::string, std::string>::const_iterator itr =
  31. m_x_cos_metas.find(key);
  32. if (itr != m_x_cos_metas.end()) {
  33. return itr->second;
  34. }
  35. return "";
  36. }
  37. void SetXCosMeta(const std::string& key, const std::string& value) {
  38. m_x_cos_metas[key] = value;
  39. }
  40. /// \brief Server端加密使用的算法
  41. std::string GetXCosServerSideEncryption() const {
  42. return GetHeader("x-cos-server-side-encryption");
  43. }
  44. void ParseFromHeaders(const std::map<std::string, std::string>& headers);
  45. protected:
  46. GetObjectResp() {}
  47. GetObjectResp(const GetObjectResp& rhs)
  48. : BaseResp(rhs),
  49. m_x_cos_object_type(rhs.m_x_cos_object_type),
  50. m_x_cos_metas(rhs.m_x_cos_metas) {}
  51. GetObjectResp& operator=(const GetObjectResp& rhs) {
  52. if (&rhs != this) {
  53. BaseResp::operator=(rhs);
  54. m_x_cos_object_type = rhs.m_x_cos_object_type;
  55. m_x_cos_metas = rhs.m_x_cos_metas;
  56. }
  57. return *this;
  58. }
  59. virtual ~GetObjectResp() {}
  60. private:
  61. std::string m_x_cos_object_type;
  62. std::map<std::string, std::string> m_x_cos_metas;
  63. };
  64. class GetObjectByStreamResp : public GetObjectResp {
  65. public:
  66. GetObjectByStreamResp() {}
  67. virtual ~GetObjectByStreamResp() {}
  68. };
  69. class GetObjectByFileResp : public GetObjectResp {
  70. public:
  71. GetObjectByFileResp() {}
  72. virtual ~GetObjectByFileResp() {}
  73. };
  74. class PutObjectResp : virtual public BaseResp {
  75. protected:
  76. PutObjectResp() {}
  77. virtual ~PutObjectResp() {}
  78. public:
  79. /// 获取Object的版本号, 如果Bucket未开启多版本, 返回空字符串
  80. std::string GetVersionId() const { return GetHeader("x-cos-version-id"); }
  81. /// Server端加密使用的算法
  82. std::string GetXCosServerSideEncryption() const {
  83. return GetHeader("x-cos-server-side-encryption");
  84. }
  85. };
  86. class PutObjectByStreamResp : public PutObjectResp {
  87. public:
  88. PutObjectByStreamResp() {}
  89. virtual ~PutObjectByStreamResp() {}
  90. };
  91. class PutObjectByFileResp : public PutObjectResp {
  92. public:
  93. PutObjectByFileResp() {}
  94. virtual ~PutObjectByFileResp() {}
  95. };
  96. class DeleteObjectResp : public BaseResp {
  97. public:
  98. DeleteObjectResp() {}
  99. virtual ~DeleteObjectResp() {}
  100. };
  101. class DeleteObjectsResp : public BaseResp {
  102. public:
  103. DeleteObjectsResp() {}
  104. virtual ~DeleteObjectsResp() {}
  105. std::vector<DeletedInfo> GetDeletedInfos() const { return m_deleted_infos; }
  106. std::vector<ErrorInfo> GetErrorMsgs() const { return m_error_infos; }
  107. virtual bool ParseFromXmlString(const std::string& body);
  108. private:
  109. std::vector<DeletedInfo> m_deleted_infos;
  110. std::vector<ErrorInfo> m_error_infos;
  111. };
  112. class HeadObjectResp : public BaseResp {
  113. public:
  114. HeadObjectResp() {}
  115. virtual ~HeadObjectResp() {}
  116. /// \brief 获取object type, 表示object是否可以被追加上传,枚举值:normal 或者
  117. /// appendable
  118. std::string GetXCosObjectType() const { return m_x_cos_object_type; }
  119. void SetXCosObjectType(const std::string& x_cos_object_type) {
  120. m_x_cos_object_type = x_cos_object_type;
  121. }
  122. /// \brief 获取自定义的元数据
  123. std::map<std::string, std::string> GetXCosMetas() const {
  124. return m_x_cos_metas;
  125. }
  126. /// \brief 获取自定义的元数据
  127. std::string GetXCosMeta(const std::string& key) const {
  128. std::map<std::string, std::string>::const_iterator itr =
  129. m_x_cos_metas.find(key);
  130. if (itr != m_x_cos_metas.end()) {
  131. return itr->second;
  132. }
  133. return "";
  134. }
  135. void SetXCosMeta(const std::string& key, const std::string& value) {
  136. m_x_cos_metas[key] = value;
  137. }
  138. /// \brief 获得 archive 类型对象的当前恢复状态
  139. std::string GetXCosRestore() const { return GetHeader("x-cos-restore"); }
  140. /// \brief Server端加密使用的算法
  141. std::string GetXCosServerSideEncryption() const {
  142. return GetHeader("x-cos-server-side-encryption");
  143. }
  144. void ParseFromHeaders(const std::map<std::string, std::string>& headers);
  145. private:
  146. std::string m_x_cos_object_type;
  147. std::map<std::string, std::string> m_x_cos_metas;
  148. };
  149. class InitMultiUploadResp : public BaseResp {
  150. public:
  151. InitMultiUploadResp() {}
  152. virtual ~InitMultiUploadResp() {}
  153. virtual bool ParseFromXmlString(const std::string& body);
  154. /// \brief 获取uploadId, 用于后续上传
  155. std::string GetUploadId() const { return m_upload_id; }
  156. /// \brief 获取Object的名称
  157. std::string GetKey() const { return m_key; }
  158. /// \brief 分片上传的目标Bucket
  159. std::string GetBucket() const { return m_bucket; }
  160. /// \brief Server端加密使用的算法
  161. std::string GetXCosServerSideEncryption() const {
  162. return GetHeader("x-cos-server-side-encryption");
  163. }
  164. private:
  165. std::string m_bucket;
  166. std::string m_key; // object名称
  167. std::string m_upload_id;
  168. };
  169. class UploadPartDataResp : public BaseResp {
  170. public:
  171. UploadPartDataResp() {}
  172. virtual ~UploadPartDataResp() {}
  173. /// \brief Server端加密使用的算法
  174. std::string GetXCosServerSideEncryption() const {
  175. return GetHeader("x-cos-server-side-encryption");
  176. }
  177. };
  178. class UploadPartCopyDataResp : public BaseResp {
  179. public:
  180. UploadPartCopyDataResp() {}
  181. virtual ~UploadPartCopyDataResp() {}
  182. virtual bool ParseFromXmlString(const std::string& body);
  183. /// \brief 获取返回文件的MD5算法校验值。
  184. /// ETag 的值可以用于检查 Object 的内容是否发生变化。
  185. std::string GetEtag() const { return m_etag; }
  186. /// \brief 返回文件最后修改时间,GMT 格式
  187. std::string GetLastModified() const { return m_last_modified; }
  188. /// \brief Server端加密使用的算法
  189. std::string GetXCosServerSideEncryption() const {
  190. return GetHeader("x-cos-server-side-encryption");
  191. }
  192. private:
  193. std::string m_etag;
  194. std::string m_last_modified;
  195. };
  196. class CompleteMultiUploadResp : public BaseResp {
  197. public:
  198. CompleteMultiUploadResp() {}
  199. virtual ~CompleteMultiUploadResp() {}
  200. virtual bool ParseFromXmlString(const std::string& body);
  201. std::string GetLocation() const { return m_location; }
  202. std::string GetKey() const { return m_key; }
  203. std::string GetBucket() const { return m_bucket; }
  204. std::string GetVersionId() const { return GetHeader("x-cos-version-id"); }
  205. /// \brief Server端加密使用的算法
  206. std::string GetXCosServerSideEncryption() const {
  207. return GetHeader("x-cos-server-side-encryption");
  208. }
  209. private:
  210. std::string m_location; // Object的外网访问域名
  211. std::string m_bucket;
  212. std::string m_key;
  213. };
  214. class AbortMultiUploadResp : public BaseResp {
  215. public:
  216. AbortMultiUploadResp() {}
  217. virtual ~AbortMultiUploadResp() {}
  218. };
  219. class ListPartsResp : public BaseResp {
  220. public:
  221. ListPartsResp()
  222. : m_bucket(""),
  223. m_encoding_type(""),
  224. m_key(""),
  225. m_upload_id(""),
  226. m_part_number_marker(0),
  227. m_parts(0),
  228. m_next_part_number_marker(0),
  229. m_storage_class(""),
  230. m_max_parts(1000),
  231. m_is_truncated(false) {}
  232. virtual ~ListPartsResp() {}
  233. virtual bool ParseFromXmlString(const std::string& body);
  234. std::string GetBucket() const { return m_bucket; }
  235. std::string GetEncodingType() const { return m_encoding_type; }
  236. std::string GetKey() const { return m_key; }
  237. std::string GetUploadId() const { return m_upload_id; }
  238. Initiator GetInitiator() const { return m_initiator; }
  239. Owner GetOwner() const { return m_owner; }
  240. uint64_t GetPartNumberMarker() const { return m_part_number_marker; }
  241. std::vector<Part> GetParts() const { return m_parts; }
  242. uint64_t GetNextPartNumberMarker() const { return m_next_part_number_marker; }
  243. std::string GetStorageClass() const { return m_storage_class; }
  244. uint64_t GetMaxParts() const { return m_max_parts; }
  245. bool IsTruncated() const { return m_is_truncated; }
  246. private:
  247. std::string m_bucket;
  248. std::string m_encoding_type;
  249. std::string m_key;
  250. std::string m_upload_id;
  251. Initiator m_initiator;
  252. Owner m_owner;
  253. uint64_t m_part_number_marker;
  254. std::vector<Part> m_parts;
  255. uint64_t m_next_part_number_marker;
  256. std::string m_storage_class;
  257. uint64_t m_max_parts;
  258. bool m_is_truncated;
  259. };
  260. class GetObjectACLResp : public BaseResp {
  261. public:
  262. GetObjectACLResp() {}
  263. virtual ~GetObjectACLResp() {}
  264. virtual bool ParseFromXmlString(const std::string& body);
  265. std::string GetOwnerID() const { return m_owner_id; }
  266. std::string GetOwnerDisplayName() const { return m_owner_display_name; }
  267. std::vector<Grant> GetAccessControlList() const { return m_acl; }
  268. private:
  269. std::string m_owner_id;
  270. std::string m_owner_display_name;
  271. std::vector<Grant> m_acl;
  272. };
  273. class PutObjectACLResp : public BaseResp {
  274. public:
  275. PutObjectACLResp() {}
  276. virtual ~PutObjectACLResp() {}
  277. };
  278. class PutObjectCopyResp : public BaseResp {
  279. public:
  280. PutObjectCopyResp() {}
  281. virtual ~PutObjectCopyResp() {}
  282. virtual bool ParseFromXmlString(const std::string& body);
  283. std::string GetEtag() const { return m_etag; }
  284. std::string GetLastModified() const { return m_last_modified; }
  285. std::string GetVersionId() const { return m_version_id; }
  286. std::string GetCrc64() const { return m_crc64; }
  287. /// Server端加密使用的算法
  288. std::string GetXCosServerSideEncryption() const {
  289. return GetHeader("x-cos-server-side-encryption");
  290. }
  291. private:
  292. std::string m_etag;
  293. std::string m_last_modified;
  294. std::string m_version_id;
  295. std::string m_crc64;
  296. };
  297. class CopyResp : public BaseResp {
  298. public:
  299. CopyResp()
  300. : m_location(""),
  301. m_bucket(""),
  302. m_key(""),
  303. m_etag(""),
  304. m_last_modified(""),
  305. m_version_id(""),
  306. m_resp_tag("") {}
  307. virtual ~CopyResp() {}
  308. void CopyFrom(const PutObjectCopyResp& resp);
  309. void CopyFrom(const CompleteMultiUploadResp& resp);
  310. void Clear() {
  311. m_location = "";
  312. m_bucket = "";
  313. m_key = "";
  314. m_etag = "";
  315. m_last_modified = "";
  316. m_version_id = "";
  317. m_resp_tag = "";
  318. }
  319. std::string GetLocation() const { return m_location; }
  320. std::string GetKey() const { return m_key; }
  321. std::string GetBucket() const { return m_bucket; }
  322. std::string GetEtag() const { return m_etag; }
  323. std::string GetLastModified() const { return m_last_modified; }
  324. std::string GetVersionId() const { return m_version_id; }
  325. std::string GetRespTag() { return m_resp_tag; }
  326. private:
  327. std::string m_location; // Object的外网访问域名
  328. std::string m_bucket;
  329. std::string m_key;
  330. std::string m_etag;
  331. std::string m_last_modified;
  332. std::string m_version_id;
  333. std::string m_resp_tag; // 用于区分是哪一种response
  334. };
  335. class PostObjectRestoreResp : public BaseResp {
  336. public:
  337. PostObjectRestoreResp() {}
  338. ~PostObjectRestoreResp() {}
  339. };
  340. class OptionsObjectResp : public BaseResp {
  341. public:
  342. OptionsObjectResp() {}
  343. ~OptionsObjectResp() {}
  344. /// \brief 获取模拟跨域访问的请求来源域名,当来源不允许的时候,此Header不返回
  345. std::string GetAccessControAllowOrigin() const {
  346. return GetHeader("Access-Control-Allow-Origin");
  347. }
  348. /// \brief 获取模拟跨域访问的请求 HTTP
  349. /// 方法,当请求方法不允许的时候,此Header不返回
  350. std::string GetAccessControlAllowMethods() const {
  351. return GetHeader("Access-Control-Allow-Methods");
  352. }
  353. /// \brief
  354. /// 获取模拟跨域访问的请求头部,当模拟任何请求头部不允许的时候,此Header不返回该请求头部
  355. std::string GetAccessControlAllowHeaders() const {
  356. return GetHeader("Access-Control-Allow-Headers");
  357. }
  358. /// \brief 获取模拟跨域访问的请求 HTTP
  359. /// 方法,当请求方法不允许的时候,此Header不返回
  360. std::string GetAccessControlExposeHeaders() const {
  361. return GetHeader("Access-Control-Expose-Headers");
  362. }
  363. /// \brief 获取OPTIONS请求得到结果的有效期
  364. std::string GetAccessControlMaxAge() const {
  365. return GetHeader("Access-Control-Max-Age");
  366. }
  367. };
  368. class SelectObjectContentResp : public BaseResp {
  369. public:
  370. SelectObjectContentResp() { resp_data.reserve(10); }
  371. ~SelectObjectContentResp() {}
  372. /// \brief 将响应结果写入本地文件
  373. int WriteResultToLocalFile(const std::string& file);
  374. bool ParseFromXmlString(const std::string& body);
  375. // bool ParseFromStringStream(const std::ostringstream& os);
  376. /// \brief 打印最终结果至终端
  377. void PrintResult() const;
  378. private:
  379. // void ParseStatsEvent(const std::string& stat_str);
  380. // void ParseProgressEvent(const std::string& prog_str);
  381. std::vector<SelectMessage> resp_data;
  382. std::string error_message;
  383. std::string error_code;
  384. };
  385. class AppendObjectResp : public PutObjectByStreamResp {
  386. public:
  387. AppendObjectResp() {}
  388. ~AppendObjectResp() {}
  389. /// \brief 获取下一次追加操作的起始点,单位:字节
  390. std::string GetNextPosition() const {
  391. return GetHeader(kRespHeaderXCosNextAppendPosition);
  392. }
  393. /// \brief 获取服务端返回的本次append内容的md5
  394. std::string GetXCosContentSha1() const {
  395. return GetHeader(kRespHeaderXCosContentSha1);
  396. }
  397. };
  398. /// \brief: 创建直播通道的响应
  399. class PutLiveChannelResp : public BaseResp {
  400. public:
  401. PutLiveChannelResp() {}
  402. virtual ~PutLiveChannelResp() {}
  403. virtual bool ParseFromXmlString(const std::string& body);
  404. const std::string GetPublishUrl() const { return m_publish_url; }
  405. std::string& GetPublishUrl() { return m_publish_url; }
  406. std::string GetPlayUrl() const { return m_play_url; }
  407. private:
  408. std::string m_publish_url; // 推流url
  409. std::string m_play_url; // 观流url
  410. };
  411. /// \brief: 启用或者禁用直播通道的响应
  412. class PutLiveChannelSwitchResp : public BaseResp {
  413. public:
  414. PutLiveChannelSwitchResp() {}
  415. virtual ~PutLiveChannelSwitchResp() {}
  416. };
  417. /// \brief: 获取直播通道配置的响应
  418. class GetLiveChannelResp : public BaseResp {
  419. public:
  420. GetLiveChannelResp() {}
  421. virtual ~GetLiveChannelResp() {}
  422. virtual bool ParseFromXmlString(const std::string& body);
  423. const LiveChannelConfiguration& GetLiveChannelConf() const {
  424. return m_chan_conf;
  425. }
  426. private:
  427. LiveChannelConfiguration m_chan_conf;
  428. };
  429. /// \brief: 获取直播通道推流历史的响应
  430. class GetLiveChannelHistoryResp : public BaseResp {
  431. public:
  432. GetLiveChannelHistoryResp() {}
  433. virtual ~GetLiveChannelHistoryResp() {}
  434. virtual bool ParseFromXmlString(const std::string& body);
  435. const std::vector<LiveRecord>& GetChanHistory() const { return m_history; }
  436. private:
  437. std::vector<LiveRecord> m_history;
  438. };
  439. /// \brief: 获取直播通道推流状态的响应
  440. class GetLiveChannelStatusResp : public BaseResp {
  441. public:
  442. GetLiveChannelStatusResp() {}
  443. virtual ~GetLiveChannelStatusResp() {}
  444. virtual bool ParseFromXmlString(const std::string& body);
  445. const LiveChannelStatus& GetLiveChannelStatus() const {
  446. return m_livechan_status;
  447. }
  448. private:
  449. LiveChannelStatus m_livechan_status;
  450. };
  451. /// \brief: 删除直播通道的响应
  452. class DeleteLiveChannelResp : public BaseResp {
  453. public:
  454. DeleteLiveChannelResp() {}
  455. virtual ~DeleteLiveChannelResp() {}
  456. };
  457. /// \brief: 查询指定通道在指定时间段推流生成的播放列表的响应
  458. class GetLiveChannelVodPlaylistResp : public BaseResp {
  459. public:
  460. GetLiveChannelVodPlaylistResp() {}
  461. virtual ~GetLiveChannelVodPlaylistResp() {}
  462. int WriteResultToFile(const std::string& file);
  463. };
  464. /// \brief: 为指定通道生成一个可供点播例用的播放列表的响应
  465. class PostLiveChannelVodPlaylistResp : public BaseResp {
  466. public:
  467. PostLiveChannelVodPlaylistResp() {}
  468. virtual ~PostLiveChannelVodPlaylistResp() {}
  469. };
  470. /* Multithread接口 */
  471. class MultiPutObjectResp : public BaseResp {
  472. public:
  473. MultiPutObjectResp() {}
  474. virtual ~MultiPutObjectResp() {}
  475. virtual bool ParseFromXmlString(const std::string& body);
  476. std::string GetRespTag() { return m_resp_tag; }
  477. std::string GetLocation() const { return m_location; }
  478. std::string GetKey() const { return m_key; }
  479. std::string GetBucket() const { return m_bucket; }
  480. void CopyFrom(const InitMultiUploadResp& resp);
  481. void CopyFrom(const UploadPartDataResp& resp);
  482. void CopyFrom(const CompleteMultiUploadResp& resp);
  483. /// \brief Server端加密使用的算法
  484. std::string GetXCosServerSideEncryption() const {
  485. return GetHeader("x-cos-server-side-encryption");
  486. }
  487. private:
  488. std::string m_location; // Object的外网访问域名
  489. std::string m_bucket;
  490. std::string m_key;
  491. std::string m_upload_id;
  492. // FIXME(sevenyou) 先这么搞吧
  493. std::string m_resp_tag; // 用于区分是哪一种response
  494. };
  495. class MultiGetObjectResp : public GetObjectByFileResp {
  496. public:
  497. MultiGetObjectResp() {}
  498. ~MultiGetObjectResp() {}
  499. };
  500. /* Async接口 */
  501. //typedef PutObjectByFileResp PutObjectAsyncResp;
  502. //typedef PutObjectByFileResp MultiPutObjectAsyncResp;
  503. //typedef GetObjectByFileResp GetObjectAsyncResp;
  504. //typedef GetObjectByFileResp MultiGetObjectAsyncResp;
  505. class AsyncResp : public BaseResp {
  506. public:
  507. AsyncResp() {}
  508. virtual ~AsyncResp() {}
  509. };
  510. /* 批量及目录操作接口 */
  511. class PutObjectsByDirectoryResp {
  512. public:
  513. PutObjectsByDirectoryResp() {}
  514. virtual ~PutObjectsByDirectoryResp() {}
  515. public:
  516. class PutResp {
  517. public:
  518. PutResp() {}
  519. virtual ~PutResp() {}
  520. std::string m_file_name; // 本地文件名
  521. std::string m_object_name; // 对象名
  522. BaseResp m_cos_resp; // cos返回的响应
  523. };
  524. // 成功上传的对象
  525. std::vector<PutResp> m_succ_put_objs;
  526. };
  527. class PutDirectoryResp : public PutObjectResp {
  528. public:
  529. PutDirectoryResp() {}
  530. virtual ~PutDirectoryResp() {}
  531. };
  532. class MoveObjectResp {
  533. public:
  534. MoveObjectResp() {}
  535. virtual ~MoveObjectResp() {}
  536. };
  537. class DeleteObjectsByPrefixResp {
  538. public:
  539. DeleteObjectsByPrefixResp() {}
  540. virtual ~DeleteObjectsByPrefixResp() {}
  541. std::vector<std::string> m_succ_del_objs; // 成功删除的对象
  542. };
  543. } // namespace qcloud_cos
  544. #endif // OBJECT_RESP_H