如何使用Python获取Blob密钥?

时间:2012-12-18 08:07:38

标签: python google-app-engine

我在存储然后检索blobstore密钥时遇到了困难。看起来我能够获取数据库行键#而不是blob键#....

1)我不确定如何存储密钥 2)另外,我不确定当前数据如何存储到UserPhoto数据结构中 建立 3)如何检索与当前用户相关的Blob密钥,以便显示指向i的链接?

任何帮助将不胜感激。

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
import urllib


class UserPhoto(db.Model):
    user = db.StringProperty()
    user1 = db.EmailProperty()        
    blob_prop = blobstore.BlobKey
    blob_key = blobstore.BlobReferenceProperty()


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


    if user:


        data = UserPhoto.all()


        #filter data by user e-mail
        results = data.filter("user1 = ", user.email())

        for dt in results:

            existing_data +='<a href="/serve/%s' % dt.key() + '">'
            existing_data += '%s' % dt.key() + '<br>'   

        self.response.out.write(
            '<div align=center>Hello %s <a align="center" 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+
            """<br>Upload File: <input type="file" name="file"><br> <input type="submit"
            name="submit" value="Submit"> </form>
            <br>"""+existing_data + "</div>"

        )

       #the code below is used for testing...purposes only...
        for b in blobstore.BlobInfo.all():
            self.response.out.write('<li><a href="/serve/%s' % str(b.key()) + '">' + str(b.filename) + '</a>')

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

            data.blob_key = str(blob_info.key())
           # data.user2[0] = self.get_uploads()

           data.blob_prop = blob_info.key()

            data.put()



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


class ViewPhotoHandler(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, photo_key):
    blob_key = str(urllib.unquote(photo_key))
    if not blobstore.get(photo_key):
        self.error(404)
    else:
        #self.send_blob(photo_key)
        #self.send_blob('/serve/%s' % photo_key)
        self.send_blob(blobstore.BlobInfo.get(blob_key), save_as=True)


 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),
                           ('/view_photo/([^/]+)?', ViewPhotoHandler)
                           ],debug=True)
def main():
  run_wsgi_app(application)


if __name__ == "__main__":
  main()

1 个答案:

答案 0 :(得分:0)

在构造输出时,您要求UserPhoto实体键,而不是该实体中的blobstore引用。它应该是:

existing_data +='<a href="/serve/%s">' % dt.blob_key