使用Go Runtime Google App Engine时出现Google Cloud Storage Client App错误

Kar*_*Rao 4 google-app-engine go google-cloud-storage google-cloud-platform

我正在尝试使用此链接中的示例代码并尝试使用来自Go运行时的Google云端存储客户端应用程序在Google云端存储上执行操作,但示例代码中的以下部分给出错误"无法使用c(类型"appengine".Context )作为类型context.Context in function argument: "appengine".Context不实现context.Context(缺少截止日期方法)"

c := appengine.NewContext(r)
hc := &http.Client{
    Transport: &oauth2.Transport{
        Source: google.AppEngineTokenSource(c, storage.ScopeFullControl),
        Base:   &urlfetch.Transport{Context: c},
    },
}
Run Code Online (Sandbox Code Playgroud)

这是什么问题?我怎么解决这个?

icz*_*cza 5

错误消息明确指出您尝试传递appengine.Context期望类型所在的类型值context.Context.

google.AppEngineTokenSource()函数需要一个类型的值,context.Context而不是您传递的值(类型appengine.Context).

您可以Context使用以下功能创建:

cloud.NewContext(projID string, c *http.Client)

我就是这样做的:

c := appengine.NewContext(r)
hc := &http.Client{}
ctx := cloud.NewContext(appengine.AppID(c), hc)
hc.Transport = &oauth2.Transport{
    Source: google.AppEngineTokenSource(ctx, storage.ScopeFullControl),
    Base:   &urlfetch.Transport{Context: c},
}
Run Code Online (Sandbox Code Playgroud)