Created
November 27, 2017 02:32
-
-
Save sinmetal/e2598a4df0d378e18a43fe3d66c67210 to your computer and use it in GitHub Desktop.
Google Cloud Datastore Export Handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package datastorebackup | |
import ( | |
"fmt" | |
"net/http" | |
"time" | |
"google.golang.org/appengine" | |
"google.golang.org/appengine/log" | |
"google.golang.org/appengine/user" | |
datastore "google.golang.org/api/datastore/v1beta1" | |
"golang.org/x/net/context" | |
"golang.org/x/oauth2/google" | |
) | |
func Export(c context.Context, w http.ResponseWriter, r *http.Request) { | |
for k, v := range r.Header { | |
log.Infof(c, "%s = %s", k, v) | |
} | |
if r.Header.Get("X-Appengine-Cron") != "true" { | |
if user.IsAdmin(c) == false { | |
l, err := user.LoginURL(c, r.URL.Path) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
http.Redirect(w, r, l, http.StatusFound) | |
return | |
} | |
} | |
ctxWithDeadline, cancel := context.WithTimeout(c, 9*time.Minute) | |
defer cancel() | |
client, err := google.DefaultClient(ctxWithDeadline, datastore.DatastoreScope) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
service, err := datastore.New(client) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
p := appengine.AppID(c) | |
op, err := service.Projects.Export(p, &datastore.GoogleDatastoreAdminV1beta1ExportEntitiesRequest{ | |
EntityFilter: &datastore.GoogleDatastoreAdminV1beta1EntityFilter{ | |
NamespaceIds: []string{}, | |
Kinds: []string{"hoge","fuga"}, | |
}, | |
OutputUrlPrefix: fmt.Sprintf("gs://%s-datastore-backup", p), | |
}).Do() | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
w.WriteHeader(op.HTTPStatusCode) | |
b, err := op.Response.MarshalJSON() | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
w.Write(b) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is really useful. Thank you.