我无法从GAE BlobStore下载blob

时间:2013-05-16 14:11:41

标签: google-app-engine python-2.7 blobstore

这段代码有什么问题?我无法下载作为UploadHandler响应发送的下载URL后的blob。我从服务器收到404响应。 我对如何发送blob密钥的url安全版本有疑问。

import urllib
import webapp2
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers

MAIN = """<html>
<body>
   <form action="%s" method="POST" enctype="multipart/form-data">
       <p>Upload File:<input type="file" name="file"></p>
       <p><input type="submit" name="submit" value="Submit"> 
   </form>
</body>
</html>
"""

DOWNLOAD = """<html><body><p><a href="%s">%s</a></p></body></html>"""

class MainHandler(webapp2.RequestHandler):
    def get(self):
        upload_url = blobstore.create_upload_url('/upload')
        self.response.out.write(MAIN % upload_url)

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        upload_files = self.get_uploads('file')  # 'file' is name field in the form
        blob_info = upload_files[0]
        _key = blob_info.key()
        _url = '/download/%s' % str(_key)
        _url_text = blob_info.filename
        self.response.out.write(DOWNLOAD % (_url, _url_text))

class DownloadHandler(blobstore_handlers.BlobstoreDownloadHandler):
    def get(self, resource):
        blob_info = blobstore.Blob.get(resource)
        self.sendblob(blob_info)

app = webapp2.WSGIApplication([('/', MainHandler),
                               ('/upload', UploadHandler),
                               ('/download/<resource>', DownloadHandler)],
                              debug=True) 

app.yaml文件是     应用:georef     版本:1     runtime:python27     api_version:1     threadsafe:false

libraries:
- name: webapp2
  version: latest

handlers:
- url: /.*
  script: georef.app

1 个答案:

答案 0 :(得分:1)

您似乎从文档中复制并粘贴了错误的代码:

from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
    def get(self, resource):
        resource = str(urllib.unquote(resource))
        blob_info = blobstore.BlobInfo.get(resource)
        self.send_blob(blob_info)

如果资源字符串中包含任何奇怪的字符,您将错过编码资源字符串的行。