本文整理汇总了Python中boto.s3.connection.Key.get_contents_as_string方法的典型用法代码示例。如果您正苦于以下问题:Python Key.get_contents_as_string方法的具体用法?Python Key.get_contents_as_string怎么用?Python Key.get_contents_as_string使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类boto.s3.connection.Key
的用法示例。
在下文中一共展示了Key.get_contents_as_string方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_s3
# 需要导入模块: from boto.s3.connection import Key [as 别名]
# 或者: from boto.s3.connection.Key import get_contents_as_string [as 别名]
def test_s3(self):
title = "Dino Test"
link = "http://example.com"
description = "My dino test"
base_url = "http://example.com/items/"
aws_key = os.environ['MYDINOSAUR_AWS_ACCESS_KEY']
aws_secret = os.environ['MYDINOSAUR_AWS_SECRET_KEY']
s3_bucket = os.environ['MYDINOSAUR_S3_BUCKET']
dino = mydinosaur.MyS3Dinosaur(':memory:',
title=title,
link=link,
description=description,
base_url=base_url,
aws_access_key=aws_key,
aws_secret_key=aws_secret,
s3_bucket=s3_bucket)
dino.update("hello there")
# make sure that stuff got uploaded
conn = S3Connection(aws_key, aws_secret)
bucket = conn.get_bucket(s3_bucket)
k = Key(bucket)
k.key = 'rss.xml'
feed = feedparser.parse(k.get_contents_as_string())
self.assertEqual(len(feed.entries), 1)
k = Key(bucket)
k.key = '1.html'
html = k.get_contents_as_string()
self.assertIn('<!-- MyDinosaur default html template -->', html)
# now, update with media!
filehandle = StringIO("Hello there.\n")
dino.update_with_media('this is a test', filehandle,
media_type="text/plain", ext="txt")
k = Key(bucket)
k.key = 'rss.xml'
feed = feedparser.parse(k.get_contents_as_string())
self.assertEqual(len(feed.entries), 2)
# ensure that media was uploaded
media_url = feed.entries[0].enclosures[0].url
key_name = urlparse(media_url).path.split('/')[-1]
k = Key(bucket)
k.key = key_name
contents = k.get_contents_as_string()
self.assertEqual(contents, "Hello there.\n")