如果我在控制台中部署例子:你好世界,url/触发器就能工作。如果我从命令行部署,它在云函数控制台中看起来是完全相同的代码/属性,但是url是404。我找不出区别/问题。
如果从命令行以这种方式部署,则部署的触发器/url显示-- "404页未找到“,用于下面的hello world示例。
gcloud functions deploy hellogo --entry-point=HelloWorld --trigger-http --region=us-central1 --memory=128MB --runtime=go116 --allow-unauthenticated
// Package p contains an HTTP Cloud Function.
package p
import (
"encoding/json"
"fmt"
"html"
"io"
"log"
"net/http"
)
// HelloWorld prints the JSON encoded "message" field in the body
// of the request or "Hello, World!" if there isn't one.
func HelloWorld(w http.ResponseWriter, r *http.Request) {
var d struct {
Message string `json:"message"`
}
if err := json.NewDecoder(r.Body).Decode(&d); err != nil {
switch err {
case io.EOF:
fmt.Fprint(w, "Hello World!")
return
default:
log.Printf("json.NewDecoder: %v", err)
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
}
if d.Message == "" {
fmt.Fprint(w, "Hello World!")
return
}
fmt.Fprint(w, html.EscapeString(d.Message))
}
发布于 2022-02-15 11:17:14
谢谢大家,我被困的项目比这个函数有更多的代码。在试图在大型项目中部署单个函数/文件之后,我走上了这条道路。如果我简化为只有一个hello.go和go.mod的文件夹,那么它确实可以:-/从命令行部署它:
gcloud functions deploy hellogo --entry-point=HelloWorld --trigger-http --region=us-central1 --memory=128MB --runtime=go116 --allow-unauthenticated
// go.mod
module github.com/nickfoden/hello
go 1.16感谢您的快速答复和帮助。与其尝试在现有项目中使用更大的go.sum、多个文件夹、现有的服务器/api等创建单个函数,我将从这里开始使用一个带有云函数的文件,并在此基础上构建,并查看在哪一点/是否再次陷入卡住状态。
发布于 2022-02-14 07:46:21
试图复制你的错误,但我无法复制。我使用了与您相同的命令,并且可以通过HTTP触发器成功地访问url或调用部署的hello world云函数。
gcloud functions deploy hellogo --entry-point=HelloWorld --trigger-http --region=us-central1 --memory=128MB --runtime=go116 --allow-unauthenticated
成功卷曲的输出:

我建议您检查您试图访问的url,因为根据这个GCP 文档
如果您试图调用不存在的函数,云函数将使用HTTP/2302重定向响应,该重定向将您带到Google帐户登录页面。这是错误的。它应该使用HTTP/2404错误响应代码进行响应。这个问题正在得到解决。
解决方案
确保正确地指定了函数的名称。您可以始终使用gcloud函数调用进行检查,该调用返回丢失函数的正确404错误。
您还可以参考这个完整的指南,使用Go运行时快速启动CF的创建和部署。
https://stackoverflow.com/questions/71087844
复制相似问题