Google App Engine / Python 㨠Google Cloud Storage ã使ã£ã¦ï¼HTTP/GETã§ãªã¯ã¨ã¹ããããã£ãã¨ãã«ï¼ç¹å®ã®ã¦ã¼ã¶ã¼ã ãã«ã³ã³ãã³ããè¿å´ããä»æããä½ãï¼
- ãç¹å®ã®ã¦ã¼ã¶ã¼ãã§ãããã©ããã¯Googleã¢ã«ã¦ã³ãã§ãã°ã¤ã³ä¸ã®ã¦ã¼ã¶ã¼ã®Gmailã¢ãã¬ã¹ã§å¤æããï¼
- ãã³ã³ãã³ããã¯ã©ã®ãããªãã¡ã¤ã«ã§ãOKï¼ä»åã¯ç»åãç¨ããï¼
ãã£ããã¨ããæé ã¨ããã°ã©ã ãç´¹ä»ããï¼
GCS ã«ãã¡ã¤ã«ãç½®ãã¦ãã
ããã°ã©ã ã§ç»åãçæããã¦GCSã«ä¿åãããï¼ã³ãã³ãã©ã¤ã³ããã¢ãããã¼ããããã§ããããã©ï¼ä»åã¯ã¦ã§ãUIã§æä½ãã¦ãã¡ã¤ã«ãä¿åããï¼
GAE ã®ããã°ã©ã ãæ¸ã
ã¡ã¤ã³ã®é¨åã ãè¼ãã¾ãï¼
from google.appengine.ext import blobstore from google.appengine.ext.webapp import blobstore_handlers # ã³ã³ãã³ãã«ã¢ã¯ã»ã¹ã§ããã¦ã¼ã¶ã¼ã®Gmailã¢ãã¬ã¹ MASTER_USERS = [] def is_master_user(user): if user is None: return False return user.email() in MASTER_USERS # äºãç»é²ãããã¦ã¼ã¶ã¼ã®ã¿ã«ã³ã³ãã³ããè¿å´ãã class GCSServingHandlerPrivate (blobstore_handlers.BlobstoreDownloadHandler): # /gs-p/<BUCKET_NAME>/<DIR_NAME>/<FILE_NAME> def get (self, bucket_name, dir_name, file_name): user = users.get_current_user() # ãã°ã¤ã³ä¸ã®ã¦ã¼ã¶ã¼æ å ±ãããã if is_master_user(user): blob_key = blobstore.create_gs_key('/gs/%s/%s/%s' % (bucket_name, dir_name, file_name)) self.send_blob(blob_key) else: # ç»é²ããã¦ããªãå ´åã¯ä»£ããã®ç»åãè¿ã blank_key = blobstore.create_gs_key('/gs/daiiz-bucket-1/public/illust.png') self.send_blob(blank_key)
ãã¨ã¯webapp2.WSGIApplication
ã®ã«ã¼ãã£ã³ã°ã®è¨å®ã追å ããï¼
app = webapp2.WSGIApplication([
...
('/gs-p/(.+)/(.+)/(.+)', GCSServingHandlerPrivate),
...
])
ã¢ã¯ã»ã¹ãã¦ã¿ã
以ä¸ã«ï¼å®éã« https://daiiz-apps.appspot.com/gs-p/daiiz-appspot/test/sushi.jpg ã<img>
ã§èªã¿è¾¼ãã§ã¿ãï¼
åã¯ãäºãç»é²ãããã¦ã¼ã¶ã¼ãã«å«ã¾ãã¦ããã®ã§ï¼äº¬é½ã®å¯¿å¸ã®åçãè¦ãããããã©ï¼å以å¤ã®äººãã¢ã¯ã»ã¹ããå ´åã¯ãã£ã©ã¯ã¿ã¼ã®ã¤ã©ã¹ããåã£ã¦ããã°å¤§æåï¼
ææ³
GAEã¨GCSã¯è§¦ã£ã¦ãã¦æ¥½ãã