0

我是 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()
4

1 回答 1

0

我不想在这里对你的整个问题提出质疑,但你真的应该考虑 Files API。它由 Google Cloud Storage 提供支持,访问界面非常快速且易于使用。我会推荐它而不是 blobstore,特别是如果您刚刚开始使用您的应用程序。

如果你在做用户照片,你可以在一个任务队列上处理两个或三个尺寸,然后把它们扔到云存储中,可能比你运行 Blobstore 便宜。

于 2012-12-16T09:54:09.740 回答