2012-12-16 97 views
0

我是新來GAE Blostore ....我試圖顯示通過GAE上傳的圖像的網址,但我有困難....任何幫助表示讚賞。創建Blobstore照片URL

1)下面的代碼顯示十六進制格式的密鑰,我不知道它爲什麼這樣做。

2)此外,如何使用十六進制值鍵獲取/創建圖像的URL?

from google.appengine.api import users 
from google.appengine.ext import webapp 
from google.appengine.ext.webapp.util import run_wsgi_app 
from google.appengine.ext import blobstore 
from google.appengine.ext.webapp import blobstore_handlers 
from google.appengine.ext import db 
from google.appengine.ext import blobstore 

#import os 
import urllib 
class UserPhoto(db.Model): 
    user = db.StringProperty() 
    user1 = db.EmailProperty() 
    blob_key = blobstore.BlobReferenceProperty()# blobstore.BlobKey # 




class MainPage(webapp.RequestHandler): 
    def get(self): 
     user = users.get_current_user() 
     upload_url = blobstore.create_upload_url('/upload') 
     existing_data = "<br>" 






    if user: 
     #user_photo = UserPhoto(user=users.get_current_user().email() 
     data = UserPhoto.all() 

     results = data.filter('user1 =',user.email()) 
     rmvStr = len("<__main__.UserPhoto object at ") 



     for blob in results: 


      existing_data += "Blob item key # : "+ str(blob)[rmvStr:len(str(blob))-1] +" <br>" 
      #existing_data += "Blob item key # : "+ str(blob) +" <br>" 


      #self.response.out.write("value of blob is: " + str(blob)) 

     self.response.out.write(
      'Hello %s <a href="%s">Sign out</a><br>Is administrator: %s' % 
      (user.nickname(), users.create_logout_url("/"), users.is_current_user_admin())   
      +'<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url+ 
      """Upload File: <input type="file" name="file"><br> <input type="submit" 
      name="submit" value="Submit"> </form> 
      <br>"""+existing_data 
     ) 
    else: 
     self.redirect(users.create_login_url(self.request.uri)) 


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] 
     user = users.get_current_user() 

     if user: 
      data = UserPhoto() 
      data.user1 = user.email() 
      data.blob_key = blob_info.key() 

      data.put() 




     #self.redirect('/serve/%s' % blob_info.key()) 
     self.redirect('/') 

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) 



application = webapp.WSGIApplication([('/', MainPage), 
          ('/upload', UploadHandler), 
          ('/serve/([^/]+)?', ServeHandler)], 
          debug=True) 
def main(): 
    run_wsgi_app(application) 


if __name__ == "__main__": 
    main() 

回答

0

我不喜歡在你的整個問題中拋出一個扳手,但你應該真的考慮Files API。它由Google雲端存儲提供支持,訪問界面非常快速且易於使用。我會推薦它通過blobstore,特別是如果你剛剛開始你的應用程序。

如果您正在處理用戶照片,您可以在任務隊列上關閉兩到三個尺寸,並將其折騰到雲存儲中,這可能比您運行Blobstore更便宜。

+0

除了它是實驗性的事實之外,文件API並不是您想要這樣做的方式。如果這是你真正想要的,create_upload_url可以將你的上傳文件寫入GCS。我不知道你爲什麼認爲GCS會更便宜。 –

+0

感謝您的建議。對於這個項目,我被要求使用Google App Engine Blobstore .... – user852415