是否可以使用boto从Google App Engine中的S3读取文件?

2024-09-28 17:05:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我想操作存储在Google App Engine沙盒中的S3中的pickled python对象。我用博托的建议documentation

from boto.s3.connection import S3Connection

from boto.s3.key import Key

conn = S3Connection(config.key, config.secret_key)
bucket = conn.get_bucket('bucketname')
key = bucket.get_key("picture.jpg")
fp = open ("picture.jpg", "w")
key.get_file (fp)

但这需要我写一个文件,这显然是不洁的GAE沙箱。

我怎么能避开这个? 非常感谢你的帮助


Tags: keyfromimportconfigappgets3bucket
2条回答

你根本不需要写文件或字符串。您可以调用key.get_contents_as_string()将键的内容作为字符串返回。key的文档是here

可以写入blob并使用StringIO检索数据

from boto.s3.connection import S3Connection
from boto.s3.key import Key
from google.appengine.ext import db

class Data(db.Model)
    image = db.BlobProperty(default=None)

conn = S3Connection(config.key, config.secret_key)
bucket = conn.get_bucket('bucketname')
key = bucket.get_key("picture.jpg")
fp = StringIO.StringIO()
key.get_file(fp)

data = Data(key_name="picture.jpg")
data.image = db.Blob(fp.getvalue())
data.put()

相关问题 更多 >