使用Go Runtime的Google App Engine时,Google Cloud Storage客户端应用程序出现错误。

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

Google Cloud Storage Client App error using Go Runtime Google App Engine

问题

我正在尝试从这个链接中使用示例代码,并尝试在Go运行时使用Google Cloud Storage客户端应用程序对Google Cloud Storage进行操作,但是示例代码中的以下部分出现错误***“无法将c(类型“appengine”。Context)用作函数参数中的context.Context类型:*** “appengine”。Context未实现context.Context(缺少Deadline方法)”

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

这里有什么问题?我该如何解决?

英文:

Im trying the example code from this link and trying to do operations on
Google Cloud Storage using Google Cloud Storage Client App from Go runtime , But the following part in the sample code is giving the error "cannot use c (type "appengine".Context) as type context.Context in function argument: "appengine".Context does not implement context.Context (missing Deadline method)"

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

Whats the issue here ?? How can i solve this ??

答案1

得分: 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},
}
英文:

The error message clearly states that you try to pass a value of type appengine.Context where the expected type is context.Context.

The google.AppEngineTokenSource() function expects a value of type context.Context and not the one you pass (which is of type appengine.Context).

You can create such Context with the function:

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

This is how I would do it:

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},
}

答案2

得分: 2

你需要按照这里的说明,从appengine更新到google.golang.org/appengine:https://github.com/golang/oauth2/#app-engine

英文:

You need to update from appengine to google.golang.org/appengine as described there: https://github.com/golang/oauth2/#app-engine

huangapple
  • 本文由 发表于 2015年3月12日 21:10:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/29010718.html
匿名

发表评论

匿名网友

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

确定