{"id":208289,"date":"2025-07-08T09:24:27","date_gmt":"2025-07-08T01:24:27","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208289\/"},"modified":"2025-07-08T09:24:27","modified_gmt":"2025-07-08T01:24:27","slug":"%e4%bd%bf%e7%94%a8-go-%e5%ae%a2%e6%88%b7%e7%ab%af%e9%83%a8%e7%bd%b2-gcp-%e4%ba%91%e5%8a%9f%e8%83%bd%e6%97%b6%ef%bc%8c%e6%88%91%e6%94%b6%e5%88%b0%ef%bc%9a%e9%94%99%e8%af%af-400%ef%bc%9a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208289\/","title":{"rendered":"\u4f7f\u7528 go \u5ba2\u6237\u7aef\u90e8\u7f72 GCP \u4e91\u529f\u80fd\u65f6\uff0c\u6211\u6536\u5230\uff1a\u201c\u9519\u8bef 400\uff1a\u5148\u51b3\u6761\u4ef6\u68c0\u67e5\u5931\u8d25\u3002\uff0cfailedPrecondition\u201d"},"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>\u4f7f\u7528 go \u5ba2\u6237\u7aef\u90e8\u7f72 GCP \u4e91\u529f\u80fd\u65f6\uff0c\u6211\u6536\u5230\uff1a\u201c\u9519\u8bef 400\uff1a\u5148\u51b3\u6761\u4ef6\u68c0\u67e5\u5931\u8d25\u3002\uff0cfailedPrecondition\u201d<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-05-01 12:33:37<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4f7f\u7528 go \u5ba2\u6237\u7aef\u90e8\u7f72 GCP \u4e91\u529f\u80fd\u65f6\uff0c\u6211\u6536\u5230\uff1a\u201c\u9519\u8bef 400\uff1a\u5148\u51b3\u6761\u4ef6\u68c0\u67e5\u5931\u8d25\u3002\uff0cfailedPrecondition\u201d\u300b<\/span>\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u4f7f\u7528 google \u7684 go \u5ba2\u6237\u7aef\u5305\u90e8\u7f72 google cloud function\u3002 <\/p>\n<p>\uff08https:\/\/pkg.go.dev\/google.golang.org\/api\/cloudfunctions\/v1?tab=doc#pkg-overview\uff09<\/p>\n<p>\u6211\u5df2\u5c06\u5176\u5206\u89e3\u4e3a\u6211\u8ba4\u4e3a\u6700\u76f8\u5173\u7684\u7247\u6bb5\uff1a<\/p>\n<pre>import (\n    \"context\"\n    log \"github.com\/sirupsen\/logrus\"\n    functions \"google.golang.org\/api\/cloudfunctions\/v1\"\n)\n\nfunc main() {\n    ctx := context.background()\n    cloudfunctionservice, err := functions.newservice(ctx)\n    if err != nil {\n        log.printf(\"error at functions.newservice(ctx): \\\"%v\\\"\\n\", err)\n    }\n    functionspec := functions.cloudfunction{\n            entrypoint:        \"deploythisfunctionentrypoint\",\n            eventtrigger: &amp;functions.eventtrigger{\n                eventtype: \"google.pubsub.topic.publish\",\n                resource:  \"projects\/mytestproject\/topics\/cloud-builds\",\n            },\n            name:                \"deploythisfunction\",\n            runtime:             \"go111\",\n            sourcerepository:    &amp;functions.sourcerepository{url: \"https:\/\/source.developers.google.com\/projects\/mytestproject\/repos\/deploythisfunction\/moveable-aliases\/master\/paths\/\/\"},\n    }\n    cloudfunctiondeploymentservice := functions.newprojectslocationsfunctionsservice(cloudfunctionservice)\n    createcall := cloudfunctiondeploymentservice.create(\"projects\/mytestproject\/locations\/us-central1\", &amp;functionspec)\n    resp, err := createcall.context(ctx).do()\n    if err != nil {\n        log.printf(\"error at createcall.context(ctx).do(): \\\"%v\\\"\\n\", err)\n    }\n    log.printf(\"response createcall.context(ctx).do(): \\\"%v\\\"\\n\", resp)\n}<\/pre>\n<p>\u4f46\u662f\uff0c\u65e0\u8bba\u6211\u5982\u4f55\u683c\u5f0f\u5316\u5b83\uff0c\u8fd8\u662f\u5c1d\u8bd5\u4e00\u4e0b\u3002\u6211\u603b\u662f\u6536\u5230\u4ee5\u4e0b\u6d88\u606f\uff1a<\/p>\n<pre>googleapi: error 400: precondition check failed., failedprecondition<\/pre>\n<p>\u901a\u8fc7 google api explorer\uff0c\u6211\u4f7f\u7528\u4ed6\u4eec\u7684\u8eab\u4efd\u9a8c\u8bc1\u548c json \u65b9\u6848\u8fd0\u884c\u4e86\u8bf7\u6c42\uff0c\u5e76\u4e14\u6536\u5230\u4e86\u76f8\u540c\u7684\u9519\u8bef\u3002 <\/p>\n<p>https:\/\/cloud.google.com\/functions\/docs\/reference\/rest\/v1\/projects.locations.functions\/create<\/p>\n<p>\u56de\u5e94\uff1a<\/p>\n<pre>{\n  \"error\": {\n    \"code\": 400,\n    \"message\": \"Precondition check failed.\",\n    \"status\": \"FAILED_PRECONDITION\"\n  }\n}<\/pre>\n<p>\u6211\u4e0d\u77e5\u9053\u51fa\u4e86\u4ec0\u4e48\u95ee\u9898\u3002\u6211\u5df2\u7ecf\u5f00\u59cb\u4e86\u81ea\u5df1\u7684\u9879\u76ee\u5e76\u4e14\u662f\u7ba1\u7406\u5458\u3002\u5f53\u4f7f\u7528 gcp \u8fd0\u884c go \u5ba2\u6237\u7aef\u7684\u53e6\u4e00\u90e8\u5206\uff08\u4f8b\u5982\u521b\u5efa\u5b58\u50a8\u3001iam \u7528\u6237\u3001\u670d\u52a1\u5e10\u6237\u3001\u6570\u636e\u5e93\uff09\u65f6\uff0c\u6211\u53ef\u4ee5\u4f7f\u5176\u5de5\u4f5c\u5e76\u521b\u5efa\u8fd9\u4e9b\u8d44\u6e90\u3002 <\/p>\n<p>\u5982\u679c\u6709\u4eba\u9047\u5230\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u5c06\u4e0d\u80dc\u611f\u6fc0\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p> \u5e94\u5339\u914d\u6a21\u5f0f\uff1a<code>providers\/*\/eventTypes\/*.*\u3002 <\/code> \u3002<\/p>\n<p>\u4f8b\u5982\uff1a<code>providers\/cloud.pubsub\/eventTypes\/topic.publish<\/code><\/p>\n<p>SourceRepository url \u4e5f\u5e94\u4e3a <code>https:\/\/source.developers.google.com\/projects\/*\/repos\/*\/revisions\/*\/paths\/<\/code> \u5e76\u4e14\u60a8\u6709 <code>https:\/\/source.developers.google.com\/projects\/mytestproject\/repos\/deploythisfunction\/moveable-aliases\/master\/paths\/\/<\/code><\/p>\n<p>\u9519\u8bef\u6d88\u606f\u8868\u660e\u60a8\u914d\u7f6e <code>FunctionSpec<\/code> \u7684\u65b9\u5f0f\u5b58\u5728\u95ee\u9898\uff0c\u6211\u6000\u7591 <code>EventTrigger<\/code> \u6216 <code>SourceRepository<\/code> \u5b57\u6bb5\u3002<\/p>\n<h1>\u7f16\u8f91<\/h1>\n<p><code>code: 400<\/code> \u662f\u4e00\u4e2a\u9519\u8bef\u7684\u8bf7\u6c42\uff0c\u5ba2\u6237\u7aef\u9519\u8bef\uff0c\u5728\u672c\u4f8b\u4e2d\u662f\u683c\u5f0f\u95ee\u9898\uff0c\u9996\u5148\u8981\u68c0\u67e5\u7684\u662f\u6bcf\u4e2a <\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u4f7f\u7528 go \u5ba2\u6237\u7aef\u90e8\u7f72 GCP \u4e91\u529f\u80fd\u65f6\uff0c\u6211\u6536\u5230\uff1a\u201c\u9519\u8bef 400\uff1a\u5148\u51b3\u6761\u4ef6\u68c0\u67e5\u5931\u8d25\u3002\uff0cfailedPrecondition\u201d\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\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\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-208289","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208289","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=208289"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208289\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}