【发布时间】:2015-11-16 19:25:45
【问题描述】:
如果我有appengine.Context 而不是context.Context,我不知道如何调用cloud.WithContext 和google.DefaultClient。
有(旧)“appengine”和(新)“google.golang.org/appengine”包。第一个带有自定义appengine.Context,而第二个带有来自"golang.org/x/net/context"的context.Context
整个google.golang.org/cloud 只需要context.Context。
我很乐意改用新的"google.golang.org/appengine",但我坚持使用尚未移植的runtime.RunInBackground。来自https://github.com/golang/appengine:
appengine/aetest、appengine/cloudsql和appengine/runtime尚未移植。
如果appengine/runtime 已经被移植,我可以写什么:
import (
"golang.org/x/net/context"
"google.golang.org/appengine"
"google.golang.org/appengine/runtime"
"google.golang.org/cloud"
"google.golang.org/cloud/storage"
)
func handler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
runtime.RunInBackground(c, func(ctx context.Context) {
hc, _ := google.DefaultClient(ctx, storage.ScopeFullControl)
cc := cloud.WithContext(ctx, appengine.AppID(ctx), hc)
…
})
}
但是还没有"google.golang.org/appengine/runtime"。所以我有
runtime.RunInBackground(c, func(ctx appengine.Context) {
【问题讨论】:
-
如果我使用
oauth2.NoContext耍花招,我会收到错误消息“不是 App Engine 上下文” -
有函数
BackgroundContext() context.Context但它仅适用于托管虚拟机 github.com/golang/appengine/blob/master/appengine_vm.go#L30 看来我应该使用托管虚拟机来完成我的任务
标签: google-app-engine go google-cloud-platform