私は GAE Blostore を初めて使用しています.... GAE 経由でアップロードされた画像の URL を表示しようとしていますが、問題が発生しています....どんな助けもありがとうございます。
1) 以下のコードはキーを 16 進形式で表示しますが、その理由はわかりません。
2) さらに、16 進値キーを使用して画像への 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()