创建Blobstore照片URL

时间:2012-12-16 07:25:47

标签: python google-app-engine blobstore

我是GAE Blostore的新手......我正在尝试显示通过GAE上传的图像的网址,但我遇到了困难....任何帮助都表示赞赏。

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()

1 个答案:

答案 0 :(得分:0)

我讨厌在这里提出整个问题,但你应该考虑使用Files API。它得到了Google云端存储的支持,访问界面非常快速且易于使用。我推荐它在blobstore上,特别是如果你刚刚开始使用你的应用程序。

如果您正在制作用户照片,您可以在任务队列中处理两到三种尺寸,然后将它们放入云存储中,这可能比运行Blobstore更便宜。