在GAE Golang Blobstore中存储任意数据是否可行?

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

Is it possible to store arbitrary data in GAE Golang Blobstore?

问题

我正在使用Google App Engine Go创建一个大型数据库应用程序。我的大部分数据都很小,所以在Datastore中存储它们没有问题。然而,我知道我会遇到一些几兆字节大小的条目,所以我需要使用Blobstore来保存它们。

在查看Blobstore的参考文档时,我发现该服务主要用于上传到服务的文件。我需要调用哪些函数来像在Datastore中那样存储任意数据在Blobstore中?我已经可以将数据转换为[]byte,并且我不需要在blob中索引任何内容,只需要通过ID存储和获取它。

英文:

I am creating a large database application in Google App Engine Go. Most of my pieces of data are small, so I have no problem storing them in Datastore. However, I know I will run into a few entries that will be a few megabytes big, so I will have to use Blobstore to save them.

Looking at the reference for Blobstore, it appears that the service was mainly intended to be used for files being uploaded to the service. What are the functions I need to call to store arbitrary data in the Blobstore like I would in Datastore? I can already convert the data to []byte and I don't need to index anything in the blob, just to store and fetch it by ID.

答案1

得分: 3

有两种方法可以将文件写入Blobstore。

一种方法是使用在Blobstore页面末尾记录的已弃用的API。他们的示例代码如下:

他们将要转向的方法是将文件存储在Google云存储中,并通过Blobstore提供文件。

另一种方法是以某种方式模拟用户上传。Go语言有一个可以将文件发送到Web地址进行上传的HTTP客户端。不过,这种方法可能有些繁琐。

var k appengine.BlobKey
w, err := blobstore.Create(c, "application/octet-stream")
if err != nil {
        return k, err
}
_, err = w.Write([]byte("... some data ..."))
if err != nil {
        return k, err
}
err = w.Close()
if err != nil {
        return k, err
}
return w.Key()
英文:

There are two ways that you could write files to the blobstore

One is to use a deprecated API documented at the end of the page for the blobstore. Their example code is below.

The approach that they are going to be switching to is storing files in Google cloud storage and serving them via the blobstore.

The other approach would be to simulate a user upload in some fashion. Go has an http client that can send files to be uploaded to web addresses. That would be a hacky way to do it though.

var k appengine.BlobKey
w, err := blobstore.Create(c, "application/octet-stream")
if err != nil {
        return k, err
}
_, err = w.Write([]byte("... some data ..."))
if err != nil {
        return k, err
}
err = w.Close()
if err != nil {
        return k, err
}
return w.Key()

答案2

得分: 2

如@yumaikas所说,Files API已被弃用。如果这些数据来自某种用户上传,您应该修改上传表单以使用Blobstore上传URL(特别是将编码设置为multipart/form-datamultipart/mixed,并将所有文件上传字段命名为file,除非您不希望存储在Blobstore中的字段)。

然而,如果这不可行(例如,您无法控制用户输入,或者您必须在将数据存储在Blobstore之前在服务器上预处理数据),那么您要么必须使用已弃用的Files API,要么使用URLFetch API上传数据。

这是一个完整的示例应用程序,它将为您存储一个示例文件到Blobstore中。

package sample

import (
	"bytes"
	"net/http"
	"mime/multipart"

	"appengine"
	"appengine/blobstore"
	"appengine/urlfetch"
)

const SampleData = `foo,bar,spam,eggs`

func init() {
	http.HandleFunc("/test", StoreSomeData)
	http.HandleFunc("/upload", Upload)
}

func StoreSomeData(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	// 首先需要创建上传URL:
	u, err := blobstore.UploadURL(c, "/upload", nil)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		c.Errorf("%s", err)
		return
	}

	// 现在可以准备一个将提交到该URL的表单。
	var b bytes.Buffer
	fw := multipart.NewWriter(&b)
	// 不要更改表单字段,它必须是“file”!
	// 但是您可以自由更改文件名,它将存储在BlobInfo中。
	file, err := fw.CreateFormFile("file", "example.csv")
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		c.Errorf("%s", err)
		return
	}
	if _, err = file.Write([]byte(SampleData)); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		c.Errorf("%s", err)
		return
	}
	// 不要忘记关闭multipart writer。
	// 如果不关闭,请求将缺少终止边界。
	fw.Close()

	// 现在您有了一个表单,可以将其提交给处理程序。
	req, err := http.NewRequest("POST", u.String(), &b)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		c.Errorf("%s", err)
		return
	}
	// 不要忘记设置内容类型,其中包含边界。
	req.Header.Set("Content-Type", fw.FormDataContentType())

	// 现在提交请求。
	client := urlfetch.Client(c)
	res, err := client.Do(req)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		c.Errorf("%s", err)
		return
	}

	// 检查响应状态,它应该与`/upload`处理程序中返回的状态相同。
	if res.StatusCode != http.StatusCreated {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		c.Errorf("bad status: %s", res.Status)
		return
	}
	// 一切顺利。
	w.WriteHeader(res.StatusCode)
}

func Upload(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	// 在这里,我们只是检查上传是否按预期进行。
	if _, _, err := blobstore.ParseUpload(r); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		c.Errorf("%s", err)
		return
	}
	// 一切看起来都很好。使用状态码通知其他处理程序。
	w.WriteHeader(http.StatusCreated)
}

现在,如果您运行curl http://localhost:8080/test,它将在Blobstore中存储一个文件。

重要提示:我不确定您如何为向自己的应用程序发出的请求的带宽付费。在最坏的情况下,您将为内部流量付费,这比正常带宽更便宜。

英文:

As @yumaikas said, the Files API is deprecated. If this data comes from some sort of a user upload, you should modify the upload form to work with Blobstore Upload URLs (in particular, setting the encoding to multipart/form-data or multipart/mixed and naming all file upload fields file, except the ones that you don't want to be stored in blobstore).

However, if that is not possible (e.g. you don't have control over the user input, or you have to pre-process the data on the server before you store it in Blobstore), then you'll either have to use the deprecated Files API, or upload the data using the URLFetch API.

Here's a complete example application that will store a sample file for you in Blobstore.

package sample

import (
	"bytes"
	"net/http"
	"mime/multipart"

	"appengine"
	"appengine/blobstore"
	"appengine/urlfetch"
)

const SampleData = `foo,bar,spam,eggs`

func init() {
	http.HandleFunc("/test", StoreSomeData)
	http.HandleFunc("/upload", Upload)
}

func StoreSomeData(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	// First you need to create the upload URL:
	u, err := blobstore.UploadURL(c, "/upload", nil)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		c.Errorf("%s", err)
		return
	}

	// Now you can prepare a form that you will submit to that URL.
	var b bytes.Buffer
	fw := multipart.NewWriter(&b)
	// Do not change the form field, it must be "file"!
	// You are free to change the filename though, it will be stored in the BlobInfo.
	file, err := fw.CreateFormFile("file", "example.csv")
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		c.Errorf("%s", err)
		return
	}
	if _, err = file.Write([]byte(SampleData)); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		c.Errorf("%s", err)
		return
	}
	// Don't forget to close the multipart writer.
	// If you don't close it, your request will be missing the terminating boundary.
	fw.Close()

	// Now that you have a form, you can submit it to your handler.
	req, err := http.NewRequest("POST", u.String(), &b)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		c.Errorf("%s", err)
		return
	}
	// Don't forget to set the content type, this will contain the boundary.
	req.Header.Set("Content-Type", fw.FormDataContentType())

	// Now submit the request.
	client := urlfetch.Client(c)
	res, err := client.Do(req)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		c.Errorf("%s", err)
		return
	}

	// Check the response status, it should be whatever you return in the `/upload` handler.
	if res.StatusCode != http.StatusCreated {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		c.Errorf("bad status: %s", res.Status)
		return
	}
	// Everything went fine.
	w.WriteHeader(res.StatusCode)
}

func Upload(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	// Here we just checked that the upload went through as expected.
	if _, _, err := blobstore.ParseUpload(r); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		c.Errorf("%s", err)
		return
	}
	// Everything seems fine. Signal the other handler using the status code.
	w.WriteHeader(http.StatusCreated)
}

Now if you curl http://localhost:8080/test, it will store a file in the Blobstore.

Important: I'm not exactly sure how you would be charged for bandwidth for the request that you make to your own app. At the worst case, you will be charged for internal traffic, which is cheaper from normal bandwidth iirc.

huangapple
  • 本文由 发表于 2013年11月22日 19:40:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/20144066.html
匿名

发表评论

匿名网友

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

确定