object_req.h 61 KB

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