doushan1863 2015-05-08 12:58
浏览 36
已采纳

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

I trying to upload to google cloud storage a json array which is posted by an app engine application using the following code:

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();
}

The posted message is handled in go with the code posted below. With this code I'm able to create a folder on the cloud storage bucket which is named with the user ID. What I would love to do is to copy into the folder the entire json array -i.e. the variable f in the code below. I tried with io.Copy(wc, f) but it gives me the following error:

cannot use content (type userData) as type io.Reader in argument to io.Copy: userData does not implement io.Reader (missing Read method)

Obviously I'm doing something wrong, but I'm quite new to go and I'm totally stuck. Can someone help me?

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条回答 默认 最新

  • doushenyu8228 2015-05-09 05:16
    关注

    This code is very confusing but from what I can tell are you trying to do this:

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

    You can't write an object directly to a file, it needs to be encoded first. I'm assuming you want json. You could do it this way:

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

    But it would be better to use json.NewEncoder:

    json.NewEncoder(wc).Encode(content)
    

    Your createUserFolder can also be simplified (you don't need a buffer to concatenate strings):

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

    I don't know what testA is supposed to mean, but it too can be simplified:

    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
    }
    

    Use uppercase field names so that json can handle the conversion for you.

    There's probably a lot more refactoring to be done, but maybe that's enough to get started.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料