【问题标题】:I can not download a blob from GAE BlobStore我无法从 GAE BlobStore 下载 blob
【发布时间】:2013-05-11 11:37:25
【问题描述】:

这段代码有什么问题?我无法按照 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 运行时:python27 api_version: 1 线程安全:假

libraries:
- name: webapp2
  version: latest

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

【问题讨论】:

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


    【解决方案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)
    

    如果资源字符串中包含任何奇怪的字符,则缺少对资源字符串进行解码的行。

    【讨论】:

    • 确实,我编写了阅读文档 [developers.google.com/appengine/docs/python/tools/webapp/… 的代码,并且我省略了带有 urllib 调用的行。但是,我在您的回答中注意到我拼错了 send_blob(我在我的代码中写了 sendblob)。所以,DownloadHandler 没有被执行。除了 urllib 的 quote 和 unquote 调用,还有一个我不知道的麻烦。
    • 您最好添加一些日志语句来调试并确保传递正确的值(如resource)。您还想在管理员中使用 blobstore 查看器来确保您的 blob 已实际上传。
    猜你喜欢
    • 2012-10-17
    • 2012-06-18
    • 2023-04-11
    • 2015-10-14
    • 2012-09-12
    • 2019-09-05
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    相关资源
    最近更新 更多