Errors with a Golang web app hosted in a Google App Engine environment; the app front-ends BigQuery

huangapple go评论75阅读模式
英文:

Errors with a Golang web app hosted in a Google App Engine environment; the app front-ends BigQuery

问题

我构建了一个使用Golang编写的Web应用程序,它作为Google BigQuery项目的前端。该应用程序有以下导入:

import (
    "context"
    "html/template"
    "log"
    "net/http"
    "regexp"
    "strings"
    "strconv"
    "cloud.google.com/go/bigquery"
    "google.golang.org/api/iterator"
)

还有一个用于BigQuery安全凭据的JSON文件。在本地,它在localhost:8080上运行得很完美。然后,我尝试使用Google App Engine进行托管,但遇到了一些问题。

对于Google App Engine部署,我首先在本地安装了Google Cloud SDK,运行了gcloud init,并安装了以下包:

gcloud components install app-engine-go
bq
core
gsutil
gcloud
beta
app-engine-python

我从main.go中删除了main()函数,并且项目目录中有一个YAML文件。我在项目目录中运行了以下命令:

gcloud config set project {正确的项目ID}

然后在DOS窗口中运行了以下命令:

gcloud app deploy

但是我得到了以下错误(格式化为SO并删除了私人信息):

C:\Golang Web Dev\golang-web-dev-master\bigqueryApp_AppEngine>gcloud app deploy

ERROR: (gcloud.app.deploy)
Staging command

[C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\goapp-stager.exe    
C:\Golang Web Dev\golang-web-dev-master\bigqueryApp_AppEngine\app.yaml    
C:\Golang Web Dev\golang-web-dev-master\bigqueryApp_AppEngine    
c:\-----\-----\appdata\local\temp\--------\--------]

failed with return code [1].

-------------------------------------STDOUT-------------------------------------
-------------------------------------STDERR-------------------------------------

2017/07/18 18:14:44 failed analyzing C:\Golang Web Dev\golang-web-dev-master\bigqueryApp_AppEngine:

cannot find package "google.golang.org/appengine/socket" in any of:
    ($GOROOT not set)
    C:\Go Workspace\src\google.golang.org\appengine\socket (from $GOPATH)    
GOPATH: C:\Go Workspace\src\google

我追踪到这个错误是由导入的

"cloud.google.com/go/bigquery"

包引起的;另一个没有使用cloud.google.com/go/bigquery的“测试”应用程序可以正常工作。我尝试在应用程序中导入

google.golang.org/appengine/socket

包,但是我得到了另一个编译错误;看起来这个页面说不要去那里。接下来,我尝试了这个视频中的方法,使用原始应用程序,保留了main.go中的原始main()函数。我在Cloud Shell窗口中输入了以下命令:

gcloud app deploy

但是我得到了以下错误:

$ ---_---------@---------------X------:~/bigqueryApp
$ gcloud app deploy
ERROR: (gcloud.app.deploy) Staging command [/google/google-cloud-sdk/platform/google_appengine/goroot-1.6/bin/go-app-stager
/home/---_---------/bigqueryApp/app.yaml /tmp/---------/---------]
failed with return code [1].

------------------------------------ STDOUT ------------------------------------
------------------------------------ STDERR ------------------------------------

2017/07/18 21:30:23 failed analyzing /home/---_---------/bigqueryApp:
cannot find package "google.golang.org/api/iterator" in any of:
        ($GOROOT not set)
        /home/---_---------/gopath/src/google.golang.org/api/iterator (from $GOPATH)
        /google/gopath/src/google.golang.org/api/iterator
GOPATH: /home/---_---------/gopath:/google/gopath

很明显,该应用程序导入了iterator包。我进行了研究/实验等来修复这两种技术中的错误,但没有成功。如果有人对如何修复这些错误有想法,我会很感激。

谢谢!

英文:

I built a Golang web app that front-ends a Google BigQuery project. The app has these imports

import (
      "context"
      "html/template"
      "log"
      "net/http"
      "regexp"
      "strings"
      "strconv"
      "cloud.google.com/go/bigquery"
      "google.golang.org/api/iterator"
)

and a JSON file for the BigQuery security credentials. Locally, it works perfectly at localhost:8080. Then, I tried to host it with Google App Engine and I hit some bugs.

For Google App Engine deployment, I first installed Google Cloud SDK locally, I ran gcloud init, and I installed the

gcloud components install app-engine-go
bq
core
gsutil
gcloud
beta
app-engine-python

packages. I removed the main() function from main.go, and the project directory has a YAML file. I ran

gcloud config set project {correct project ID}

and in a DOS window, I ran

gcloud app deploy

at the project directory. I got this error (formatted for SO and to remove private information):

C:\Golang Web Dev\golang-web-dev-master\bigqueryApp_AppEngine>gcloud app deploy

ERROR: (gcloud.app.deploy)
Staging command

[C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\goapp-stager.exe    
C:\Golang Web Dev\golang-web-dev-master\bigqueryApp_AppEngine\app.yaml    
C:\Golang Web Dev\golang-web-dev-master\bigqueryApp_AppEngine    
c:\-----\-----\appdata\local\temp\--------\--------]

failed with return code [1].

-------------------------------------STDOUT-------------------------------------    
-------------------------------------STDERR-------------------------------------

2017/07/18 18:14:44 failed analyzing C:\Golang Web Dev\golang-web-dev-master\bigqueryApp_AppEngine:

cannot find package "google.golang.org/appengine/socket" in any of:
		($GOROOT not set)
		C:\Go Workspace\src\google.golang.org\appengine\socket (from $GOPATH)    
GOPATH: C:\Go Workspace\src\google

I traced this bug down to the imported

"cloud.google.com/go/bigquery"

package; another “test” app without cloud.google.com/go/bigquery works OK using this technique. I tried to import the

google.golang.org/appengine/socket

package in the app and I got another compile error; it looks like this page says don’t even go there. Next, I tried the ideas in this vid using the original application, keeping the original main() function in main.go. I typed

gcloud app deploy

in a Cloud Shell window. I got this

$ ---_---------@---------------X------:~/bigqueryApp
$ gcloud app deploy
ERROR: (gcloud.app.deploy) Staging command [/google/google-cloud-sdk/platform/google_appengine/goroot-1.6/bin/go-app-stager
/home/---_---------/bigqueryApp/app.yaml /tmp/---------/---------]
failed with return code [1].

------------------------------------ STDOUT ------------------------------------    
------------------------------------ STDERR ------------------------------------

2017/07/18 21:30:23 failed analyzing /home/---_---------/bigqueryApp:
cannot find package "google.golang.org/api/iterator" in any of:
        ($GOROOT not set)
        /home/---_---------/gopath/src/google.golang.org/api/iterator (from $GOPATH)
        /google/gopath/src/google.golang.org/api/iterator
GOPATH: /home/---_---------/gopath:/google/gopath

error. The app clearly imports the iterator package. I researched / experimented / etc. to fix the bugs in both techniques but no luck. If someone has ideas re: how to fix these bugs, I’d like to know them and I’d be grateful.

Thank you!

答案1

得分: 0

解决方案:

1)删除“context”导入

2)导入“google.golang.org/appengine”;请参阅https://github.com/golang/appengine/blob/master/README.md了解有关本地appengine包安装的更多详细信息

3)这个函数

http.HandleFunc("/", bqPage)

调用

bqPage(w http.ResponseWriter, req *http.Request)

作为处理程序函数。将第二个req参数传递给构建/调用bigquery客户端的代码:

ctx := appengine.NewContext(req)

// 从Google Cloud控制台获取projectID值:

projectID := "--------------"

// 创建一个客户端。

client, err := bigquery.NewClient(ctx, projectID)

一旦你有了客户端对象,你就可以开始了。

4)从指向包含main.go文件的目录的DOS窗口中运行

gcloud app deploy

然后用以下命令运行应用程序

gcloud app browse

英文:

Solution:

  1. Remove the "context" import

  2. Import "google.golang.org/appengine"; see

     [https://github.com/golang/appengine/blob/master/README.md][1]
    

    for more details re: the local appengine package installation

  3. This function

     http.HandleFunc("/", bqPage)
    

    calls

     bqPage(w http.ResponseWriter, req *http.Request)
    

    as the handler function. Pass that second req parameter down to the code that builds / calls the bigquery client:

     ctx := appengine.NewContext(req)
    
     // Get the projectID value from the Google Cloud Console:
    
     projectID := "--------------"
    
     // Create a client.
    
     client, err := bigquery.NewClient(ctx, projectID)
    

    Once you have the client object, you're in business.

  4. From a DOS window pointed to the directory hosting the main.go file, run

     gcloud app deploy
    

    and then run the app with

     gcloud app browse
    

huangapple
  • 本文由 发表于 2017年7月21日 12:05:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/45228766.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定