dongqu4443 2019-02-23 14:48
浏览 57
已采纳

当源代码包含多个级别/目录时部署Google Cloud Function

I want to deploy a Google Cloud Function, written in Go, with a code structure containing a subdirectory, like this :

function
├── module1
│   ├── go.mod
│   └── module1.go
├── go.mod
└── entrypoint.go

But when I deploy the function, using the GCP console or gcloud command :

# from function/ directory
gcloud functions deploy myfunction --runtime go111 [...]

Only go.mod and entrypoint.go are uploaded (I checked on the Source tab of Function details in the GCP Console). Thus the function fails to deploy, because obviously entrypoint.go use methods from module1/module1.go.

The same happens if the source is a .zip (containing multiple directories) on Google Cloud Storage :

gcloud functions deploy myfunction \
    --runtime go111 \
    --source gs://${BUCKET}/function.zip [...]

Is it possible to deploy functions using a code structure with subdirectories? I don't know if the same happens for other runtimes (Python, NodeJS), or if the problem is specific to Go.

Edit

I tried to follow this guide : https://cloud.google.com/functions/docs/writing/#functions-writing-file-structuring-go (2nd point : A package at the root of your project that imports code from a sub-package and exports one or more functions), as suggested in the comments, but without success. Here's the structure I used (works in local) :

.
├── function.go
├── go.mod
└── shared
    ├── go.mod
    └── shared.go
go.mod
module testcloudfunction

require testcloudfunction/shared v0.0.0

replace testcloudfunction/shared => ./shared
function.go
package function

import (
    "fmt"

    "testcloudfunction/shared"
)

func HelloWorld(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, shared.Hello())
}
shared/go.mod
module testcloudfunction/shared
shared/shared.go
package shared

func Hello() string {
    return "Hello World!"
}
  • 写回答

1条回答 默认 最新

  • duanhong1985 2019-02-25 18:05
    关注

    OK. It works for me with some changes.

    I'm using GOPATH and so am prefixing with GO111MODULE=on. If you are outside of GOPATH, I think you can just drop the GO111MODULE=on environment setting.

    Starting with directories and .go files only (no .mod files).

    My path is github.com/DazWilkin/cloudfuncs.

    IIUC you will need to -- minimally -- prefix module paths with example.com.

    package function
    
    import (
        "fmt"
        "net/http"
        "github.com/DazWilkin/cloudfuncs/shared"
    )
    
    func HelloFreddie(w http.ResponseWriter, r *http.Request) {
        fmt.Fprint(w, shared.Hello())
    }
    

    Then, from within my cloudfuncs directory:

    GO111MODULE=on go mod init github.com/DazWilkin/test
    

    Results in a go.mod:

    module github.com/DazWilkin/cloudfuncs
    
    go 1.11
    

    There is no go.mod file in .../cloudfuncs/shared.

    Then I deploy using:

    gcloud functions deploy HelloFreddie \
    --region=us-central1 \
    --entry-point=HelloFreddie \
    --runtime=go111 \
    --source=$PWD/cloudfuncs \
    --project=${PROJECT} \
    --trigger-http
    

    You can see the result here: https://us-central1-dazwilkin-190225-54842789.cloudfunctions.net/HelloFreddie

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵