object_op.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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_OP_H
  8. #define OBJECT_OP_H
  9. #include "op/base_op.h"
  10. #include "op/cos_result.h"
  11. #include "request/data_process_req.h"
  12. #include "request/object_req.h"
  13. #include "request/auditing_req.h"
  14. #include "response/data_process_resp.h"
  15. #include "response/object_resp.h"
  16. #include "response/auditing_resp.h"
  17. namespace qcloud_cos {
  18. class FileUploadTask;
  19. class FileCopyTask;
  20. /// \brief 封装了Object相关的操作
  21. class ObjectOp : public BaseOp {
  22. public:
  23. /// \brief BucketOp构造函数
  24. ///
  25. /// \param cos_conf Cos配置
  26. explicit ObjectOp(const SharedConfig& config) : BaseOp(config) {}
  27. ObjectOp() {}
  28. /// \brief ObjectOP析构函数
  29. virtual ~ObjectOp() {}
  30. /// \brief 判断object是否存在
  31. bool IsObjectExist(const std::string& bucket_name,
  32. const std::string& object_name);
  33. std::string GetResumableUploadID(const std::string& bucket_name,
  34. const std::string& object_name);
  35. bool CheckUploadPart(const PutObjectByFileReq& req,
  36. const std::string& bucket_name,
  37. const std::string& object_name,
  38. const std::string& uploadid,
  39. std::vector<std::string>& already_exist);
  40. bool CheckSinglePart(const PutObjectByFileReq& req, uint64_t offset,
  41. uint64_t local_part_size, uint64_t size,
  42. const std::string& etag);
  43. /// \brief 获取对应Object的meta信息数据
  44. ///
  45. /// \param request HeadObject请求
  46. /// \param response HeadObject返回
  47. ///
  48. /// \return 返回HTTP请求的状态码及错误信息
  49. CosResult HeadObject(const HeadObjectReq& req, HeadObjectResp* resp);
  50. /// \brief 下载Bucket中的一个文件至流中
  51. ///
  52. /// \param request GetObjectByStream请求
  53. /// \param response GetObjectByStream返回
  54. ///
  55. /// \return 返回HTTP请求的状态码及错误信息
  56. CosResult GetObject(const GetObjectByStreamReq& req,
  57. GetObjectByStreamResp* resp);
  58. /// \brief 下载Bucket中的一个文件到本地
  59. ///
  60. /// \param request GetObjectByFile请求
  61. /// \param response GetObjectByFile返回
  62. ///
  63. /// \return 返回HTTP请求的状态码及错误信息
  64. CosResult GetObject(const GetObjectByFileReq& req, GetObjectByFileResp* resp,
  65. const SharedTransferHandler& handler = nullptr);
  66. /// \brief 多线程下载Bucket中的一个文件到本地
  67. ///
  68. /// \param request MultiGetObject请求
  69. /// \param response MultiGetObject返回
  70. ///
  71. /// \return 返回HTTP请求的状态码及错误信息
  72. CosResult MultiGetObject(const GetObjectByFileReq& req,
  73. GetObjectByFileResp* resp);
  74. /// \brief 将本地的文件上传至指定Bucket中
  75. ///
  76. /// \param request PutObjectByFile请求
  77. /// \param response PutObjectByFile返回
  78. ///
  79. /// \return 返回HTTP请求的状态码及错误信息
  80. CosResult PutObject(const PutObjectByFileReq& req, PutObjectByFileResp* resp,
  81. const SharedTransferHandler& handler = nullptr);
  82. /// \brief 将指定流上传至指定Bucket中
  83. ///
  84. /// \param request PutObjectByStream请求
  85. /// \param response PutObjectByStream返回
  86. ///
  87. /// \return 返回HTTP请求的状态码及错误信息
  88. CosResult PutObject(const PutObjectByStreamReq& req,
  89. PutObjectByStreamResp* resp, const SharedTransferHandler& handler=nullptr);
  90. /// \brief 删除Object
  91. ///
  92. /// \param req DeleteObject请求
  93. /// \param resp DeleteObject返回
  94. ///
  95. /// \return 本次请求的调用情况(如状态码等)
  96. CosResult DeleteObject(const DeleteObjectReq& req, DeleteObjectResp* resp);
  97. /// \brief 批量删除Object
  98. ///
  99. /// \param req DeleteObjects请求
  100. /// \param resp DeleteObjects返回
  101. ///
  102. /// \return 本次请求的调用情况(如状态码等)
  103. CosResult DeleteObjects(const DeleteObjectsReq& req, DeleteObjectsResp* resp);
  104. /// \brief
  105. /// 请求实现初始化分片上传,成功执行此请求以后会返回UploadId用于后续的Upload
  106. /// Part请求
  107. ///
  108. /// \param request InitMultiUpload请求
  109. /// \param response InitMultiUpload返回
  110. ///
  111. /// \return 返回HTTP请求的状态码及错误信息
  112. CosResult InitMultiUpload(const InitMultiUploadReq& req,
  113. InitMultiUploadResp* resp);
  114. /// \brief 初始化以后的分块上传,支持的块的数量为1到10000,块的大小为1MB到5GB
  115. ///
  116. /// \param request UploadPartData请求
  117. /// \param response UploadPartData返回
  118. ///
  119. /// \return 返回HTTP请求的状态码及错误信息
  120. CosResult UploadPartData(const UploadPartDataReq& req,
  121. UploadPartDataResp* resp);
  122. /// \brief
  123. /// 初始化以后的分块复制,实现将一个文件的分块内容从源路径复制到目标路径。
  124. /// 通过指定 x-cos-copy-source 来指定源文件,x-cos-copy-source-range
  125. /// 指定字节范围。 允许分块的大小为 5 MB - 5 GB。
  126. ///
  127. /// \param request UploadPartCopyData请求
  128. /// \param response UploadPartCopyData返回
  129. ///
  130. /// \return 返回HTTP请求的状态码及错误信息
  131. CosResult UploadPartCopyData(const UploadPartCopyDataReq& req,
  132. UploadPartCopyDataResp* resp);
  133. /// \brief 完成整个分块上传。当使用 Upload Parts 上传完所有块以后,
  134. /// 必须调用该 API 来完成整个文件的分块上传
  135. ///
  136. /// \param request CompleteMultiUpload请求
  137. /// \param response CompleteMultiUpload返回
  138. ///
  139. /// \return 返回HTTP请求的状态码及错误信息
  140. CosResult CompleteMultiUpload(const CompleteMultiUploadReq& req,
  141. CompleteMultiUploadResp* resp);
  142. /// \brief 异步多线程上传
  143. /// \param request MultiUploadObject请求
  144. /// \param response MultiUploadObject返回
  145. /// \param handler TransferHandler
  146. ///
  147. /// \return result
  148. CosResult MultiUploadObject(const PutObjectByFileReq& req,
  149. MultiPutObjectResp* resp,
  150. const SharedTransferHandler& handler = nullptr);
  151. /// \brief 舍弃一个分块上传并删除已上传的块
  152. ///
  153. /// \param req AbortMultiUpload请求
  154. /// \param resp AbortMultiUpload返回
  155. ///
  156. /// \return
  157. CosResult AbortMultiUpload(const AbortMultiUploadReq& req,
  158. AbortMultiUploadResp* resp);
  159. /// \brief 查询特定分块上传中的已上传的块
  160. ///
  161. /// \param req ListParts请求
  162. /// \param resp ListParts返回
  163. ///
  164. /// \return result
  165. CosResult ListParts(const ListPartsReq& req, ListPartsResp* resp);
  166. /// \brief 列出Object下的ACL
  167. ///
  168. /// \param req GetObjectACL请求
  169. /// \param resp GetObjectACL返回
  170. ///
  171. /// \return 本次请求的调用情况(如状态码等)
  172. CosResult GetObjectACL(const GetObjectACLReq& req, GetObjectACLResp* resp);
  173. /// \brief 增加/替换Object下的ACL, 可以通过Header或者Body传入ACL信息
  174. /// 注意Header 和 Body 只能选择其中一种,否则响应返回会冲突
  175. ///
  176. /// \param req PutObjectACL请求
  177. /// \param resp PutObjectACL返回
  178. ///
  179. /// \return 本次请求的调用情况(如状态码等)
  180. CosResult PutObjectACL(const PutObjectACLReq& req, PutObjectACLResp* resp);
  181. /// \brief 复制Object
  182. ///
  183. /// \param req PutObjectCopy请求
  184. /// \param resp PutObjectCopy返回
  185. ///
  186. /// \return 本次请求的调用情况(如状态码等)
  187. CosResult PutObjectCopy(const PutObjectCopyReq& req, PutObjectCopyResp* resp);
  188. /// \brief 复制文件,实现将一个文件的分块内容从源路径复制到目标路径。
  189. /// 通过指定 x-cos-copy-source 来指定源文件,x-cos-copy-source-range
  190. /// 指定字节范围。
  191. ///
  192. /// \param request Copy请求
  193. /// \param response Copy返回
  194. ///
  195. /// \return 返回HTTP请求的状态码及错误信息
  196. CosResult Copy(const CopyReq& req, CopyResp* resp);
  197. /// \brief 对一个通过 COS 归档为 archive 类型的对象进行恢复
  198. ///
  199. /// \param request PostObjectRestore请求
  200. /// \param response PostObjectRestore返回
  201. ///
  202. /// \return 返回HTTP请求的状态码及错误信息
  203. CosResult PostObjectRestore(const PostObjectRestoreReq& req,
  204. PostObjectRestoreResp* resp);
  205. std::string GeneratePresignedUrl(const GeneratePresignedUrlReq& req);
  206. CosResult OptionsObject(const OptionsObjectReq& req, OptionsObjectResp* resp);
  207. CosResult SelectObjectContent(const SelectObjectContentReq& req,
  208. SelectObjectContentResp* resp);
  209. CosResult AppendObject(const AppendObjectReq& req, AppendObjectResp* resp);
  210. /// \brief 创建推流通道
  211. ///
  212. /// \param request PutLiveChannelReq请求
  213. /// \param response PutLiveChannelResp返回
  214. ///
  215. /// \return 返回HTTP请求的状态码及错误信息
  216. CosResult PutLiveChannel(const PutLiveChannelReq& req,
  217. PutLiveChannelResp* resp);
  218. /// \brief 启用或禁用通道
  219. ///
  220. /// \param request PutLiveChannelSwitchReq请求
  221. /// \param response PutLiveChannelSwitchResp返回
  222. ///
  223. /// \return 返回HTTP请求的状态码及错误信息
  224. CosResult PutLiveChannelSwitch(const PutLiveChannelSwitchReq& req,
  225. PutLiveChannelSwitchResp* resp);
  226. /// \brief 获取直播通道配置
  227. ///
  228. /// \param request GetLiveChannelReq请求
  229. /// \param response GetLiveChannelResp返回
  230. ///
  231. /// \return 返回HTTP请求的状态码及错误信息
  232. CosResult GetLiveChannel(const GetLiveChannelReq& req,
  233. GetLiveChannelResp* resp);
  234. /// \brief 获取直播通道推流历史
  235. ///
  236. /// \param request GetLiveChannelHistoryReq请求
  237. /// \param response GetLiveChannelHistoryResp返回
  238. ///
  239. /// \return 返回HTTP请求的状态码及错误信息
  240. CosResult GetLiveChannelHistory(const GetLiveChannelHistoryReq& req,
  241. GetLiveChannelHistoryResp* resp);
  242. /// \brief 获取直播通道推流状态
  243. ///
  244. /// \param request GetLiveChannelStatusReq请求
  245. /// \param response GetLiveChannelStatusResp返回
  246. ///
  247. /// \return 返回HTTP请求的状态码及错误信息
  248. CosResult GetLiveChannelStatus(const GetLiveChannelStatusReq& req,
  249. GetLiveChannelStatusResp* resp);
  250. /// \brief 删除直播通
  251. ///
  252. /// \param request DeleteLiveChannelReq请求
  253. /// \param response DeleteLiveChannelResp返回
  254. ///
  255. /// \return 返回HTTP请求的状态码及错误信息
  256. CosResult DeleteLiveChannel(const DeleteLiveChannelReq& req,
  257. DeleteLiveChannelResp* resp);
  258. /// \brief 查询指定通道在指定时间段推流生成的播放列表
  259. ///
  260. /// \param request GetLiveChannelVodPlaylistReq请求
  261. /// \param response GetLiveChannelVodPlaylistResp返回
  262. ///
  263. /// \return 返回HTTP请求的状态码及错误信息
  264. CosResult GetLiveChannelVodPlaylist(const GetLiveChannelVodPlaylistReq& req,
  265. GetLiveChannelVodPlaylistResp* resp);
  266. /// \brief 指定通道生成一个可供点播例用的播放列表
  267. ///
  268. /// \param request PostLiveChannelVodPlaylistReq请求
  269. /// \param response GetLiveChannelVodPlaylistResp返回
  270. ///
  271. /// \return 返回HTTP请求的状态码及错误信息
  272. CosResult PostLiveChannelVodPlaylist(const PostLiveChannelVodPlaylistReq& req,
  273. PostLiveChannelVodPlaylistResp* resp);
  274. /// \brief 异步多线程下载,handler处理回调
  275. CosResult MultiThreadDownload(const GetObjectByFileReq& req,
  276. GetObjectByFileResp* resp,
  277. const SharedTransferHandler& handler = nullptr);
  278. /* Resumable接口 */
  279. /// \brief 支持断点下载
  280. CosResult ResumableGetObject(const GetObjectByFileReq& req,
  281. GetObjectByFileResp* resp,
  282. const SharedTransferHandler& handler = nullptr);
  283. /*批量及目录操作接口*/
  284. CosResult PutObjects(const PutObjectsByDirectoryReq& req,
  285. PutObjectsByDirectoryResp* resp);
  286. CosResult PutDirectory(const PutDirectoryReq& req, PutDirectoryResp* resp);
  287. CosResult MoveObject(const MoveObjectReq& req);
  288. /*数据处理接口*/
  289. /**基础图片处理**/
  290. /**图片持久化处理**/
  291. /***上传时处理***/
  292. CosResult PutImage(const PutImageByFileReq& req, PutImageByFileResp* resp);
  293. /***云上数据处理***/
  294. CosResult CloudImageProcess(const CloudImageProcessReq& req,
  295. CloudImageProcessResp* resp);
  296. /***下载图片时识别二维码***/
  297. CosResult GetQRcode(const GetQRcodeReq& req, GetQRcodeResp* resp);
  298. /*文档处理接口*/
  299. /***查询已经开通文档预览功能的 Bucket***/
  300. CosResult DescribeDocProcessBuckets(const DescribeDocProcessBucketsReq& req,
  301. DescribeDocProcessBucketsResp* resp);
  302. /***预览文档***/
  303. CosResult DocPreview(const DocPreviewReq& req, DocPreviewResp* resp);
  304. /***获取媒体文件的信息***/
  305. CosResult GetMediaInfo(const GetMediaInfoReq& req, GetMediaInfoResp* resp);
  306. CosResult GetImageAuditing(const GetImageAuditingReq& req, GetImageAuditingResp* resp);
  307. private:
  308. // 生成request body所需的xml字符串
  309. bool GenerateCompleteMultiUploadReqBody(const CompleteMultiUploadReq& req,
  310. std::string* req_body);
  311. /// \brief 多线程上传,handler处理回调
  312. CosResult
  313. MultiThreadUpload(const PutObjectByFileReq& req, const std::string& upload_id,
  314. const std::vector<std::string>& already_exist_parts,
  315. bool resume_flag, std::vector<std::string>* etags_ptr,
  316. std::vector<uint64_t>* part_numbers_ptr,
  317. const SharedTransferHandler& handler = nullptr);
  318. /// \brief 读取文件内容, 并返回读取的长度
  319. // uint64_t GetContent(const std::string& src, std::string* file_content) const;
  320. void FillUploadTask(const std::string& upload_id, const std::string& host,
  321. const std::string& path, unsigned char* file_content_buf,
  322. uint64_t len, uint64_t part_number,
  323. FileUploadTask* task_ptr, bool sign_header_host);
  324. void FillCopyTask(const std::string& upload_id, const std::string& host,
  325. const std::string& path, uint64_t part_number,
  326. const std::string& range,
  327. const std::map<std::string, std::string>& headers,
  328. const std::map<std::string, std::string>& params,
  329. bool verify_cert,const std::string& ca_location,
  330. FileCopyTask* task, bool sign_header_host);
  331. /// \brief 检查是否可以走断点下载
  332. /// \param req PutObjectByFile请求
  333. /// \param head_resp HeadObjectResp响应结果
  334. /// \param last_offset 返回的上一次下载偏移量
  335. /// \return true可以走断点下载,false表示不可以
  336. bool CheckResumableDownloadTask(
  337. const std::string& json_file,
  338. const std::map<std::string, std::string>& element_map,
  339. uint64_t* resume_offset);
  340. /// \brief 更新断点下载json文件
  341. /// \param json_file json文件名
  342. /// \param element_map 检查的元素映射
  343. /// \param last_offset 返回的上一次下载偏移量
  344. /// \return true文件检查成功, 否则失败
  345. void UpdateResumableDownloadTaskFile(
  346. const std::string& json_file,
  347. const std::map<std::string, std::string>& element_map,
  348. uint64_t resume_offset);
  349. void SetResultAndLogError(CosResult& result, const std::string& err_msg);
  350. };
  351. } // namespace qcloud_cos
  352. #endif // OBJECT_OP_H