cos_config.h 7.9 KB

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