GCP 云函数在命令行部署时返回 404 错误
时间:2024-02-16
浏览:261
作者:佚名
对于一个Golang开发者来说,牢固扎实的基础是十分重要的,golang学习网就来带大家一点点的掌握基础知识点。今天本篇文章带大家了解《GCP 云函数在命令行部署时返回 404 错误》,主要介绍了,希望对大家的知识积累有所帮助,快点收藏起来吧,否则需要时就找不到了!
如果我在控制台中部署示例 hello world,则 url/trigger 会起作用。如果我从命令行部署,它看起来与云函数控制台中的代码/属性完全相同,但 url 是 404。我无法发现差异/问题。
如果从命令行以这种方式部署,则部署的触发器/url 在下面的 hello world 示例中显示“404 页面未找到”。
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))
}
正确答案
尝试重现您的错误,但我无法复制。我使用了与您相同的命令,并且可以成功访问 url 或通过 http 调用触发部署的 hello world 云函数。
gcloud functions deploy hellogo --entry-point=helloworld --trigger-http --region=us-central1 --memory=128mb --runtime=go116 --allow-unauthenticated
谢谢大家,我陷入困境的项目中有更多代码,而不仅仅是这个函数。在尝试在较大的项目中部署单个函数/文件后,我走上了这条道路。如果我简化为仅包含 hello.go 和 go.mod 的文件夹,它确实可以工作:-/从命令行部署它:
gcloud函数部署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 等的现有项目中创建单个函数。我将从这里开始,拥有一个具有云函数的单个文件,并在其之上进行构建,然后查看如果我再次陷入困境,那该怎么办?
今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~
本文内容来源于互联网,如有侵权请联系删除。
近期热门
站内热门
最新文章
热门专题
