ä½åº¦ããã£ããã¨ããã®ã«ãããã°ã«æ¸ãã¦ãªãã£ãã£ã½ãã®ã§æ¸ãã
Pythonããbotoã使ã£ã¦AWSã®S3ã«ãã¡ã¤ã«ãã¢ãããã¼ãããæ¹æ³ã
試ããPythonã®ãã¼ã¸ã§ã³ã¯2.6ãbotoã¯2.0rc1
botoã®ã¤ã³ã¹ãã¼ã«
pipãeasy_installã§å ¥ããã°ããã
$ pip install boto
S3ã¸ã®ãã±ããã®ç¨æ
ã¢ãããã¼ãå
ã®ãã±ãããç¨æããã
botoã®APIã§ä½æãããã¨ãã§ããããä»åã¯AWSã®ã³ã³ã½ã¼ã«ããä½æãã¦ããã
S3ã®ã¿ãã®Bucketsãã"Create Bucket"ã§ãã±ããåãå
¥åãã¦ä½æã
ä»å㯠test-boto.nullpobug.com ã¨ããã(å¥ã«ãã¡ã¤ã³åã«ãªã£ã¦ãªãã¦ãããããCNAMEå²ãå½ã¦ã§ã®ã¢ã¯ã»ã¹ã使ããªããã¡ã¤ã³åã«ããå¿
è¦ããã)
ã¢ã¯ã»ã¹ãã¼ã®ç¢ºèª
AWSã®APIãå©ç¨ããã«ã¯ã¢ã¯ã»ã¹ãã¼ã¨ã·ã¼ã¯ã¬ããã¢ã¯ã»ã¹ãã¼ã®2ã¤ãå¿
è¦ã
ã»ãã¥ãªãã£è¨¼ææ¸ã®ãã¼ã¸ãã確èªãåå¾ãã¦ããã
botoã®ã³ãã¯ã·ã§ã³
S3ã使ãå ´åã¯ãbotoã®S3Connectionãªãã¸ã§ã¯ããä½ã£ã¦ããå¿
è¦ãããã
2ã¤ã®ãã¼ã¯å¼æ°ã¨ãã¦ä¸ããããç°å¢å¤æ°ã§è¨å®ãã¦ããã
>>> from boto.s3.connection import S3Connection >>> conn = S3Connection('ã¢ã¯ã»ã¹ãã¼ã®æåå', 'ã·ã¼ã¯ã¬ããã¢ã¯ã»ã¹ãã¼ã®æåå')
ãã®connãªãã¸ã§ã¯ãã®ã¡ã½ããããã´ãã§ã´ãã§éå§ããã
ä¸èº«ã®éä¿¡ã¯HTTPSã§XMLæãã£ãã
ãã©ã«ãã®ä½æ
ãã±ããã¯ä½ææ¸ã¿ãªã®ã§ãã³ãã¯ã·ã§ã³ãããã±ããã®ãªãã¸ã§ã¯ããå¾ãã
>>> bucket = conn.get_bucket('test-boto.nullpobug.com')
>>> bucket
<Bucket: test-boto.nullpobug.com>
ãã±ããã«ã¯Keyãªãã¸ã§ã¯ãã¨ãã¦ãã¡ã¤ã«ãä¿åã§ããã
ãã©ã«ãããã¡ã¤ã«ãã¨ããããã©ã«ãã¨ããæ¦å¿µããªãã®ã§ãS3ç¨ã®ãã¼ã«é¡ãªã©ãããã©ã«ãã¨ãã¦è¦ããã®ã¯ãkeyã®æå¾ãã»ãã¬ã¼ã¿ã®ã¹ã©ãã·ã¥(/)ã«ãªã£ã¦ããã®ã ã£ãããæ¡å¼µåã "_$folder$" ã¨ãªã£ã¦ããã®ã¿ããã
ãã±ããã®ä¸èº«ãã¨ãããã確èªããã«ã¯ãlisté¢æ°ã§è¦ãã°ããã
>>> list(bucket)
[]
ä¸èº«ãå¤ãã¨å¦çãæ»ã£ã¦ããªãã®ã§æ³¨æã__iter__ã§ã¢ã¯ã»ã¹ã§ããã®ã¼ãã
ä½ã£ãã°ã£ãããªã®ã§ç©ºã«ãªã£ã¦ãã
ãã©ã«ããä½ã£ã¦ã¿ãã
ã³ã³ãã³ãã¯ç©ºã§ããã®ã§ç©ºæååãæå®ãset_contents_from_stringãå¼ã³åºããã¿ã¤ãã³ã°ã§ãªã¯ã¨ã¹ããæãã¦ãã¿ããã§ãã
>>> from boto.s3.key import Key >>> k = Key(bucket) >>> k.key = 'test_folder/' >>> k.set_contents_from_string('')
ããã§test_folderã¨ããååã®ãã©ã«ããä½ãããã
Management Consoleããè¦ãã¨ãããªæãã
ãã¡ã¤ã«ãã¢ãããã¼ããã
ãã¡ã¤ã«ãåæ§ã«Keyãªãã¸ã§ã¯ããä½æããã°ã¢ãããã¼ãã§ãã¾ãã
>>> f = Key(bucket) >>> f.key = 'test.txt' >>> f.set_contents_from_filename('test.txt')
ããã§ã«ã¬ã³ããã©ã«ãã«ããtest.txtã¨ããååã®ãã¡ã¤ã«ãã¢ãããã¼ãã§ãã¾ããã
ããã¡ãã