{"id":204020,"date":"2025-05-29T11:56:03","date_gmt":"2025-05-29T03:56:03","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204020\/"},"modified":"2025-05-29T11:56:03","modified_gmt":"2025-05-29T03:56:03","slug":"read-cloud-specific-configuration-from-configuration-files","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204020\/","title":{"rendered":"Read cloud specific configuration from configuration files"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Read cloud specific configuration from configuration files<\/h1>\n<p>\u5077\u5077\u52aa\u529b\uff0c\u6084\u65e0\u58f0\u606f\u5730\u53d8\u5f3a\uff0c\u7136\u540e\u60ca\u8273\u6240\u6709\u4eba\uff01\u54c8\u54c8\uff0c\u5c0f\u4f19\u4f34\u4eec\u53c8\u6765\u5b66\u4e60\u5566~\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aRead cloud specific configuration from configuration files\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u4e0d\u77e5\u9053\u5927\u5bb6\u5bf9\u5176\u90fd\u6709\u591a\u5c11\u4e86\u89e3\uff0c\u4e0b\u9762\u6211\u4eec\u5c31\u4e00\u8d77\u6765\u770b\u4e00\u5427\uff01\u5f53\u7136\uff0c\u975e\u5e38\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u8bc4\u8bba\uff0c\u7ed9\u51fa\u5408\u7406\u7684\u5efa\u8bae\uff0c\u6211\u4eec\u4e00\u8d77\u5b66\u4e60\uff0c\u4e00\u8d77\u8fdb\u6b65\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241019\/17293169226713483a384ba.jpg\" class=\"aligncenter\"><\/p>\n<p>\u7279\u5b9a\u4e8e\u4e91\u63d0\u4f9b\u5546\u7684\u914d\u7f6e\u53ef\u4ee5\u901a\u8fc7\u5c06\u4e91\u63d0\u4f9b\u5546\u540d\u79f0\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u90e8\u5206\u6765\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u5206\u9694\u3002<\/p>\n<p>\u5728 src \u5305\u4e2d\u521b\u5efa\u4e00\u4e2a\u914d\u7f6e\u6587\u4ef6 &#8211; config.ini \u5e76\u5b9a\u4e49\u7c7b\u4f3c\u4e8e\u4e0b\u9762\u7684\u4e91\u63d0\u4f9b\u5546\u7279\u5b9a\u914d\u7f6e\u3002<\/p>\n<pre>[aws]\nbucket_name: test-aws-bucket\n\n[gcp]\nbucket_name: test-gcp-bucket\n<\/pre>\n<p>\u4ece\u73af\u5883\u53d8\u91cf\u4e2d\u8bfb\u53d6\u4e91\u63d0\u4f9b\u5546\u3002<\/p>\n<p>cloud_provider = os.environ.get(&#8216;cloud_provider&#8217;)<\/p>\n<p>\u5728python\u4e2d\u58f0\u660e\u4e00\u4e2a\u914d\u7f6e\u89e3\u6790\u5668\u5e76\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6<\/p>\n<pre>config = configparser.ConfigParser()\nconfig_path = os.path.join(os.path.dirname(__file__), 'config.ini')\nconfig.read(config_path)\nbucket_name = config.get(cloud_provider,'bucket_name')\n<\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u5206\u79bb\u4e91\u63d0\u4f9b\u5546\u7684\u7279\u5b9a\u914d\u7f6e\u3002<\/p>\n<p>\u5982\u6709\u4efb\u4f55\u5efa\u8bae\/\u53cd\u9988\uff0c\u8bf7\u968f\u65f6\u53d1\u8868\u8bc4\u8bba\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aRead cloud specific configuration from configuration files\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Read cloud speci&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204020","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204020","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204020"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204020\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204020"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}