Keep getting "not an App Engine context" though I'm using appengine context

1,346 views
Skip to first unread message

Erik Lyngved

unread,
Aug 19, 2018, 6:28:57 PM8/19/18
to google-appengine-go
Hi, I'm not sure what I'm doing wrong here. I'm using appengine.NewContext(r) to make a new context, yet when I try to call blobstore.BlobKeyForFile with the context it panics with "not an App Engine context". It doesn't work with dev_appserver.py nor when deploying as an app engine service. Does someone know what I can try out?

I should note that I'm a golang noob; I just installed it via homebrew today, so not sure if there is something I have to do in my environment.



package main


import (
 
"fmt"
 
"log"
 
"net/http"
 
"encoding/json"


 
"google.golang.org/appengine"
 
"google.golang.org/appengine/blobstore"
)


type
Image struct {
 
Bucket string
 
Path string
}


func main
() {
 http
.HandleFunc("/serveurl", handle)
 log
.Print("Listening on port 8080")
 log
.Fatal(http.ListenAndServe(":8080", nil))
}


func handle
(w http.ResponseWriter, r *http.Request) {
 ctx
:= appengine.NewContext(r)
  image
:= Image{}
  err
:= json.NewDecoder(r.Body).Decode(&image)
 
if err != nil{
    panic
(err)
 
}


  fullPath
:= fmt.Sprintf("/gs/%s", image.Bucket, image.Path)
  fmt
.Fprint(w, fullPath)


  key
, err := blobstore.BlobKeyForFile(ctx, fullPath)
 
if err != nil {
    panic
(err)
 
}
  fmt
.Fprint(w, key)
}



Steven Buss

unread,
Sep 12, 2018, 5:34:58 PM9/12/18
to elyn...@gmail.com, google-appengine-go
Currently, on App Engine Standard, you need to call `appengine.Main()` instead of `http.ListenAndServe`:

func main() {
http.HandleFunc("/serveurl", handle)
appengine.Main()
}

Try that and give it another shot

--
You received this message because you are subscribed to the Google Groups "google-appengine-go" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-appengin...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-appengine-go/47d226ae-24ac-48b1-8611-bd233088f3ba%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages