阅读背景:

GAE BlobstoreUploadHandler无法识别文件

来源:互联网 

When I send a request from a client like this:

当我从这样的客户发送请求时:

with open(config.datafile, "rb") as f:
  r = requests.post(config.endpoint, files={"file": f})
  print r.text

to my GAE app like this:

我的GAE应用程序是这样的:

class FileUploadHandler(blobstore_handlers.BlobstoreUploadHandler):

  def post(self):
    self.response.out.write(self.get_uploads())

I would expect to get something like this:

我希望得到这样的东西:

{
  "file": "line 1\nline 2\nline 3\nline 4\nline 5\nline 6\nline 7\nline 8\nline 9\n"
},

but instead the request is putting the file data in data so that if I change the app to look like this:

但是请求是将文件数据放入数据中,这样如果我将应用程序更改为如下所示:

class FileUploadHandler(blobstore_handlers.BlobstoreUploadHandler):

  def post(self):
    self.response.out.write(self.request.get("file"))

I now get a response body like:

我现在得到一个响应体,如:

line 1
line 2
line 3
...

This seems contrary to the documentation. Can someone explain why please?

这似乎与文档相反。有人可以解释为什么好吗?

1 个解决方案

#1


I suspect you are sending the file to the URL mapped to FileUploadHandler. This is not the right thing to do.

我怀疑您是将文件发送到映射到FileUploadHandler的URL。这不是正确的做法。

You need to upload the file to the url returned from blobstore.create_upload_url.

您需要将文件上传到blobstore.create_upload_url返回的url。

Even then you cannot access the file through the request. You have to use a BlobReader to access it.

即使这样,您也无法通过请求访问该文件。您必须使用BlobReader来访问它。

Here are the relevant docs:

以下是相关文档:

Upload to blobstore: https://cloud.google.com/appengine/docs/python/blobstore/#Python_Uploading_a_blob

上传到blobstore:https://cloud.google.com/appengine/docs/python/blobstore/#Python_Uploading_a_blob

Read from blobstore: https://cloud.google.com/appengine/docs/python/blobstore/blobreaderclass

从blobstore阅读:https://cloud.google.com/appengine/docs/python/blobstore/blobreaderclass


分享到: