首页 > 解决方案 > GAE Python存储桶上传不起作用

问题描述

我正在尝试将文件上传到我的谷歌云存储桶。但它没有出现,虽然它在一段时间前工作过,所以我知道存储桶的位置是正确的。但它是用 0b 保存它或根本没有它。我将如何解决这个问题?

路线

@app.route('/admin/create/post/<int:id>', methods=['GET', 'POST'])
def create_post(id=None):
    """Display form for creating a post."""
    form = forms.Post()
    if form.validate_on_submit():
        models.Post.create(project_id=id,
                           name=form.name.data,
                           content=form.content.data)
        post = (models.Post.select()
                .where(
                    (models.Post.name == form.name.data),
                    (models.Post.content == form.content.data)).get())
        for filename in request.files:
            file = request.files.get(str(filename))
            upload_image_file(file, post.id)
        return redirect(url_for('admin'))
    return render_template('create_post.html', form=form)

上传图片文件功能

def upload_image_file(filename, filecontent, post_id):
    """Save file."""
    logging.debug("""upload_image_file, name={},content={},postid={}"""
                  .format(filename, filecontent, post_id))
    path = 'post/' + '{}/'.format(post_id)
    path = path.lower()
    filepath = (path + str(filename)).lower()
    client = _get_storage_client()
    logging.debug('select storage')
    bucket = client.bucket(app.config['CLOUD_STORAGE_BUCKET'])
    logging.debug('select bucket')
    blob = bucket.blob(filepath)
    print('file read = {}'.format(filecontent))
    # blob.upload_from_string(file.read())
    blob.upload_from_string(
        'test.txt',
        content_type=filecontent)
    url = blob.public_url
    # try:
    #     url = url.decode('utf-8')
    #     logging.debug('decoded url = {}'.format(url))
    # except Exception:
    #     logging.debug('Did not decode = {} | url'.format(url))
    models.Media.create(
        post_id=post_id,
        media=url)

标签: pythongoogle-app-engineflaskgoogle-cloud-datastore

解决方案


您的代码中的错误。主要的一个:

你打电话upload_image_file(file, post.id)

然而,该upload_image_file()函数需要 3 个参数:

def upload_image_file(filename, filecontent, post_id):

你的日志说什么?


推荐阅读