Google App Engine ã®ãã¼ã¿ã¹ãã¢ï¼Cloud Datastoreï¼ã«ä¿åãããã¢ã¤ãã ï¼ã¨ã³ãã£ãã£ï¼ã«ã¯ï¼ãã¨ã³ãã£ãã£ã®ç¨®é¡ãã¨ãidããä¸ãããã¦ããï¼
ä¾ãã°ï¼ã¦ã¼ã¶ã¼æ å ±ã管çãããã¼ã¿ã¢ãã«ã
from google.appengine.ext import db class User (db.Model): first_name = db.StringProperty() last_name = db.StringProperty() user_name = db.StringProperty()
ã®ããã«å®ç¾©ãã¦ï¼ããã®ã¤ã³ã¹ã¿ã³ã¹ããã¼ã¿ã¹ãã¢ã«ä¿åããå ´åã«ã¯ãã¨ã³ãã£ãã£ã®ç¨®é¡ãã¯User
ã¨ãªãï¼idã¯ä¿åæã«èªåã§æ¡çªãããï¼
ã¨ã³ãã£ãã£ã®ç¨®é¡, idãåå¾ããæ¹æ³
ããã°ã©ã ä¸ã§ã¨ã³ãã£ãã£eã®æ å ±ãåå¾ãããå ´åã¯ï¼é¢æ°keyã使ãã°ããï¼å ã®ãã®ã¨ã¯å¥ã®ä¾ã§ã®å®è¡çµæã¯ä»¥ä¸ã®ã¨ããï¼
print(e.key()) # -> datastore_types.Key.from_path(u'Screenshot', 5348024557502464L, _app=u'dev~svgscreenshot') print("%s" % str(s.key().id())) # ã¨ã³ãã£ãã£ã®id # -> 5348024557502464 print("%s" % s.key().kind()) # ã¨ã³ãã£ãã£ã®ç¨®é¡ # -> Screenshot
ã¨ã³ãã£ãã£ã®ç¨®é¡ãidãåå¾ã§ããã¨ï¼ã¨ã³ãã£ãã£åºæå 容ãã·ã§ã¢ããã¦ã§ããã¼ã¸ã®URLãæ§æããå ´åã«å½¹ç«ã¤ï¼
URLã»ã¼ããã¼
ä¸è¨ä¾ã§ã® e.key() ã§å¾ãããçµæãæåååããã¨ï¼ä»¥ä¸ã®ããã«ï¼
print("%s" % str(e.key())) # -> ahFkZXZ-c3Znc2NyZWVuc2hvdHIXCxIKU2NyZWVuc2hvdBiAgICAgIDACQw
å°ã
é·ãã®ãã¼ãå¾ãããï¼ããã¯ä½ãï¼
Google Cloud Consoleã®ããã¼ã¿ã¹ãã¢ãã®é
ç®ã§ã®èª¬æã«ããã¨ï¼ä¸è¨ã§å¾ãããæååã¯ãURL ã»ã¼ããã¼ãã¨å¼ã°ãããã®ãããï¼ä¸è¨å¼ç¨ã®ãããªèª¬æãæ¸ããã¦ããï¼
ããã¯ã¨ã³ãã£ãã£ã®ç¨®é¡ã¨ ID ã base64 ã§ã¨ã³ã³ã¼ããããã·ãªã¢ã©ã¤ãº ãã¼ã¸ã§ã³ã§ãã Cloud ConsoleãApp Engine ã¯ã©ã¤ã¢ã³ã ã©ã¤ãã©ãªãCloud Datastore NDB ã¯ã©ã¤ã¢ã³ã ã©ã¤ãã©ãªã® URL ã»ã¼ããã¼ã使ç¨ã§ãã¾ãã è¦å: URL ã»ã¼ããã¼ã¯æå·åããã¦ãã¾ããã種é¡ã ID ã«æ©å¯ãã¼ã¿ãå«ã¾ãã¦ããå ´åãURL ã»ã¼ããã¼ããç°¡åã«è§£èªã§ããã®ã§æ³¨æãã¦ãã ããã
ãããèªãéãã ã¨ï¼URLã»ã¼ããã¼ã解èªï¼base64ãã³ã¼ãï¼ããã°ãã¨ã³ãã£ãã£ã®ç¨®é¡ãã¨ãidããåå¾ãããã¨ãã§ãããããªã®ã§ï¼ææ¦ãã¦ã¿ãï¼
url_safe_key = 'ahFkZXZ-c3Znc2NyZWVuc2hvdHIXCxIKU2NyZWVuc2hvdBiAgICAgIDACQw' decoded_key = base64.urlsafe_b64decode(url_safe_key + ('=' * (4 - len(url_safe_key) % 4))) # -> 'j\x11dev~svgscreenshotr\x17\x0b\x12\nScreenshot\x18\x80\x80\x80\x80\x80\x80\xc0\t\x0c'
16é²è¡¨è¨ã¨å¶å¾¡æåã®å¤æã¯ï¼http://www.oocities.org/dtmcbride/tech/charsets/ascii.html ãçºããã¨ãããï¼
- \x11: ^Q (Default UNIX START char)
- dev~svgscreenshot: ã¢ããªåï¼svgscreenshotã®ãã¼ã«ã«éçºç°å¢ï¼
- \x17: ^W (End of transmission block)
- \x0b\x12 ^K (Vertical tab) ^R
- \n: æ¹è¡
- Screenshot: ã¨ã³ãã£ãã£ã®ç¨®é¡
- \x18: ^X (Cancel)
- \x80\x80\x80\x80\x80\x80\xc0: ããããidã表ãã¦ããï¼ï¼æ£è§£id = 5348024557502464 = 128 ^ 6 + 1216ï¼128㯠\x80 ã®10é²è¡¨è¨ã§ï¼6ã¯\x80ã®åºç¾åæ°ï¼ï¼1216ã®åºå¦ãä¸æï¼\xc0æªä½¿ç¨ï¼
- \t: ã¿ã
- \x0c: ^L (Form feed)
ã¨ã³ãã£ãã£idãããããªã...
ãã®ãããã®è§£èªæ¹æ³ã«ã¤ãã¦ï¼ä¸æããããããªææ³ããåç¥ã®æ¹ãããã°æãã¦ãã ããï¼ï¼