【问题标题】:Flask AttributeError: 'unicode' object has no attribute 'tell'Flask AttributeError:“unicode”对象没有属性“tell”
【发布时间】:2014-04-15 17:27:58
【问题描述】:

我正在尝试使用 Flask 应用程序将图像上传到 Amazon S3,并将密钥和元数据存储在 Redis 数据库中。这是我的应用程序:

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)

页面加载成功,但是当我尝试上传图片时返回错误:

AttributeError: 'unicode' object has no attribute 'tell'set_contents_from_file

失败的那一行是:spos = fp.tell()

感谢任何帮助。

【问题讨论】:

    标签: python amazon-web-services amazon-s3 flask flask-wtforms


    【解决方案1】:

    您需要改用k.set_contents_from_string

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-05
    • 2017-05-19
    • 2019-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多