Gre*_*ray 3 google-cloud-platform google-cloud-firestore google-cloud-run
我在这里使用 Go SDK @ cloud.google.com/go/firestore GoDoc来调用 Firestore。它在 App Engine 上运行得很好,但我最近向 Cloud Run 添加了一项新服务。此新服务调用旧服务使用的相同代码,但该代码(适用于 App Engine)在 Cloud Run 上引发错误:rpc error: code = InvalidArgument desc = Invalid resource field value in the request
引发错误的特定代码行是:
snap, err := client.Collection(queryable.CollectionName()).Doc(queryable.GetFirestoreID()).Get(ctx)
if err != nil {
return fmt.Errorf("couldn't read document with id: %v error: %w", queryable.GetFirestoreID(), err)
}
Run Code Online (Sandbox Code Playgroud)
其中 queryable 是一个接口,而 CollectionName() 返回一个常量字符串,因此不可能是问题(此外,如果这是问题,它将返回不同的错误)。
我用更多文本包装错误消息,因此完整的错误消息可以让您了解堆栈:DoTaxDog: allDB.UpdateData: firestoreCRUD.ReadSchema: error in Firestore's ReadSchema: couldn't read document with id: AGPFiyoJdMAmurVNqHYxvKHEb error: rpc error: code = InvalidArgument desc = Invalid resource field value in the request.
该allDB.UpdateData函数在此代码和 App Engine 中运行的代码之间共享,因此出于所有意图和目的,我将代码在 App Engine 上运行的代码和我在 Cloud Run 上运行的代码是相同的。
我在 Gitlab 中部署了这个:
deploy_run:
image: google/cloud-sdk
stage: deploy
only:
- master
script:
- echo $DEPLOY_KEY > /tmp/$CI_PIPELINE_ID.json
- gcloud auth activate-service-account --key-file /tmp/$CI_PIPELINE_ID.json
- gcloud builds submit --tag gcr.io/project-name/project-name --project project-name
- gcloud run deploy project-name --image gcr.io/project-name/project-name --platform managed --project project-name --region=us-central1 --allow-unauthenticated
Run Code Online (Sandbox Code Playgroud)
我以前也遇到过这个问题,但我不记得上次是如何解决的,所以我写了这篇文章。
Gre*_*ray 13
固定的!Cloud Run 不会设置 GOOGLE_CLOUD_PROJECT环境变量。当我通过此 Go 代码初始化 Firestore 客户端时:firestore.NewClient(ctx, projectID, option.WithCredentials(credentials))变量projectID的值是空字符串。如果未设置projectID变量,Firestore的Go SDK函数NewClient不会抛出错误。相反,当您第一次发出请求时,它会从底层服务返回 rpc 错误,这就是我最初看到的:rpc error: code = InvalidArgument desc = Invalid resource field value in the request
为了解决这个问题,我将此代码添加到我的gcloud run deploy命令中:--set-env-vars=GOOGLE_CLOUD_PROJECT=project-name
感谢向我询问有关代码本身的更多详细信息的评论者!
[更新] 我在 Github 中打开了一个问题,并在 PR 中修复了该问题,该 PR 已被接受。该问题已被解决,不应再次发生。
[更新]仅供参考,仅修复了该问题的一个实例,因此其他 SDK 函数调用仍可能会出现此问题。
| 归档时间: |
|
| 查看次数: |
10401 次 |
| 最近记录: |