【问题标题】:Python using Google App Engine. Upload file feature that stores files in the GAE datastore使用 Google App Engine 的 Python。将文件存储在 GAE 数据存储中的上传文件功能
【发布时间】:2016-05-18 13:03:36
【问题描述】:

我找到了用于在数据存储中存储文本的留言簿的代码。我整个早上都在寻找如何修改我的代码以上传文件而不是从文本字段中读取。并在显示后显示文件详细信息。我将不胜感激任何帮助?或者也许已经有答案了,我只是还没有找到。 到目前为止,这是我的代码:

import cgi
import datetime
import urllib
import wsgiref.handlers

from google.appengine.ext import db
from google.appengine.api import users
import webapp2


class Greeting(db.Model):
  author = db.UserProperty()
  content = db.StringProperty(multiline=True)
  date = db.DateTimeProperty(auto_now_add=True)


def upload_key(upload_name=None):
  return db.Key.from_path('Upload', upload_name or 'default_upload')


class MainPage(webapp2.RequestHandler):
  def get(self):
    self.response.out.write('<html><body>')
    upload_name=self.request.get('upload_name')

    greetings = db.GqlQuery("SELECT * "
                            "FROM Greeting "
                            "WHERE ANCESTOR IS :1 "
                            "ORDER BY date DESC LIMIT 10",
                            upload_key(upload_name))

    for greeting in greetings:
      if greeting.author:
        self.response.out.write(
            '<b>%s</b> wrote:' % greeting.author.nickname())
      else:
        self.response.out.write('An anonymous person wrote:')
      self.response.out.write('<blockquote>%s</blockquote>' %
                              cgi.escape(greeting.content))

    self.response.out.write("""
          <form action="/sign?%s" method="post">
            <div><textarea name="content" rows="3" cols="60"></textarea></div>
            <div><input type="submit" value="Upload a File"></div>
          </form>
          <hr>
          <form>Name: <input value="%s" name="upload_name">
          <input type="submit" value="switch user"></form>
        </body>
      </html>""" % (urllib.urlencode({'upload_name': upload_name}),
                          cgi.escape(upload_name)))


class Upload(webapp2.RequestHandler):
  def post(self):

    upload_name = self.request.get('upload_name')
    greeting = Greeting(parent=upload_key(upload_name))

    if users.get_current_user():
      greeting.author = users.get_current_user()

    greeting.content = self.request.get('content')
    greeting.put()
    self.redirect('/?' + urllib.urlencode({'upload_name': upload_name}))


APP = webapp2.WSGIApplication([
  ('/', MainPage),
  ('/sign', Upload)
], debug=True)


def main():
  APP.RUN()


if __name__ == '__main__':
  main()

【问题讨论】:

    标签: python google-app-engine


    【解决方案1】:

    有两种基本方法。传统方法以及您会找到最多示例的方法是 Blobstore API。新方法是谷歌云存储。 Blobstore 的优势在于现有样本较多,而 GCS 的优势在于相同的代码可以在 App Engine 的上下文之外运行。

    方法 1 - BLOBSTORE API - 更简单 *

    这里是官方Blobstore docs 的示例。

    这是similar Stack Overflow question.

    方法 - 谷歌云存储 API - 更好

    对于谷歌云存储,官方客户端库是gcloud-python。由于这不是 App Engine SDK 的一部分,因此您通常会在使用 pip -t 标志部署 App Engine 应用程序并修改 appengine_config.py 文件之前“供应”它(将其直接包含在您的项目中)。请参阅"Installing a library" 中的说明。这个故事的简短版本是你做的

    mkdir lib
    pip install gcloud-python -t lib
    

    然后添加 appengine_config.py 并使用以下行:

    from google.appengine.ext import vendor
    
    # Third-party libraries are stored in "lib", vendoring will make
    # sure that they are importable by the application.
    vendor.add('lib')
    

    最后,我们将在 Python 应用程序中使用此 API in this tutorial

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-24
      • 2017-05-04
      • 1970-01-01
      • 2020-08-21
      • 2021-07-05
      • 1970-01-01
      • 1970-01-01
      • 2011-05-05
      相关资源
      最近更新 更多