{"id":207991,"date":"2025-07-08T17:09:41","date_gmt":"2025-07-08T09:09:41","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207991\/"},"modified":"2025-07-08T17:09:41","modified_gmt":"2025-07-08T09:09:41","slug":"%e5%a6%82%e4%bd%95%e7%bc%96%e5%86%99%e6%b5%8b%e8%af%95%e6%9d%a5%e6%a3%80%e6%9f%a5%e7%89%b9%e5%ae%9a%e7%b1%bb%e5%9e%8b%e7%9a%84%e5%8f%98%e9%87%8f%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207991\/","title":{"rendered":"\u5982\u4f55\u7f16\u5199\u6d4b\u8bd5\u6765\u68c0\u67e5\u7279\u5b9a\u7c7b\u578b\u7684\u53d8\u91cf\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5982\u4f55\u7f16\u5199\u6d4b\u8bd5\u6765\u68c0\u67e5\u7279\u5b9a\u7c7b\u578b\u7684\u53d8\u91cf\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-27 17:33:39<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p><span style=\"font-size: 15px\">\u5b66\u4e60\u77e5\u8bc6\u8981\u5584\u4e8e\u601d\u8003\uff0c\u601d\u8003\uff0c\u518d\u601d\u8003\uff01\u4eca\u5929\u5c0f\u7f16\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u7f16\u5199\u6d4b\u8bd5\u6765\u68c0\u67e5\u7279\u5b9a\u7c7b\u578b\u7684\u53d8\u91cf\uff1f\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6216\u51c6\u5907\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u5c31\u90fd\u4e0d\u8981\u9519\u8fc7\u672c\u6587\u5566~\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff0c\u80fd\u5e2e\u52a9\u5230\u4f60\u5c31\u66f4\u597d\u4e86\uff01<\/span><\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6709\u4e00\u4e2a\u8fd4\u56de\u7279\u5b9a\u7c7b\u578b\u5ba2\u6237\u7aef\u7684\u51fd\u6570\uff0c\u6211\u60f3\u901a\u8fc7\u68c0\u67e5\u8fd4\u56de\u7684\u53d8\u91cf\u7c7b\u578b\u662f\u5426\u4e3a <code>azblob.blockblobclient<\/code> \u7c7b\u578b\u6765\u6d4b\u8bd5\u8be5\u51fd\u6570\u3002<\/p>\n<p>\u5f53\u6211\u4f7f\u7528\u7b80\u5355\u7684 <code>if<\/code> \u8bed\u53e5\u6765\u68c0\u67e5\u5982\u4e0b\u7c7b\u578b\u65f6\uff1a <code>if var == azblob.blockblobclient<\/code> \u6211\u6536\u5230\u9519\u8bef <code>azblob.blockblobclient (type) is not an expression<\/code><\/p>\n<p>\u4f7f\u7528\u6807\u51c6 <code>testing<\/code> \u5305\u6d4b\u8bd5\u53d8\u91cf\u7c7b\u578b\u7684\u6b63\u786e\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/p>\n<p>\u63d0\u524d\u975e\u5e38\u611f\u8c22\uff01<\/p>\n<p><strong>\/\/\u51fd\u6570<\/strong><\/p>\n<pre>func getclient(blob, container string) azblob.blockblobclient {\n  storageaccount := os.getenv(\"azure_storage_account_name\")\n  \n  cred, err := azidentity.newdefaultazurecredential(nil)\n  if err != nil {\n      log.fatal(\"invalid credentials with error:\" + err.error())\n  }\n\n  bloburl := fmt.sprintf(\"https:\/\/%s.blob.core.windows.net\/%s\/%s\", storageaccount, container, blob)\n  fmt.println(bloburl)\n  client, err := azblob.newblockblobclient(bloburl, cred, nil)\n  if err != nil {\n      log.fatal(\"unable to create blob client\")\n  }\n  return client\n}<\/pre>\n<p><strong>\/\/\u6d4b\u8bd5<\/strong><\/p>\n<pre>package main \n\nimport (\n    \"testing\"\n    \"os\"\n    \"github.com\/azure\/azure-sdk-for-go\/sdk\/storage\/azblob\"\n)\n\nfunc testgetclient(t *testing.t){\n  blob := \"text.txt\"\n  container := \"testcontainer\"\n  os.setenv(\"azure_storage_account_name\", \"mystorageaccount\")\n  client := getclient(blob, container)\n  \n  if client != azblob.blockblobclient {\n    t.errorf(\"client should be type blockblobclient\")\n  }\n}\n<\/pre>\n<pre><\/pre>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u5b9e\u9645\u4e0a\u4e0d\u9700\u8981\u8fd9\u6837\u505a\uff0c\u56e0\u4e3a\u60a8\u7f16\u5199\u7684\u51fd\u6570\u4ec5\u8fd4\u56de <code>azblob.blockblobclient<\/code> \u7c7b\u578b\uff0c\u7f16\u8bd1\u5668\u5c06\u5728\u6784\u5efa\u6d4b\u8bd5\u4e4b\u524d\u68c0\u67e5\u8fd9\u4e00\u70b9\u3002\u5982\u679c\u4e0d\u662f\u8fd9\u79cd\u60c5\u51b5\uff0c\u6d4b\u8bd5\u5c06\u65e0\u6cd5\u8fd0\u884c\u3002<\/p>\n<p>\u6211\u505a\u4e86\u4ee5\u4e0b\u66f4\u6539\u6765\u663e\u793a\u8fd9\u4e00\u70b9\uff1a<\/p>\n<h3>\/\/\u51fd\u6570<\/h3>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"log\"\n    \"os\"\n\n    \"github.com\/azure\/azure-sdk-for-go\/sdk\/azidentity\"\n    \"github.com\/azure\/azure-sdk-for-go\/sdk\/storage\/azblob\"\n)\n\nfunc getclient(blob, container string) interface{} {\n    storageaccount := os.getenv(\"azure_storage_account_name\")\n\n    cred, err := azidentity.newdefaultazurecredential(nil)\n    if err != nil {\n        log.fatal(\"invalid credentials with error:\" + err.error())\n    }\n\n    bloburl := fmt.sprintf(\"https:\/\/%s.blob.core.windows.net\/%s\/%s\", storageaccount, container, blob)\n    fmt.println(bloburl)\n    client, err := azblob.newblockblobclient(bloburl, cred, nil)\n    if err != nil {\n        log.fatal(\"unable to create blob client\")\n    }\n    return client\n}\n<\/pre>\n<h3>\/\/\u6d4b\u8bd5<\/h3>\n<pre>package main\n\nimport (\n    \"os\"\n    \"testing\"\n\n    \"github.com\/Azure\/azure-sdk-for-go\/sdk\/storage\/azblob\"\n)\n\nfunc TestgetClient(t *testing.T) {\n    blob := \"text.txt\"\n    container := \"testcontainer\"\n    os.Setenv(\"AZURE_STORAGE_ACCOUNT_NAME\", \"mystorageaccount\")\n    client := getClient(blob, container)\n\n    _, ok := client.(azblob.BlockBlobClient)\n    if !ok {\n        t.Errorf(\"client should be type BlockBlobClient\")\n    }\n}\n<\/pre>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5982\u4f55\u7f16\u5199\u6d4b\u8bd5\u6765\u68c0\u67e5\u7279\u5b9a\u7c7b\u578b\u7684\u53d8\u91cf\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#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-207991","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207991","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=207991"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207991\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}