【问题标题】:How do I deploy a Go Google Cloud Function placed in a subpackage?如何部署放置在子包中的 Go Google Cloud 函数?
【发布时间】:2020-02-02 12:47:17
【问题描述】:

我有以下设置,我正在尝试创建一个多功能 GCP Cloud Function 项目,但由于“通用”错误,我无法部署任何功能,这根本不是很有帮助。

我的项目结构是这样的,为了简单起见,我只写了function1,但是还有function2等等。

使用云函数go 1.13 支持和go mod 支持

项目结构:

.
├── go.mod
├── function1
│   └── function1.go
├── common
│   └── common.go // Contains struct GCSEvent see https://cloud.google.com/functions/docs/calling/storage#functions-calling-storage-go
└── main.go

代码sn-ps:

// go.mod
module github.com/me/myproject

go 1.13
// function1/function1.go
package function1

import (
    "github.com/me/myproject/common"
    // Other imports
)

func Function1(ctx context.Context, gcsEvent common.GCSEvent) error {
    // Do Stuff
}
// common/common.go
package common

// See https://cloud.google.com/functions/docs/calling/storage#functions-calling-storage-go
type GCSEvent struct {
    // ...
}
// main.go
package main

import (
    "github.com/me/myproject/function1"
    "github.com/me/myproject/common"
    "context"
    // Other imports
)

func main() {
    // Call Function1 from the command line for manual testing
    event := common.GCSEvent{/*...*/}
    function1.Function1(context.Background(), event)
}

function1.go 使用 GCP 应该能够解析的 go 模块导入语法引用 common.go

从项目的根目录部署我调用gcloud的函数:

gcloud functions deploy Function1 --runtime=go113 <more arguments...>

我收到的错误是

错误:(gcloud.functions.deploy)操作错误:代码=3,消息=构建 失败:构建错误详细信息不可用

我提到的一点用处都没有。

【问题讨论】:

标签: go google-cloud-platform google-cloud-functions


【解决方案1】:

解决第一个错误

错误:(gcloud.functions.deploy)操作错误:代码=3,消息=构建 失败:构建错误详细信息不可用

我必须意识到 Cloud Functions 只能(?)从项目的根目录部署,但我的 Cloud Functions 位于子包function1/

为了解决这个问题,我在项目根目录的新文件functions.go 中创建了一个代理函数。

// functions.go DO NOT USE SEE BELOW
// functions.go DO NOT USE SEE BELOW
// functions.go DO NOT USE SEE BELOW
package main // Please note that this package is _wrong_ which will become clear further down in the answer

import (
    "github.com/me/myproject/function1"
    "github.com/me/myproject/common"
    "context"
)

// Proxy function to the real Function1 in package function1
func Function1(ctx context.Context, gcsEvent common.GCSEvent) error {
    return backupdelegate.Function1(ctx, gcsEvent)
}

部署这似乎成功,直到我收到一条新的错误消息,有点不那么混乱但仍然令人困惑:

错误:(gcloud.functions.deploy)操作错误:代码=3,消息=构建 失败:去:下载 github.com/GoogleCloudPlatform/functions-framework-go v0.3.0

我检查了我的go.modgo.sum,但它们没有包含对functions-framework-go 的任何引用。这是 GCP “为我”下载的东西,而不是仅仅告诉我我做错了什么。

直到我阅读了GoogleCloudPlatform/functions-framework-go 的描述,我才意识到这可能与我的代码的主要功能有关。

在测试了一些东西后,我得出的结论是:

  • 代理函数 1 不得位于名为 main 的包中
  • 为了保持 main.go 文件的可执行性,我不得不将它移动到另一个目录

最终结构和文件修改:

.
├── go.mod
├── function1
│   └── function1.go
├── common
│   └── common.go
├── testing
│   └── main.go
└── functions.go
// functions.go
// THIS IS THE CORRECT VERSION OF THE PROXY FUNCTION(S)
package functions

import (
    "github.com/me/myproject/function1"
    "github.com/me/myproject/common"
    "context"
)

// Proxy function to the real Function1 in package function1
func Function1(ctx context.Context, gcsEvent common.GCSEvent) error {
    return backupdelegate.Function1(ctx, gcsEvent)
}

从项目结构中可以看出,我不得不将 main.go 移动到一个子文件夹中,因为 go 只允许每个文件夹一个包。

在这些最后的更改之后,功能部署成功。

这个答案帮助我构建了子包,去给它一些爱吧: How can I use a sub-packages with Go on Google Cloud Functions?

作为最后一点,我想对 GCP Cloud Functions 文档进行一些说明,该文档缺少一些高级示例,并且没有非常清楚地说明包命名、函数放置等 - 至少对于去项目。

【讨论】:

    猜你喜欢
    • 2019-09-23
    • 2019-06-12
    • 2019-12-30
    • 2021-03-09
    • 1970-01-01
    • 2022-01-08
    • 2018-05-05
    • 2023-04-07
    • 2021-06-21
    相关资源
    最近更新 更多