3

我已经能够使用此处提供的代码(在主要基于 GAE 文档的评论之一中)成功地将图像上传到我的 Google App Engine Blobstore 。

以下是完整代码供参考:

import os
import urllib

from google.appengine.ext import blobstore
from google.appengine.ext import webapp
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

class MainHandler(webapp.RequestHandler):
    def get(self):
        upload_url = blobstore.create_upload_url('/upload')
        self.response.out.write('<html><body>')
        self.response.out.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url)
        self.response.out.write("""Upload File: <input type="file" name="file"><br> <input type="submit" name="submit" value="Submit"> </form></body></html>""")

        for b in blobstore.BlobInfo.all():
            self.response.out.write('<li><a href="/serve/%s' % str(b.key()) + '">' + str(b.filename) + '</a>')

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        upload_files = self.get_uploads('file')
        blob_info = upload_files[0]
        self.redirect('/')

class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
    def get(self, blob_key):
        blob_key = str(urllib.unquote(blob_key))
        if not blobstore.get(blob_key):
            self.error(404)
        else:
            self.send_blob(blobstore.BlobInfo.get(blob_key), save_as=True)

def main():
    application = webapp.WSGIApplication(
          [('/', MainHandler),
           ('/upload', UploadHandler),
           ('/serve/([^/]+)?', ServeHandler),
          ], debug=True)
    run_wsgi_app(application)

if __name__ == '__main__':
  main()

但是,此代码要求在包含图像数据的GET请求之前在请求中创建上传 url :POST

def get(self):
  upload_url = blobstore.create_upload_url('/upload')

当我尝试从移动设备发送图像时,我想将服务器代码推送到该def post(self):功能下的单个代码块中,但我在执行此操作时遇到了麻烦。

将上面的行移到def post(self):代码中似乎并不能解决问题。

有任何想法吗?

干杯! 布雷特

4

2 回答 2

2

是的,您可以使用 urlfetch 将其设为一个代码块。这是我的方法:

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        upload_files = self.get_uploads('file')
        if len(upload_files) > 0 :
            blob_info = upload_files[0]
            self.response.write(str(blob_info.key()))
        else:
            self.error(404) 

class SomeHandler(webapp.RequestHandler):
    def post(self):
        file = self.request.POST.get('file')
        if (file is not None):
            # Use urlfetch to call to the blob upload url and get result
            # Just copy the same request body and header and pass to UploadHandler here
            result = urlfetch.fetch(
                url= blobstore.create_upload_url('/upload'),
                payload=self.request.body,
                method=urlfetch.POST,
                headers=self.request.headers)
            if result.status_code == 200:
                blob_key_str = result.content
                # Get blob key
                blob_key = blobstore.BlobKey(blob_key_str)
                # Maybe a url for the file
                blob_url = images.get_serving_url(blob_key_str, 400)

这样,您始终可以将文件上传到 SomeHandler,而不必先获取上传 URL,然后再仅上传。

希望能帮助到你!

于 2014-03-30T17:15:59.133 回答
0

这不适用于默认的 Blobstore 上传处理程序:它需要两个请求:第一个创建一次性下载 url,第二个实际执行 POST 到此 url。

如果您想一次性完成所有操作,请创建自己的文件上传处理程序并使用新的 Blobstore API 来写入文件

于 2012-08-07T17:52:34.847 回答