Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python使用googleappengine。在GAE数据存储中存储文件的上载文件功能_Python_Google App Engine - Fatal编程技术网

Python使用googleappengine。在GAE数据存储中存储文件的上载文件功能

Python使用googleappengine。在GAE数据存储中存储文件的上载文件功能,python,google-app-engine,Python,Google App Engine,我找到了在数据存储中存储文本的留言簿代码。我整个上午都在寻找如何修改代码以上传文件,而不是从文本字段读取。并在显示后显示文件详细信息。我会很感激你的帮助吗?或者可能已经有答案了,我只是还没找到。 以下是我目前的代码: import cgi import datetime import urllib import wsgiref.handlers from google.appengine.ext import db from google.appengine.api import users

我找到了在数据存储中存储文本的留言簿代码。我整个上午都在寻找如何修改代码以上传文件,而不是从文本字段读取。并在显示后显示文件详细信息。我会很感激你的帮助吗?或者可能已经有答案了,我只是还没找到。 以下是我目前的代码:

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()
导入cgi
导入日期时间
导入URL库
导入wsgiref.handlers
从google.appengine.ext导入数据库
从google.appengine.api导入用户
导入webapp2
班级问候语(db.Model):
author=db.UserProperty()
content=db.StringProperty(multiline=True)
date=db.DateTimeProperty(自动\u现在\u添加=True)
def上传密钥(上传名称=无):
返回db.Key.from_path('Upload',Upload_name或'default_Upload')
类主页(webapp2.RequestHandler):
def get(自我):
self.response.out.write(“”)
upload\u name=self.request.get('upload\u name')
问候语=db.GqlQuery(“选择*”
“来自问候语”
“祖先所在地:1”
“按日期说明订单限额10”,
上传密钥(上传名称))
问候语中的问候语:
如果是greeting.author:
self.response.out.write(
“%s编写:”%greeting.author.昵称()
其他:
self.response.out.write('匿名者写:')
self.response.out.write('%s'%
cgi.escape(问候语.内容))
self.response.out.write(“”)

姓名: “%”(urllib.urlencode({'upload\u name':upload\u name}), cgi.escape(上传名称))) 类上载(webapp2.RequestHandler): def post(自我): upload\u name=self.request.get('upload\u name') 问候语=问候语(父项=上传密钥(上传名称)) if users.get_current_user(): greeting.author=users.get_current_user() greeting.content=self.request.get('content') 问候语 self.redirect(“/?”+urllib.urlencode({'upload\u name':upload\u name})) APP=webapp2.WSGIApplication([ (“/”,主页), ('/sign',上传) ],debug=True) def main(): APP.RUN() 如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu': main()
有两种基本方法。传统的方法是BlobstoreAPI,您会发现这种方法的示例最多。新的方法是谷歌云存储。Blobstore的优点是有更多的现有示例,但GCS的优点是相同的代码可以在AppEngine的上下文之外工作

方法1-BLOBSTORE API-更简单*

这是官方提供的样品

这里有一个

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

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

mkdir lib
pip install gcloud-python -t lib
然后添加带有以下行的
appengine\u 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