Google App Engine AttributeError:“ NoneType”对象没有属性“ read”

时间:2019-07-24 23:46:17

标签: python google-app-engine flask google-cloud-storage

我想让用户通过表单将图像上传到Google存储,但是却收到此错误AttributeError:'NoneType'对象没有属性'read'。

我看过app engine documentation 在那里上传图片也做同样的事情。

这是我的代码中与上传图片有关的部分

def save_picture(form_picture):
    if form_picture:
        random_hex = urandom(8).hex()
        _, f_ext = os.path.splitext(form_picture.filename)
        picture_fn = random_hex + f_ext

    gcs = storage.Client()
    bucket = gcs.get_bucket(CLOUD_STORAGE_BUCKET)      
    blob = bucket.blob(picture_fn)                  

    blob.upload_from_string(
    form_picture.read(),
    content_type=form_picture.content_type
    )

    return blob.public_url

@app.route('/CreatePost/amirshahedi', methods=['GET','POST'])
def CreatePost():
    form = Create()
    if form.validate_on_submit():
        uploaded_file = request.files.get('file')
        image_url = save_picture(uploaded_file)
        post = Post(category=form.category.data, title=form.title.data, content=form.content.data, price=form.price.data, quantity=form.quantity.data, image_file = image_url )
        db.session.add(post)
        db.session.commit()
    return render_template('CreatePost.html', title="CreatePost", form1=form)

0 个答案:

没有答案