瓶AttributeError的:“UNI code'对象有没有属性'告诉' [英] Flask AttributeError: 'unicode' object has no attribute 'tell'
问题描述
我想上传图片到Amazon S3的烧瓶中的应用程序和按键和元数据存储在Redis的分贝。这里是我的应用程序:
I'm trying to upload images to Amazon S3 with a Flask app and store the keys and metadata in a Redis db. Here is my app:
def s3upload(image, acl='public-read'):
key = app.config['S3_KEY']
secret = app.config['S3_SECRET']
bucket = app.config['S3_BUCKET']
conn = S3Connection(key, secret)
mybucket = conn.get_bucket(bucket)
r = redis.StrictRedis(connection_pool = pool)
iid = r.incr('image')
now = time.time()
r.zadd('image:created_on', now, iid)
k = Key(mybucket)
k.key = iid
k.set_contents_from_file(image)
return iid
@app.route('/', methods = ['GET', 'POST'])
def index():
form = ImageForm(request.form)
print 'CHECKING REQUEST'
if form.validate_on_submit():
print 'VALID REQUEST'
image = form.image.data
s3upload(image)
else:
image = None
r = redis.StrictRedis(connection_pool = pool)
last_ten = r.zrange('image:created_on', 0, 9)
print last_ten
images = []
key = app.config['S3_KEY']
secret = app.config['S3_SECRET']
bucket = app.config['S3_BUCKET']
conn = S3Connection(key, secret)
mybucket = conn.get_bucket(bucket)
for image in last_ten:
images.append(mybucket.get_key(image, validate = False))
return render_template('index.html', form=form, images=images)
成功的页面加载,但是当我尝试上载它返回一个错误的形象:
The page loads successfully, however when I try to upload the image it returns an error:
AttributeError异常:UNI code'对象有没有属性'告诉'
在 set_contents_from_file
在出现故障的线路是: SPOS = fp.tell()
The line the fails is: spos = fp.tell()
任何帮助是AP preciated感谢。
Any help is appreciated thanks.
推荐答案
k.set_contents_from_file
期待与命名方法的类文件对象告诉()
,你传递一个单code字符串。
k.set_contents_from_file
expects a file-like object with a method named tell()
, You pass it a unicode string.
您需要使用<一个href="https://sourcegraph.com/github.com/boto/boto/symbols/python/boto/s3/key/Key/set_contents_from_string"相对=nofollow> k.set_contents_from_string
代替。
You need to use k.set_contents_from_string
instead.
这篇关于瓶AttributeError的:“UNI code'对象有没有属性'告诉'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!