我正在尝试运行一些示例代码,这些代码使用firestore服务器中的云函数存储一些随机数据,但是,云功能部署命令拒绝构建命令:
// Package p contains an HTTP Cloud Function.
package p
import (
//...
firebase "firebase.google.com/go"
"log"
"net/http"
"os"
)
// Store1 Stores data on FireBase
func Store1(w http.ResponseWriter, r *http.Request) {
// Use the application default credentials
ctx := context.Background()
conf := &firebase.Config{ProjectID: "firefirefire"}
app, err := firebase.NewApp(ctx, conf)
if err != nil {
log.Fatalln(err)
}
client, err := app.Firestore(ctx)
if err != nil {
log.Fatalln(err)
}
defer client.Close()
_, _, err = client.Collection("users").Add(ctx, map[string]interface{}{
"first": "Ada",
"last": "Lovelace",
"born": 1815,
})
if err != nil {
log.Fatalf("Failed adding alovelace: %v", err)
}
fmt.Println("ENV:" + os.Getenv("VAR1"))
fmt.Fprint(w, html.EscapeString(d.Message))
}
这就是我的错误:
localhost:store1 b$ ./deploy.sh
Updated property [functions/region].
Deploying function (may take a while - up to 2 minutes)...failed.
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: /tmp/sgb/gopath/src/serverlessapp/vendor/p/store1.go:6:2: cannot find package "firebase.google.com/go" in any of:
/tmp/sgb/gopath/src/serverlessapp/vendor/firebase.google.com/go (vendor tree)
/go/src/firebase.google.com/go (from $GOROOT)
/tmp/sgb/gopath/src/firebase.google.com/go (from $GOPATH)
您可以看到,问题似乎是Google在云功能引擎上没有firebase.com/go,因此我无法让我的无服务器配置做firebase的事情。我应该搬到Cloudsql并支付11美元的费用吗?我应该继续尝试让Firebase工作吗?我应该尝试使用firebase功能吗?请建议。
根据您的描述和代码,如果我清楚地理解您的问题,您正在尝试触发HTTP Cloud函数以将数据写入Cloud Firestore。部署云功能时,将" firebase.google.com/go"打包"找不到云"。becasue" firebase.google.com/go",这是Firebase Admin SDK的切入点。但是,云功能,已安装了用于GO的Google Cloud客户端库。根据您的要求,Firebase的云功能似乎可以提供您需要的解决方案。