尝试将图像上传到亚马逊s3上的水桶。
图像通过表单上传,图像存储在“文件”中:
file = request.files['file']
我也尝试将其重命名为'filename',其中包含上传图像的用户的ID。
我的应用程序在Heroku上运行
以下是代码:
filename = str(userName.id)
# S3_BUCKET, AWS_ACCESS_KEY & AWS_SECRET_KEY = HEROKU envar from config.py
conn = boto.connect_s3(app.config['AWS_ACCESS_KEY'], app.config['AWS_SECRET_KEY'])
bucket = conn.get_bucket(app.config['S3_BUCKET'])
key = '%s.jpg' % filename
k = Key(bucket)
k.key = key
k.set_contents_from_filename(file.filename)
当我尝试上传名为'888888.jpg'的图片时,我在heroku日志中收到以下错误:
2015-02-12T22:56:33.214246+00:00 heroku[router]: at=info method=POST path="/registering" host=monkey-me.herokuapp.com request_id=261d25bd-10bc-4703-aa38-badbcb4e634e fwd="213.243.186.53" dyno=web.1 connect=1ms service=809ms status=500 bytes=456
2015-02-12T22:56:33.181111+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
2015-02-12T22:56:33.181113+00:00 app[web.1]: rv = self.dispatch_request()
2015-02-12T22:56:33.181101+00:00 app[web.1]: Traceback (most recent call last):
2015-02-12T22:56:33.181105+00:00 app[web.1]: response = self.full_dispatch_request()
2015-02-12T22:56:33.181082+00:00 app[web.1]: Exception on /registering [POST]
2015-02-12T22:56:33.181103+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
2015-02-12T22:56:33.181106+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
2015-02-12T22:56:33.181110+00:00 app[web.1]: reraise(exc_type, exc_value, tb)
2015-02-12T22:56:33.181107+00:00 app[web.1]: rv = self.handle_user_exception(e)
2015-02-12T22:56:33.181114+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
2015-02-12T22:56:33.181117+00:00 app[web.1]: File "/app/app.py", line 361, in registering
2015-02-12T22:56:33.181121+00:00 app[web.1]: with open(filename, 'rb') as fp:
2015-02-12T22:56:33.181119+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/boto/s3/key.py", line 1358, in set_contents_from_filename
2015-02-12T22:56:33.181116+00:00 app[web.1]: return self.view_functions[rule.endpoint](**req.view_args)
2015-02-12T22:56:33.181109+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
2015-02-12T22:56:33.181122+00:00 app[web.1]: IOError: [Errno 2] No such file or directory: u'888888.jpg'
2015-02-12T22:56:33.181118+00:00 app[web.1]: k.set_contents_from_filename(file.filename)
我做错了什么(这是我第一次使用S3)
答案 0 :(得分:0)
You need to store the file on disk。您还可以使用cStringIO将文件加载到内存中并将其上载到S3而不是先写入磁盘,但请记住,大文件需要上传块,否则您将使用以下方法填充内存。以下是如何使用cStringIO
的示例# Python 2.7
import cStringIO
buff = cStringIO.StringIO()
f = request.FILES['file']
buff.write(f.read())
buff.seek(0)
k.set_contents_from_file(buff)