dtgta48604 2017-07-21 04:05
浏览 53
已采纳

在Google App Engine环境中托管的Golang网络应用出现错误; 应用前端BigQuery

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条回答 默认 最新

  • duanchu7271 2017-07-24 21:38
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分 合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备