上传图像到S3 (boto + GAE)

[英]Uploading image to S3 (boto + GAE)


I'm trying to get s3 (using boto) setup with my GAE python app to store images uploaded by users. Currently I get the following error:

我尝试使用我的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'

I'm at a loss for why this is happening – the file uploaded should be a png. Here's the code that makes the upload:

我不知道为什么会这样,上传的文件应该是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)

If it helps, while debugging it worked beautifully when substituting set_contents_from_file(image) with set_contents_from_string('some string'). I must be missing something simple. Here's the html:

如果它有帮助,在调试它的时候,用set_contents_from_file(图像)替换set_contents_from_string('some string')时,它工作得很好。我肯定错过了一些简单的东西。html:

<form method='post' action='/settings' enctype='multipart/form-data'>
    <input type='file' name='image'>
    <input type='submit'>
</form>

disclaimer: I'm new to both python and SO (this is my first question!) Any edits to improving the phrasing of the question if necessary would be appreciated.

免责声明:我是python的新手,所以(这是我的第一个问题!)如果有必要的话,任何可以改善问题措辞的编辑都会被欣赏。

2 个解决方案

#1


6  

You will need to wrap image in a StringIO instance so it looks like a file object

您需要在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
>>> 

So in your case you would

所以在你的情况下,你会。

k.set_contents_from_file(StringIO(image))

#2


0  

If you can put the code (in the method: set_contents_from_file inside the Key class ) where you have defined or assigned fp, we can help better. But, looking at the error AttributeError: 'str' object has no attribute 'tell', it is quite clear that fp is not a file object. It is a string object, hence the error. As you are new to SO, first try to correct the code yourself, then, if you ae still not getting it resolved, you can paste the code you are using to change fp to a file object.

如果您可以将代码(在方法:set_contents_from_file中)放入您已经定义或指定的fp,我们可以更好地帮助您。但是,查看错误AttributeError:“str”对象没有属性“tell”,很明显,fp不是一个文件对象。它是一个字符串对象,因此出现了错误。由于您是新手,首先尝试更正代码,如果您仍然没有解决它,您可以将您正在使用的代码粘贴到一个文件对象中。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2012/09/09/3a78be26c5c84f6dc7db7938d968d44d.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告