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