将图像上传到S3(boto + GAE)

phy*_*ikz 3 python google-app-engine boto

我正在尝试使用我的GAE python应用程序获取s3(使用boto)设置来存储用户上传的图像.目前我收到以下错误:

File "/Users/phyzikz/project/boto/s3/key.py", line 936, in set_contents_from_file spos = fp.tell()
AttributeError: 'str' object has no attribute 'tell'
Run Code Online (Sandbox Code Playgroud)

我不知道为什么会发生这种情况 - 上传的文件应该是png.这是上传的代码:

class Settings(Handler):
    def get(self):
        self.render('settings.html')

    def post(self):
        image = self.request.get('image')

        if image:
            connection = S3Connection('<ak>','<sak>')
            bucket = connection.create_bucket('<s3bucket>')
            k = Key(bucket)
            k.key = '/pictures/users/'+ str(self.user.key().id())
            k.set_contents_from_file(image)
Run Code Online (Sandbox Code Playgroud)

如果它有帮助,在调试时,当用set_contents_from_string('some string')替换set_contents_from_file(image)时,它工作得非常好.我一定很遗憾.这是html:

<form method='post' action='/settings' enctype='multipart/form-data'>
    <input type='file' name='image'>
    <input type='submit'>
</form>
Run Code Online (Sandbox Code Playgroud)

免责声明:我是python和SO的新手(这是我的第一个问题!)如果有必要,任何改进问题措辞的编辑都将受到赞赏.

Tim*_*man 6

您需要将图像包装在StringIO实例中,使其看起来像文件对象

Python 2.7.2+ (default, Oct  4 2011, 20:03:08) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = "123"
>>> from StringIO import StringIO
>>> y = StringIO(x)
>>> y.tell()
0
>>> 
Run Code Online (Sandbox Code Playgroud)

所以在你的情况下,你会

k.set_contents_from_file(StringIO(image))
Run Code Online (Sandbox Code Playgroud)