我正在编写一个应用程序,允许用户将视频上传到我的网站,因此我可以编辑它们并发回给他们。我正在使用谷歌应用程序引擎和python。我已经阅读了关于blob和东西的内容,但我认为仍需要进一步解释。
它究竟是如何运作的?
我使用google blob示例代码编写了一些代码,因此看起来文件正在上传。但是,我不知道如何访问它们。我甚至不知道他们在哪里得救。
这是我的表格
<form method="POST" action="%s" enctype="multipart/form-data">
Upload file: <input type="file" name="file" /><br />
</br>
<input type="submit" name="Submit" value="Submit"/>
</form>
这是我的处理程序:
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from google.appengine.dist import use_library
use_library('django', '1.2')
import os
import logging
import cgi
import datetime
import urllib
import wsgiref.handlers
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from util.sessions import Session
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
# A Model for a User
class User(db.Model):
account = db.StringProperty()
password = db.StringProperty()
name = db.StringProperty()
# A helper to do the rendering and to add the necessary
# variables for the _base.htm template
def doRender(handler, tname = 'index.html', values = { }):
logging.info('doRender(' + tname + ')')
temp = os.path.join(
os.path.dirname(__file__),
'templates/' + tname)
logging.info(temp)
if not os.path.isfile(temp):
return False
# Make a copy of the dictionary and add the path and session
newval = dict(values)
newval['path'] = handler.request.path
handler.session = Session()
if 'username' in handler.session:
newval['username'] = handler.session['username']
logging.info(newval)
outstr = template.render(temp, newval)
handler.response.out.write(outstr)
return True
class LoginHandler(webapp.RequestHandler):
def get(self):
doRender(self, 'login.html')
def post(self):
self.session = Session()
acct = self.request.get('account')
pw = self.request.get('password')
logging.info('Checking account='+acct+' pw='+pw)
self.session.delete_item('username')
self.session.delete_item('userkey')
if pw == '' or acct == '':
doRender(
self,
'login.html',
{'error' : 'Please specify Account and Password'} )
return
que = db.Query(User)
que = que.filter('account =',acct)
que = que.filter('password = ',pw)
results = que.fetch(limit=1)
if len(results) > 0 :
user = results[0]
self.session['userkey'] = user.key()
self.session['username'] = acct
doRender(self,'index.html',{ } )
else:
doRender(
self,
'login.html',
{'error' : 'Incorrect password'} )
class AddUserHandler(webapp.RequestHandler):
def get(self):
doRender(self, 'adduser.html')
def post(self):
self.session = Session()
name = self.request.get('name')
acct = self.request.get('account')
pw = self.request.get('password')
logging.info('Adding account='+acct)
if pw == '' or acct == '' or name == '':
doRender(
self,
'adduser.html',
{'error' : 'Please fill in all fields'} )
return
# Check if the user already exists
que = db.Query(User).filter('account =',acct)
results = que.fetch(limit=1)
if len(results) > 0 :
doRender(
self,
'adduser.html',
{'error' : 'Account Already Exists'} )
return
# Create the User object and log the user in
newuser = User(name=name, account=acct, password=pw);
pkey = newuser.put();
self.session['username'] = acct
self.session['userkey'] = pkey
doRender(self,'index.html',{ })
class LogoutHandler(webapp.RequestHandler):
def get(self):
self.session = Session()
self.session.delete_item('username')
self.session.delete_item('userkey')
doRender(self, 'index.html')
class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('file') # 'file' is file upload field in the form
blob_info = upload_files[0]
self.redirect('/serve/%s' % blob_info.key())
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)
class MainHandler(webapp.RequestHandler):
def get(self):
if doRender(self,self.request.path) :
return
doRender(self,'index.html')
def main():
application = webapp.WSGIApplication([
('/login', LoginHandler),
('/add-user', AddUserHandler),
('/logout', LogoutHandler),
('/upload', UploadHandler),
('/serve/([^/]+)?',ServeHandler),
('/.*', MainHandler)],
debug=True)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == '__main__':
main()
有人可以帮我解决这个问题吗?此外,有没有选项可以直接在我的谷歌驱动器或保管箱上保存这些文件?
感谢您的帮助
答案 0 :(得分:1)
您可以使用行为类似于Python文件对象的BlobReader类来访问blobstore文件数据。
例如:
upload_files = self.get_uploads('file')
blob_info = upload_files[0]
f = blob_info.open() # Return a BlobReader
content = f.read() # Read the entire file content into memory
您可以使用Drive SDK在Google云端硬盘上保存文件,您可以找到适用于Google App Engine的示例应用here