无法存储在数据存储区gcloud中

时间:2017-09-26 07:34:10

标签: google-app-engine go google-cloud-datastore

我正在学习golang和google app引擎数据存储区。

我打算做一个简单的rest api调用来保存和检索数据存储区中的数据。

我遵循了几个hello world教程和官方入门指南。最后我想出的就是这个。

`

sql = "insert into Files values ('{}')".format(MySQLdb.escape_string(dp))
cursor.execute(sql)

`

现在我遇到的问题是在这一行

package hello import ( "fmt" "log" "net/http" "encoding/json" "cloud.google.com/go/datastore" "golang.org/x/net/context" "github.com/gorilla/mux" "time" ) type Task struct { Description string `datastore:"description"` Created time.Time `datastore:"created"` id int64 } func saveData(w http.ResponseWriter, r *http.Request){ fmt.Println("Endpoint Hit: saveData") input := r.URL.Query().Get("input") fmt.Println(input) ctx := context.Background() projectID := "api-project-426361742627" client, err := datastore.NewClient(ctx, projectID) if err != nil { log.Fatalf("Failed to create client: %v", err) } kind := "Task" task := Task{ Description: input, Created: time.Now(), } taskKey := datastore.IncompleteKey(kind, nil) if _, err := client.Put(ctx, taskKey, &task); err != nil { log.Fatalf("Failed to save task: %v", err) } fmt.Printf("Saved %v: %v\n", taskKey, task.Description) json.NewEncoder(w).Encode(task) } func returnAll(w http.ResponseWriter, r *http.Request){ fmt.Println("Endpoint Hit: returnAll") ctx := context.Background() projectID := "api-project-426361742627" client, err := datastore.NewClient(ctx, projectID) if err != nil { log.Fatalf("Failed to create client: %v", err) } query := datastore.NewQuery("Task").Order("description") var tasks []*Task keys, err := client.GetAll(ctx, query, &tasks) for i, key := range keys { tasks[i].id = key.ID } // for _, t := range tasks { // // fmt.Println( t.id, t.Description) // } json.NewEncoder(w).Encode(tasks) } func homePage(w http.ResponseWriter, r *http.Request){ fmt.Fprintf(w, "Welcome to the HomePage!") fmt.Println("Endpoint Hit: homePage") } func handleRequests() { myRouter := mux.NewRouter() myRouter.HandleFunc("/", homePage) myRouter.HandleFunc("/save", saveData) myRouter.HandleFunc("/retrieve", returnAll) http.Handle("/", myRouter) } func init() { handleRequests() }

我收到以下错误。


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

我不确定如何解决这个问题。我尝试使用

2017/09/26 07:15:16 http: panic serving 127.0.0.1:59488: not an App Engine context
goroutine 17 [running]:
net/http.(*conn).serve.func1(0xc82021e000)
    /home/aditya/google-cloud-sdk/platform/google_appengine/goroot-1.6/src/net/http/server.go:1389 +0xc1
panic(0xa44740, 0xc82018a8c0)
    /home/aditya/google-cloud-sdk/platform/google_appengine/goroot-1.6/src/runtime/panic.go:443 +0x4e9
google.golang.org/appengine/internal.fullyQualifiedAppID(0x7f4142e12be0, 0xc82000b4e0, 0x0, 0x0)
    /home/aditya/work/src/google.golang.org/appengine/internal/identity_classic.go:54 +0x8b
google.golang.org/appengine/internal.FullyQualifiedAppID(0x7f4142e12be0, 0xc82000b4e0, 0x0, 0x0)
    /home/aditya/work/src/google.golang.org/appengine/internal/api_common.go:77 +0xe2
google.golang.org/appengine/internal.AppID(0x7f4142e12be0, 0xc82000b4e0, 0x0, 0x0)
    /home/aditya/work/src/google.golang.org/appengine/internal/identity.go:13 +0x37
google.golang.org/appengine.AppID(0x7f4142e12be0, 0xc82000b4e0, 0x0, 0x0)
    /home/aditya/work/src/google.golang.org/appengine/identity.go:20 +0x37
golang.org/x/oauth2/google.FindDefaultCredentials(0x7f4142e12be0, 0xc82000b4e0, 0xc820222170, 0x1, 0x1, 0xc820222150, 0x0, 0x0)
    /home/aditya/work/src/golang.org/x/oauth2/google/default.go:92 +0x5a8
google.golang.org/api/internal.Creds(0x7f4142e12be0, 0xc82000b4e0, 0xc820248240, 0xc8202452c0, 0x0, 0x0)
    /home/aditya/work/src/google.golang.org/api/internal/creds.go:37 +0x173
google.golang.org/api/transport/grpc.Dial(0x7f4142e12be0, 0xc82000b4e0, 0xc820245638, 0x3, 0x3, 0x0, 0x0, 0x0)
    /home/aditya/work/src/google.golang.org/api/transport/grpc/dial.go:47 +0x272
cloud.google.com/go/datastore.NewClient(0x7f4142e12be0, 0xc82000b4e0, 0xc0c320, 0x18, 0x0, 0x0, 0x0, 0x9, 0x0, 0x0)
    /home/aditya/work/src/cloud.google.com/go/datastore/datastore.go:87 +0x87f
main76346.returnAll(0x7f4142dcb058, 0xc820234000, 0xc82022a000)
    hello.go:51 +0x1b8
net/http.HandlerFunc.ServeHTTP(0xcd13d8, 0x7f4142dcb058, 0xc820234000, 0xc82022a000)
    /home/aditya/google-cloud-sdk/platform/google_appengine/goroot-1.6/src/net/http/server.go:1618 +0x3a
github.com/gorilla/mux.(*Router).ServeHTTP(0xc820052660, 0x7f4142dcb058, 0xc820234000, 0xc82022a000)
    /home/aditya/work/src/github.com/gorilla/mux/mux.go:133 +0x37c
net/http.(*ServeMux).ServeHTTP(0xc82000eab0, 0x7f4142dcb058, 0xc820234000, 0xc82022a000)
    /home/aditya/google-cloud-sdk/platform/google_appengine/goroot-1.6/src/net/http/server.go:1910 +0x17d
appengine_internal.handleFilteredHTTP(0x7f4142dcb058, 0xc820234000, 0xc82022a000)
    /home/aditya/google-cloud-sdk/platform/google_appengine/goroot-1.6/src/appengine_internal/api_dev.go:102 +0x409
net/http.HandlerFunc.ServeHTTP(0xccfe48, 0x7f4142dcb058, 0xc820234000, 0xc82022a000)
    /home/aditya/google-cloud-sdk/platform/google_appengine/goroot-1.6/src/net/http/server.go:1618 +0x3a
net/http.serverHandler.ServeHTTP(0xc820078500, 0x7f4142dcb058, 0xc820234000, 0xc82022a000)
    /home/aditya/google-cloud-sdk/platform/google_appengine/goroot-1.6/src/net/http/server.go:2081 +0x19e
net/http.(*conn).serve(0xc82021e000)
    /home/aditya/google-cloud-sdk/platform/google_appengine/goroot-1.6/src/net/http/server.go:1472 +0xf2e
created by net/http.(*Server).Serve
    /home/aditya/google-cloud-sdk/platform/google_appengine/goroot-1.6/src/net/http/server.go:2137 +0x44e
INFO     2017-09-26 07:15:16,800 module.py:821] default: "GET /retrieve HTTP/1.1" 500 3349

在本地提供对数据存储的访问权限,但它无法正常工作。每当我尝试保存数据或在初始化数据存储区新客户端时返回所有条目,我都会遇到此问题。

1 个答案:

答案 0 :(得分:2)

更新处理程序以使用从应用程序处理程序请求而非context.Background()

派生的上下文
func saveData(w http.ResponseWriter, r *http.Request){
    // ..
    fmt.Println(input)

    ctx := appengine.NewContext(r)
    projectID := "api-project-426361742627"
    // ...
}

请记住,上下文也可以用作不透明的值容器,而app引擎环境则会大量使用此功能。

包裹appengine docs