cos_config.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #ifndef COS_CONFIG_H
  2. #define COS_CONFIG_H
  3. #include <stdint.h>
  4. #include <memory>
  5. #include <mutex>
  6. #include <string>
  7. #include "Poco/JSON/Parser.h"
  8. #include "util/log_util.h"
  9. namespace qcloud_cos {
  10. class CosConfig {
  11. public:
  12. /// \brief CosConfig构造函数
  13. ///
  14. /// \param config_file 配置文件所在路径
  15. explicit CosConfig(const std::string& config_file);
  16. /// \brief CosConfig构造函数
  17. CosConfig()
  18. : m_app_id(0),
  19. m_access_key(""),
  20. m_secret_key(""),
  21. m_region(""),
  22. m_tmp_token(""),
  23. m_set_intranet_once(false),
  24. m_is_use_intranet(false),
  25. m_intranet_addr(""),
  26. m_dest_domain(""),
  27. m_is_domain_same_to_host(false),
  28. m_is_domain_same_to_host_enable(false),
  29. m_config_parsed(false) {}
  30. /// \brief CosConfig构造函数
  31. ///
  32. /// \param appid 开发者访问 COS
  33. /// 服务时拥有的用户维度唯一资源标识,用以标识资源 \param access_key
  34. /// 开发者拥有的项目身份识别 ID,用以身份认证 \param secret_key
  35. /// 开发者拥有的项目身份密钥
  36. CosConfig(uint64_t appid, const std::string& access_key,
  37. const std::string& secret_key, const std::string& region)
  38. : m_app_id(appid),
  39. m_access_key(access_key),
  40. m_secret_key(secret_key),
  41. m_region(region),
  42. m_tmp_token(""),
  43. m_set_intranet_once(false),
  44. m_is_use_intranet(false),
  45. m_intranet_addr(""),
  46. m_dest_domain(""),
  47. m_is_domain_same_to_host(false),
  48. m_is_domain_same_to_host_enable(false),
  49. m_config_parsed(false) {}
  50. /// \brief CosConfig构造函数
  51. ///
  52. /// \param appid 开发者访问 COS
  53. /// 服务时拥有的用户维度唯一资源标识,用以标识资源 \param access_key
  54. /// 开发者拥有的项目身份识别 ID,用以身份认证 \param secret_key
  55. /// 开发者拥有的项目身份密钥
  56. CosConfig(uint64_t appid, const std::string& access_key,
  57. const std::string& secret_key, const std::string& region,
  58. const std::string& tmp_token)
  59. : m_app_id(appid),
  60. m_access_key(access_key),
  61. m_secret_key(secret_key),
  62. m_region(region),
  63. m_tmp_token(tmp_token),
  64. m_set_intranet_once(false),
  65. m_is_use_intranet(false),
  66. m_intranet_addr(""),
  67. m_dest_domain(""),
  68. m_is_domain_same_to_host(false),
  69. m_is_domain_same_to_host_enable(false),
  70. m_config_parsed(false) {}
  71. /// \brief CosConfig复制构造函数
  72. ///
  73. /// \param config
  74. CosConfig(const CosConfig& config) {
  75. m_app_id = config.m_app_id;
  76. m_access_key = config.m_access_key;
  77. m_secret_key = config.m_secret_key;
  78. m_region = config.m_region;
  79. m_tmp_token = config.m_tmp_token;
  80. m_set_intranet_once = config.m_set_intranet_once;
  81. m_is_use_intranet = config.m_is_use_intranet;
  82. m_intranet_addr = config.m_intranet_addr;
  83. m_dest_domain = config.m_dest_domain;
  84. m_is_domain_same_to_host = config.m_is_domain_same_to_host;
  85. m_is_domain_same_to_host_enable = config.m_is_domain_same_to_host;
  86. m_config_parsed = config.m_config_parsed;
  87. }
  88. /// \brief CosConfig赋值构造函数
  89. ///
  90. /// \param config
  91. CosConfig& operator=(const CosConfig& config) {
  92. m_app_id = config.m_app_id;
  93. m_access_key = config.m_access_key;
  94. m_secret_key = config.m_secret_key;
  95. m_region = config.m_region;
  96. m_tmp_token = config.m_tmp_token;
  97. m_set_intranet_once = config.m_set_intranet_once;
  98. m_is_use_intranet = config.m_is_use_intranet;
  99. m_intranet_addr = config.m_intranet_addr;
  100. m_dest_domain = config.m_dest_domain;
  101. m_is_domain_same_to_host = config.m_is_domain_same_to_host;
  102. m_is_domain_same_to_host_enable = config.m_is_domain_same_to_host;
  103. m_config_parsed = config.m_config_parsed;
  104. return *this;
  105. }
  106. /// \brief 根据配置文件进行初始化
  107. ///
  108. /// \param config_file 配置文件所在路径
  109. ///
  110. /// \return 初始化成功则返回true,否则返回false
  111. bool InitConf(const std::string& config_file);
  112. /// \brief 获取AppID
  113. ///
  114. /// \return appid
  115. uint64_t GetAppId() const;
  116. /// \brief 获取AccessKey
  117. ///
  118. /// \return access_key
  119. std::string GetAccessKey() const;
  120. /// \brief 获取SecretKey
  121. ///
  122. /// \return secret_key
  123. std::string GetSecretKey() const;
  124. /// \brief 操作的Region
  125. /// region的有效值参见https://cloud.tencent.com/document/product/436/6224
  126. ///
  127. /// \return region
  128. std::string GetRegion() const;
  129. /// \brief 获取临时密钥
  130. std::string GetTmpToken() const;
  131. /// \brief 设置AppID
  132. void SetAppId(uint64_t app_id) { m_app_id = app_id; }
  133. /// \brief 设置AccessKey
  134. void SetAccessKey(const std::string& access_key) {
  135. m_access_key = access_key;
  136. }
  137. /// \brief 设置SecreteKey
  138. void SetSecretKey(const std::string& secret_key) {
  139. m_secret_key = secret_key;
  140. }
  141. /// \brief 设置操作的Region
  142. /// region的有效值参见https://cloud.tencent.com/document/product/436/6224
  143. void SetRegion(const std::string& region) { m_region = region; }
  144. /// \brief 设置临时密钥
  145. void SetTmpToken(const std::string& tmp_token) { m_tmp_token = tmp_token; }
  146. /// \brief 更新临时密钥
  147. void SetConfigCredentail(const std::string& access_key,
  148. const std::string& secret_key,
  149. const std::string& tmp_token);
  150. /// \brief 设置是否使用自定义ip和端口号
  151. void SetIsUseIntranetAddr(bool is_use_intranet);
  152. bool IsUseIntranet();
  153. /// \berief 设置自定义ip和端口号
  154. void SetIntranetAddr(const std::string& intranet_addr);
  155. std::string GetIntranetAddr();
  156. bool GetSetIntranetOnce() const {return m_set_intranet_once;}
  157. void SetDestDomain(const std::string& domain);
  158. const std::string& GetDestDomain() const;
  159. bool IsDomainSameToHost() const;
  160. void SetDomainSameToHost(bool is_domain_same_to_host);
  161. bool IsDomainSameToHostEnable() const;
  162. // void SetDomainSameToHostEnable(bool is_domain_same_to_host_enable);
  163. /// \brief 设置日志回调
  164. void SetLogCallback(const LogCallback log_callback);
  165. static bool JsonObjectGetStringValue(
  166. const Poco::JSON::Object::Ptr& json_object, const std::string& key,
  167. std::string* value);
  168. static bool JsonObjectGetIntegerValue(
  169. const Poco::JSON::Object::Ptr& json_object, const std::string& key,
  170. uint64_t* value);
  171. static bool JsonObjectGetBoolValue(const Poco::JSON::Object::Ptr& json_object,
  172. const std::string& key, bool* value);
  173. private:
  174. mutable std::mutex m_lock;
  175. uint64_t m_app_id;
  176. std::string m_access_key;
  177. std::string m_secret_key;
  178. std::string m_region;
  179. std::string m_tmp_token;
  180. bool m_set_intranet_once;
  181. bool m_is_use_intranet;
  182. std::string m_intranet_addr;
  183. std::string m_dest_domain;
  184. bool m_is_domain_same_to_host;
  185. bool m_is_domain_same_to_host_enable;
  186. bool m_config_parsed;
  187. };
  188. typedef std::shared_ptr<CosConfig> SharedConfig;
  189. } // namespace qcloud_cos
  190. #endif