从GAE将Json Array上传到GO中的云存储

时间:2015-05-08 12:58:37

标签: javascript json google-app-engine go google-cloud-storage

我尝试使用以下代码将应用引擎应用程序发布的json数组上传到Google云端存储:

saveData : function saveData() {
  var _this = this,
      save = this.shadowRoot.querySelector('#save-data'),
      subData = JSON.stringify(_this.app.userSession);

  save.url="url";
  save.body = subData;
  save.go();
}

发布的消息将使用下面发布的代码处理。使用此代码,我可以在云存储桶上创建一个用用户ID命名的文件夹。我想要做的是将整个json数组-i.e复制到文件夹中。下面代码中的变量f。我尝试使用io.Copy(wc,f),但它给了我以下错误:

  

不能在参数中使用内容(类型userData)作为类型io.Reader   io.Copy:           userData没有实现io.Reader(缺少Read方法)

显然我做错了什么,但我很新,我完全陷入了困境。有人能帮助我吗?

package expt

import (
    "bytes"
    "encoding/json"
    "io/ioutil"
    "log"
    "net/http"
    "golang.org/x/net/context"
    "golang.org/x/oauth2"
    "golang.org/x/oauth2/google"
    "google.golang.org/appengine"
    "google.golang.org/appengine/file"
    "google.golang.org/appengine/urlfetch"
    "google.golang.org/cloud"
    "google.golang.org/cloud/storage"
)

var bucket = "expt"

func init() {
    http.HandleFunc("/", handleStatic)
    http.HandleFunc("/save", saveJson)
}

func handleStatic(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Cache-Control", "no-cache")
    http.ServeFile(w, r, "static/"+r.URL.Path)
}

type Result map[string]interface {
}

type userData struct {
    id     string
    age    string
    gender string
}

func testA(r *http.Request) userData {
    defer r.Body.Close()
    body, err := ioutil.ReadAll(r.Body)
    var userDataCurr userData
    if err != nil {
        log.Printf("Couldn't read request body: %s", err)
    } else {
        var f Result
        err := json.Unmarshal(body, &f)
        if err != nil {
            log.Println("Error: %s", err)
        } else {
            user := f["user"].(map[string]interface{})
            userDataCurr.id = user["id"].(string)
        }
    }
    return userDataCurr
}

// saveData struct holds information needed to run the various saving functions.
type saveData struct {
    c   context.Context
    r   *http.Request
    w   http.ResponseWriter
    ctx context.Context
    // cleanUp is a list of filenames that need cleaning up at the end of the saving.
    cleanUp []string
    // failed indicates that one or more of the saving steps failed.
    failed bool
}

func (d *saveData) errorf(format string, args ...interface{}) {
    d.failed = true
    // log.Errorf(d.c, format, args...)
}

// testSave is the main saving entry point that calls the GCS operations.
func saveJson(w http.ResponseWriter, r *http.Request) {

    c := appengine.NewContext(r)
    if bucket == "" {
        var err error
        if bucket, err = file.DefaultBucketName(c); err != nil {
            // log.Errorf(c, "failed to get default GCS bucket name: %v", err)
            return
        }
    }
    hc := &http.Client{
        Transport: &oauth2.Transport{
            Source: google.AppEngineTokenSource(c, storage.ScopeFullControl),
            Base:   &urlfetch.Transport{Context: c},
        },
    }
    ctx := cloud.NewContext(appengine.AppID(c), hc)

    d := &saveData{
        c:   c,
        r:   r,
        w:   w,
        ctx: ctx,
    }

    d.createUserFolder()

}

// createFile creates a file in Google Cloud Storage.
func (d *saveData) createFile(fileName string) {

    wc := storage.NewWriter(d.ctx, bucket, fileName)
    wc.ContentType = "text/plain"
    d.cleanUp = append(d.cleanUp, fileName)

    if err := wc.Close(); err != nil {
        d.errorf("createFile: unable to close bucket %q, file %q: %v", bucket, fileName, err)
        return
    }
}

//create files that will be used by listBucket.
func (d *saveData) createUserFolder() {
    var (
        content = testA(d.r)
        buffer  bytes.Buffer
    )

    buffer.WriteString(content.id)
    buffer.WriteString("/")
    d.createFile(buffer.String())
}

1 个答案:

答案 0 :(得分:0)

这段代码非常混乱,但据我所知,你试图这样做:

func (d *saveData) createFile(fileName string, content userData) {
    wc := storage.NewWriter(d.ctx, bucket, fileName)
    wc.ContentType = "text/plain"
    d.cleanUp = append(d.cleanUp, fileName)

    //*** new code *******
    io.Copy(wc, content)
    //********************

    if err := wc.Close(); err != nil {
        d.errorf("createFile: unable to close bucket %q, file %q: %v", bucket, fileName, err)
        return
    }
}

您无法将对象直接写入文件,需要先对其进行编码。我假设你想要json。你可以这样做:

bs, err := json.Marshal(content)
if err != nil {
  return err
}
io.Copy(wc, bytes.NewReader(bs))

但最好使用json.NewEncoder

json.NewEncoder(wc).Encode(content)

您的createUserFolder也可以简化(您不需要缓冲区来连接字符串):

func (d *saveData) createUserFolder() {
    content := testA(d.r)
    d.createFile(content.id + "/")
}

我不知道testA应该是什么意思,但它也可以简化:

type UserData {
  ID     string `json:"id"`
  Age    string `json:"age"`
  Gender string `json:"gender"`
}

func testA(r *http.Request) UserData {
  defer r.Body.Close()
  var obj struct {
    User UserData `json:"user"`
  }
  err := json.NewDecoder(r.Body).Decode(&obj)
  if err != nil {
      log.Println("Error: %s", err)
  }
  return obj.User
}

使用大写字段名称,以便json可以为您处理转换。

可能需要进行更多的重构,但这可能足以让你开始。