2012-05-08 143 views
0

我正在編寫一個應用程序,允許用戶上傳視頻到我的網站,所以我可以編輯它們並將它們發回給他們。我正在使用谷歌應用程序引擎和Python。我已經閱讀了關於blob和東西,但我認爲仍然需要一點進一步的解釋。Google App Engine,上傳視頻,python

它是如何工作的?

我寫了一些使用谷歌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

您是否嘗試過google搜索 「App Engine的Blob存儲」或「Google Drive API」?你可能會發現一些有用的結果... –

+0

正如我所說,我確實使用谷歌應用程序引擎blobstore的代碼,我不會問以前做一些研究的問題。謝謝你的幫助tho –

+0

什麼是你的問題?不要只是給我們一層代碼,問一些模糊的東西,比如「有人能幫我解決這個問題嗎?」,我們不知道你想弄清楚什麼。 –

回答

1

您可以使用BlobReader類表現得像一個Python文件對象訪問Blob存儲文件數據。

例如:

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保存在谷歌雲端硬盤文件,你可以找到谷歌App Engine的一個示例應用程序here

+0

所以我應該如何在我的電腦上訪問它?抱歉,我從來沒有寫過一個應用程序來管理文件。 –

+0

blobstore文件託管在App Engine上,您可以使用您在問題中發佈的'ServeHandler'服務它們。 – proppy

+0

更新了我對有關Google雲端硬盤問題的回答。 – proppy