1
此代碼有什麼問題?作爲UploadHandler響應發送的下載url後,我無法下載blob。我得到了服務器的404響應。 我懷疑如何發送一個URL安全版本的BLOB密鑰。我無法從GAE BlobStore下載blob
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
確實,我編寫了閱讀文檔的代碼[https://developers.google.com/appengine/docs/python/tools/webapp/blobstorehandlers#BlobstoreUploadHandler](docs),並且我忽略了行urllib調用。但是,我注意到在你的答案中,我拼錯send_blob(我在我的代碼中寫了sendblob)。所以,DownloadHandler不會被執行。除了urllib的引用和不引用的調用,還有另一個我不知道的麻煩。 –
你最好添加一些日誌語句來調試,並確保正確的值(如'resource')被傳遞。您還想在管理員中使用blobstore查看器,以確保您的blob實際上傳。 – dragonx